投稿数をアンカータグ内に入れる(カテゴリー一覧などリスト表示するとこ)

Filed under: functions.php,wordpress — kdcs @ 17年12月9日 土曜日

投稿数をアンカータグ内に入れる方法

テンプレート内に記述する方法(ulリストタグを吐き出し、フォントアイコンを使う)

<ul>
<?php
$args=array(
  'orderby' => 'name',
  'order' => 'ASC'
  );
$categories=get_categories($args);
  foreach($categories as $category) { 
    echo '<li><a href="' . get_category_link( $category->term_id ) . '" title="'.$category->name.'" ' . '>'.'<i class="fa icon-search"></i>' . $category->name.' ('.$category->count.')'.'</a> </li> ';
 }
?>
</ul>

function.php内に記述する方法

◆カテゴリー一覧

// カテゴリの投稿数をaタグの中に入れる --------------------------------
add_filter( 'wp_list_categories', 'my_list_categories', 10, 2 );
function my_list_categories( $output, $args ) {
  $output = preg_replace('/<\/a>\s*\((\d+)\)/',' ($1)</a>',$output);
  return $output;
}

◆アーカイブ一覧

// アーカイブの投稿数をaタグの中に入れる ------------------------------
add_filter( 'get_archives_link', 'my_archives_link' );
function my_archives_link( $output ) {
  $output = preg_replace('/<\/a>\s*( )\((\d+)\)/',' ($2)</a>',$output);
  return $output;
}

※テンプレートへの記述は以下

<h3>カテゴリー</h3>
<ul class="sp_list">
<?php wp_list_categories('title_li=&orderby=ID&hide_empty=0&show_count=1'); ?>
</ul>

※レスポンシブでのスマホcss

#aside ul.sp_list {
  margin-bottom: 20px;
  }
#aside ul.sp_list li {
  padding:0;
  font-size: 107%;
  background-image: none;
  border-bottom: 1px dotted #ccc;
  }
#aside ul.sp_list li:first-child {
  border-top: 1px dotted #ccc;
  }
#aside ul.sp_list li a {
  background-image: url("images/list_mark_mb.gif");
  background-position: 98% 50%;
  background-repeat: no-repeat;
  background-size: 10px 16px;
  font-size: 107%;
  color: #666;
  display: block;
  padding: 10px 25px 10px 10px;
  text-decoration: none;
  }
#aside ul.sp_list li a:hover {
  background-color: #f9f9f9;
  }

サイト内検索

カテゴリー

最近の投稿

« |投稿数をアンカータグ内に入れる(カテゴリー一覧などリスト表示するとこ)| »
↑上に戻る