
吊橋と鶏
検索サイトから飛んできて、ある記事を表示した時に、その記事と同じカテゴリーの最新記事のリストを表示するには?
まず、PHP Code Widget プラグインをインストールするとサイドバーのウィジェットにPHPコードが使えるようになる。
そこに次のコードを書き込む。
<h4 class="widgettitle"><span class="side-title-inner">
<?php
// SHOW YOAST PRIMARY CATEGORY, OR FIRST CATEGORY
$category = get_the_category();
$useCatLink = 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 );
$now_id = $category[0]->term_id;
} else {
// Yoast Primary category
$category_display = $term->name;
$category_link = get_category_link( $term->term_id );
$now_id = $term->term_id;
}
}
else {
// Default, display the first category in WP's list of assigned categories
$category_display = $category[0]->name;
$category_link = get_category_link( $category[0]->term_id );
$now_id = $category[0]->term_id;
}
// Display category
if ( !empty($category_display) ){
if ( $useCatLink == true && !empty($category_link) ){
echo '<a href="'.$category_link.'">'.htmlspecialchars($category_display).'</a>';
} else {
echo ''.htmlspecialchars($category_display).'';
}
}
}
?>
カテゴリ最新記事</span></h4>
<ul class="wpp-list wpp-list-with-thumbnails">
<?php
// 同じカテゴリから記事を10件呼び出す
$args = array(
'post__not_in' => array($post -> ID), // 今読んでいる記事を除く
'posts_per_page'=> 8,
'cat' => $now_id,
);
$query = new WP_Query($args);
if( $query -> have_posts() ): while ($query -> have_posts()) : $query -> the_post();
?>
<li>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php
if( has_post_thumbnail() ) {
$post_title = get_the_title();
the_post_thumbnail( 'wpp348x208', array( 'class' => 'wpp-thumbnail wpp_featured_stock ', 'alt' => $post_title,'title' => $post_title) );
} else {
echo catch_that_image_wpp348x208_size();
}
?>
</a>
<span class="wpp-views"><?php if(function_exists('the_views')) { the_views(); } ?></span>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" class="wpp-post-title" >
<?php the_title(); ?>
</a>
<span class="wpp-excerpt"><?php echo get_content_excerpt( get_the_content(), 100) ?></span>
<?php $archive_year = get_the_time( 'Y' ); ?>
<?php $archive_month = get_the_time( 'm' ); ?>
<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(' ') ?> </span>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
<?php wp_reset_query(); ?>
これで、Wordpress Popular Post と同じデザインでサイドバーに表示されるようになる。
メインカテゴリを取得するために、Yoast SEO プラグイン の機能を使用するので、インストールしておく。
表示しないページを細かく設定するには ダイナミックウィジェット Dynamic Widgets プラグインが便利。過去記事アーカイブのページでは非表示するなどの設定ができる。
全カテゴリーの新着記事を表示するには次のコードを PHP Code Widget に放り込む。
<ul class="wpp-list wpp-list-with-thumbnails">
<?php
// 同じカテゴリから記事を10件呼び出す
$args = array(
'post__not_in' => array($post -> ID), // 今読んでいる記事を除く
'posts_per_page'=> 8,
);
$query = new WP_Query($args);
if( $query -> have_posts() ): while ($query -> have_posts()) : $query -> the_post();
?>
<li>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php
if( has_post_thumbnail() ) {
$post_title = get_the_title();
the_post_thumbnail( 'wpp348x208', array( 'class' => 'wpp-thumbnail wpp_featured_stock ', 'alt' => $post_title,'title' => $post_title) );
} else {
echo ch_that_image_wpp348x208_size();
}
?>
</a>
<span class="wpp-views"><?php if(function_exists('the_views')) { the_views(); } ?></span>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" class="wpp-post-title" >
<?php the_title(); ?>
</a>
<span class="wpp-excerpt"><?php echo get_content_excerpt( get_the_content(), 100) ?></span>
<?php $archive_year = get_the_time( 'Y' ); ?>
<?php $archive_month = get_the_time( 'm' ); ?>
<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(' ') ?> </span>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
<?php wp_reset_query(); ?>




