現在表示している記事と同じカテゴリーの人気記事のリストを表示するには?
WPP Plus Widget というプラグインを使ってみたら、その記事の所属カテゴリーだけに限定することができず、その記事のカテゴリーの親のカテゴリーの記事まで表示されてしまう不思議に遭遇した。
他にも時間の表示フォーマットを変更できなかったり、不具合があったので使用停止。
PHP Code Widget に独自PHPコードを放り込むほうが良い。
Simple GA Ranking プラグインの機能を使用するので、インストールしておく。
<h4 class="widgettitle"><span class="side-title-inner">
<?php
// SHOW YOAST PRIMARY category, OR FIRST category
$category = get_the_category();
$useLink = true;
// If post has a category assigned.
if ($category){
$category_display = '';
$category_link = '';
if ( class_exists('WPSEO_Primary_Term') )
{
// Show the post's 'Primary' category, if this Yoast feature is available, & one is set
$wpseo_primary_term = new WPSEO_Primary_Term( 'category', get_the_id() );
$wpseo_primary_term = $wpseo_primary_term->get_primary_term();
$term = get_term( $wpseo_primary_term );
if (is_wp_error($term)) {
// Default to first category (not Yoast) if an error is returned
$category_display = $category[0]->name;
$category_link = get_category_link( $category[0]->term_id );
$category_slug = $category[0]->slug;
$now_id = $category[0]->term_id;
} else {
// Yoast Primary category
$category_display = $term->name;
$category_link = get_category_link( $term->term_id );
$category_slug = $term->slug;
$now_id = $term->term_id;
}
}
else {
// Default, display the first category in WP's list of assigned egories
$category_display = $category[0]->name;
$category_slug = $category[0]->slug;
$category_link = get_category_link( $category[0]->term_id );
$category_id = $term->term_id;
$now_id = $category[0]->term_id;
}
// Display category
if ( !empty($category_display) ){
if ( $useLink == true && !empty($category_link) ){
echo '<a href="'.$category_link.'">'.htmlspecialchars($category_display).'</a>';
} else {
echo ''.htmlspecialchars($category_display).'';
}
}
}
?>
カテゴリ人気記事</span></h4>
<?php
$sga_args = array(
'display_count' => 8, //5件表示する
'period' => 90,
'' => $category_slug
);
$sga_args = wp_parse_args( $sga_arg_, $sga_args );
/**
* ランキングデータ取得
*/
$ranking_data = sga_ranking_get_date( $sga_args );
/**
* ランキング作成
*/
if ( !empty( $ranking_data ) ):
echo '<ul class="wpp-list wpp-list-with-thumbnails">';
foreach ( $ranking_data as $post_id ):
?>
<li>
<a href="<?php the_permalink($post_id); ?>" title="<?php the_title_attribute($post_id); ?>">
<?php
$post_title = get_the_title($post_id);
echo get_the_post_thumbnail($post_id, 'wpp348x208', array( 'class' => 'wpp-thumbnail wpp_featured_stock ', 'alt' => $post_title,'title' => $post_title) );
?>
</a>
<span class="wpp-views"><?php echo get_post_meta($post_id, views, true); ?></span>
<a href="<?php echo get_permalink($post_id); ?>" title="<?php the_title_attribute($post_id); ?>" class="wpp-post-title" >
<?php echo get_the_title($post_id); ?>
</a>
<span class="wpp-excerpt"><?php echo get_the_excerpt_by_id($post_id, 100) ?></span>
<?php $archive_year = get_the_time( 'Y',$post_id ); ?>
<?php $archive_month = get_the_time( 'm',$post_id ); ?>
<a href="<?php echo get_month_link( $archive_year, $archive_month ); ?>" class="wpp-date"><span class="wpp-date"> <?php echo get_the_time('Y.n.j',$post_id); ?>
</span></a>
<span class="wpp-category-all"><?php the_category(' ','',$post_id) ?> </span>
</li>
<?php
endforeach;
echo '</ul>';
endif;
wp_reset_postdata();
?>
メインカテゴリを取得するために、Yoast SEO プラグイン の機能を使用するので、インストールしておく。
表示しないページを細かく設定するには ダイナミックウィジェットDynamic Widgets プラグインが便利。
過去記事アーカイブのページでは非表示するなどの設定ができる。






