dsfg3241 2019-06-08 16:36
浏览 78

从相关帖子中排除特定类别

Looking to exclude specific category (ID = 100) from "Related Posts" feed at bottom of Blog pages. Extra bonus if it can also exclude from the sidebar archive (not sure if they are connected??)

I'm using WP Theme "TheFox", have asked them - not part of their theme.

I "think" it has to do in the functions.php. I have found some similar questions, and code, but have not had any luck.

I'm a complete noob for .php, so be gentle :)

I've found some other attempts, no luck. Not registering or effecting the feed.

$categories_to_exclude [ 100 ];
$first_cat  = false; 
$categories = get_the_category( $post->ID );
while ( ! empty( $categories ) && false === $first_cat ) {
 if ( in_array($categories[0]->cat_ID, $categories_to_exclude) ) {
  array_shift($categories);
 }
   else {
       $first_cat = $categories[0]->cat_ID;
   }
}
  • 写回答

2条回答 默认 最新

  • doupang4126 2019-06-08 17:25
    关注

    What I could gather from your question is that You want to ignore one category (may be more) in the related post query?

    use the following CODE (some explanation is given within the CODE in comments):

    // set the category ID (or multiple category IDs) // you want to ignore in the following array $cats_to_ignore = array( 2 ); $categories = wp_get_post_categories( get_the_ID() ); $category_in = array_diff( $categories, $cats_to_ignore ); // ignore only if we have any category left after ignoring if( count( $category_in ) == 0 ) { $category_in = $categories; } $cat_args = array( 'category__in' => $category_in, 'posts_per_page' => 4, 'orderby' => 'date', 'post__not_in' => array( get_the_ID() ) ); $cat_query = new WP_Query( $cat_args ); while ( $cat_query->have_posts() ) : $cat_query->the_post(); /* just example markup for related posts */ echo '<h2><a href="' . get_the_permalink() . '">' . get_the_title() . '</a></h2>'; endwhile; // reset $post after custom loop ends (if you need the main loop after this point) wp_reset_postdata();

    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改