sidebar.phpの中でよく使うWordPressのコード

HTMLサイトをWordPressにする本

年別アーカイブリストを表示

・投稿タイプ=投稿(post)/カスタム投稿の場合は(info-en)のように指定
・ 年別(yearly)※月別にしたい場合は「monthly」に変更
・ 件数表示あり(show_post_count=1)

<ul class="archive-list">
  <?php wp_get_archives('post_type=post&type=yearly&show_post_count=1'); ?>
</ul>

デフォルトでついていない「年」を出力するため、functions.phpに以下を追記

function add_nen_year_archives( $link_html ) {
  $regex = array (
    "/ title='([\d]{4})'/" => " title='$1年'",
    "/ ([\d]{4}) /"        => " $1年 ",
    "/>([\d]{4})<\/a>/"    => ">$1年</a>"
  );
  $link_html = preg_replace( array_keys( $regex ), $regex, $link_html );
  return $link_html;
}
add_filter( 'get_archives_link', 'add_nen_year_archives' );

セレクトボックスを使った年別アーカイブリストを表示

・投稿タイプ=投稿(post)/カスタム投稿の場合は(info-en)のように指定
・ 年別(yearly)※月別にしたい場合は「monthly」に変更
・ 後ろに「年」を入れる(after=年)

<select name="archive-dropdownlist" onchange="document.location.href=this.options[this.selectedIndex].value;">
  <option disabled selected value>アーカイブ</option>
  <?php wp_get_archives('post_type=post&type=yearly&format=option&after=年'); ?>
</select>

カテゴリーリストを表示

・カテゴリー一覧=タイトルなし(title_li=)

<ul class="archive-list">
  <?php wp_list_categories('title_li='); ?>
</ul>

・件数表示あり(show_count=1)

<ul class="archive-list">
  <?php wp_list_categories('title_li=&show_count=1'); ?>
</ul>

・一部のカテゴリーのみ表示(include=「ID1」,「ID2」)

<?php wp_list_categories('title_li&include=1,2'); ?>

・一部のカテゴリーを非表示(exclude=「ID1」,「ID2」)

<?php wp_list_categories('title_li&exclude=1,2'); ?>

ターム一覧を表示

・ターム一覧=タイトルなし(title_li=)、タクソノミー名は 「info-encat」

<ul>
  <?php wp_list_categories('title_li=&taxonomy=info-encat'); ?>
</ul>

・件数表示あり(show_count=1)、タクソノミー名は 「info-encat」

<ul class="archive-list">
  <?php wp_list_categories('title_li=&show_count=1&taxonomy=info-encat'); ?>
</ul>

・一部のタームのみ表示(include=「ID1」,「ID2」)、タクソノミー名は 「info-encat」

<?php wp_list_categories('title_li&include=1,2&taxonomy=info-encat'); ?>

・一部のタームを非表示(exclude=「ID1」,「ID2」)、タクソノミー名は 「info-encat」

<?php wp_list_categories('title_li&exclude=1,2&taxonomy=info-encat'); ?>

タグクラウドを表示

・登録されているタグをulタグで表示

<?php wp_tag_cloud('format=list'); ?>

・一部のタグのみ表示(include=「ID1」,「ID2」)

<?php wp_tag_cloud('include=1,2'); ?>

・一部のタグを非表示(exclude=「ID1」,「ID2」)

<?php wp_tag_cloud('exclude=1,2'); ?>

検索ボックスを表示

オリジナルのボタン画像を入れてサイト全体を検索する

<form id="form-search" class="search-form" action="<?php echo esc_url(home_url('/')); ?>" method="get">
    <input type="text" id="search" name="s" placeholder="記事を検索" class="search-box" value="<?php the_search_query(); ?>">
    <input type="image" src="<?php echo get_stylesheet_directory_uri(); ?>/img/icon-search.svg" alt="検索" class="btn-search">
    <!--<input type="hidden" name="post_type" value="diary"> ※特定のカスタム投稿のみ検索範囲にする場合は valueにカスタム投稿名を書く(例)diary-->
</form>

現在の記事を除いた最新の投稿を表示

<?php
$id = get_the_ID();  // 現在の記事のIDを取得
$args = array(
  'post_type' => 'post',  // 投稿タイプ:「投稿」
  'post__not_in' => array($id),  // 現在の記事以外を取得
  'posts_per_page' => 5,  // 取得したい件数
  'post_status' => 'publish',  // 投稿ステータス:公開済み
);
$the_query = new WP_Query($args);
if($the_query->have_posts()):
?>
<ul>
  <?php while($the_query->have_posts()): $the_query->the_post(); ?>
  <li><a href="<?php the_permalink(); ?>">
    <figure class="img-block">
    <?php if(has_post_thumbnail()): ?>
      <?php the_post_thumbnail('thumbnail'); ?>
    <?php else: ?>
      <img src="<?php echo get_stylesheet_directory_uri()?>/img/comingsoon.jpg" alt="comingsoon">
    <?php endif; ?>
    </figure>
    <div class="txt-block">
      <time datetime="<?php the_time('Y-m-d') ?>"><?php the_time(get_option('date_format')); ?></time>
      <h3><?php the_title();?></h3>
    <?php 
      $cats = get_the_category();
      if($cats):
    ?>
      <ul class="post-categories">
      <?php foreach($cats as $cat): ?>
        <li><?php echo $cat->name; ?></li>
      <?php endforeach; ?>
      </ul>
    <?php endif; ?>
    </div>
  </a></li>
  <?php endwhile; ?>
</ul>
<?php endif; ?>
<?php wp_reset_postdata(); ?>

ウィジェット機能を表示

必ずfunctions.phpの記述とセットで使用してください

<?php dynamic_sidebar('news-widgets'); ?>

最低限覚えておきたい
WordPressのコード