WordPress

リンクURL

<link>タグ href属性の後に

<?php echo get_stylesheet_directory_uri() ?>/

<link rel="stylesheet" href="<?php echo get_stylesheet_directory_uri() ?>/style.css">
 <?php echo get_stylesheet_directory_uri() ?>//で直下(ルート)まで見ているので、この場合はファイル名だけで大丈夫

<a>タグ href属性の後に

<?php echo esc_url( home_url( '/' ) ); ?>

<a href="<?php echo esc_url( home_url( '/' ) ); ?>#">

<script>タグ<img>タグ src属性の後に 

<?php echo get_stylesheet_directory_uri() ?>

<script src="<?php echo get_stylesheet_directory_uri() ?>/js/perlin.js"></script>
<img src="<?php echo get_stylesheet_directory_uri() ?>/img/title_bg_blog@2x.png" alt="BLOG">

ワードプレスループ

<?php if(have_posts()): while(have_posts()):the_post(); ?>
//繰り返し処理する内容
<?php endwhile; endif; ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
   //繰り返し処理する内容
<?php endwhile; else : ?>
	//投稿データが取得できない場合の処理
<?php endif; ?>

コンテンツの出力
(主にシングルページ、アーカイブページ)

//タイトル
<?php the_title(); ?>

//投稿日
<?php the_time('Y-m-d'); ?>

//該当カテゴリのみリンクなし
 <?php
$categories = get_the_category();
foreach( $categories as $category ) {
echo $category->name.' ';
 }
?>              

//記事コンテンツ
<?php the_content(); ?>

コンテンツの出力(サイドメニュー)

//登録カテゴリすべて、リンク付きリスト表示
<?php
$categories = get_categories();
foreach($categories as $category) {
   echo '<li><img src='.get_stylesheet_directory_uri().'/images/concierge/icon_hart.jpg" alt="ハートアイコン"><a href="'.get_category_link($category->term_id).'">'.$category->name.'</a></li>';
}
?>            

リンク出力

<a href="<?php the_permalink(); ?>"></a><

アイキャッチ出力

<!--アイキャッチ出力-->
 <?php if (has_post_thumbnail()) : ?>
 <div class="item_img" style="background: url(<?php echo the_post_thumbnail_url(); ?>) no-repeat center/contain;">
 </div>
<?php else : ?>
<!--アイキャッチがない時の画像-->
 <div class="item_img" style="background: url(<?php bloginfo('template_url'); ?>/images/common/no_image.gif) no-repeat center/contain;">
 </div>
<?php endif ; ?>
<!--/アイキャッチ出力 end-->

本文の文字数を100文字で制限して、HTMLタグを削除して表示

参考https://takami-design.jp/webtips/p281

<?php
if ( mb_strlen( $post->post_content, 'UTF-8' ) > 100 ) {
 $content = mb_substr( strip_tags( $post->post_content ), 0, 100, 'UTF-8' );
 echo $content . '…';
} else {
 echo strip_tags( $post->post_content );
}
?>

カスタム投稿タイプで特定IDを指定して特定の記事のみを表示【WP_Query】

参考:https://into-the-program.com/particular-article-only-show/

<?php
$args = array(
 //特定の記事のみを取得する条件を指定
 'post_type' => 'place',
 'page_id' => $hall_id
);
$set_query = new WP_Query( $args ); // WP_Queryオブジェクト生成
?>
<?php if ( $set_query->have_posts() ): ?>
<?php while ( $set_query->have_posts() ) : $set_query->the_post(); ?>

//繰り返し処理する内容
   
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_postdata(); ?>

カスタム投稿タイプで指定したタームの記事一覧を表示

参考https://cotodama.co/custom-post-type_terms_list/

<?php
$custom_posts = get_posts(array(
   'post_type' => 'カスタム投稿タイプスラッグ', // 投稿タイプ
   'posts_per_page' => 6, // 表示件数
   'orderby' => 'date', // 表示順の基準
   'order' => 'DESC', // 昇順・降順
   'tax_query' => array(
       array(
           'taxonomy' => 'タクソノミースラッグ', //タクソノミーを指定
           'field' => 'slug', //ターム名をスラッグで指定する
           'terms' => 'タームスラッグ', //表示したいタームをスラッグで指定
           'operator' => 'IN'
       ),
   )
));
global $post;
if($custom_posts): foreach($custom_posts as $post): setup_postdata($post); ?>
 
<!-- ループはじめ -->
<h3><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h3>
<p><?php the_time('Y/m/d') ?></p>
<p><?php the_excerpt(); ?></p>
<!-- ループおわり -->
 
<?php endforeach; wp_reset_postdata(); endif; ?>

この記事が気に入ったらサポートをしてみませんか?