如何按类别显示相关帖子
-
-
[如何显示相同类别的相关帖子?](http://wordpress.stackexchange.com/questions/30039/how-to-display-related-posts-from-same-category)的可能重复项possible duplicate of [How to display related posts from same category?](http://wordpress.stackexchange.com/questions/30039/how-to-display-related-posts-from-same-category)
- 0
- 2012-02-05
- kaiser
-
2 个回答
- 投票数
-
- 2012-02-05
问题已经被提出,答案也已经发布了,
在想要显示相关帖子的循环之后,在single.php中添加此代码,
<?php $related = get_posts( array( 'category__in' => wp_get_post_categories($post->ID), 'numberposts' => 5, 'post__not_in' => array($post->ID) ) ); if( $related ) foreach( $related as $post ) { setup_postdata($post); ?> <ul> <li> <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a> <?php the_content('Read the rest of this entry »'); ?> </li> </ul> <?php } wp_reset_postdata(); ?>
它将显示同一类别的相关文章以及文章摘录和标题, 但是,如果希望此代码仅显示相关帖子的标题,则删除此行,
<?php the_content('Read the rest of this entry »'); ?>
The question has already been asked and the answer has been posted too,
How to display related posts from same category?
Add this code inside your single.php after a loop wherever you want to show related post,
<?php $related = get_posts( array( 'category__in' => wp_get_post_categories($post->ID), 'numberposts' => 5, 'post__not_in' => array($post->ID) ) ); if( $related ) foreach( $related as $post ) { setup_postdata($post); ?> <ul> <li> <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a> <?php the_content('Read the rest of this entry »'); ?> </li> </ul> <?php } wp_reset_postdata(); ?>
It will display related post from the same category with the post excerpt and title , however if want this code to display just the title of related post then remove this line,
<?php the_content('Read the rest of this entry »'); ?>
-
抱歉,我在wordpress和PHP中是菜鸟.如果您不介意,您能告诉我如何将代码放入single.php吗?sorry I am noob in wordpress and PHP.If yu dont mind, could yu tell me how to put that code in my single.php??
- 0
- 2012-02-05
- Felix
-
再次阅读我的答案,我添加了更多详细信息(已测试)read my answer again i have added few more details (TESTED)
- 1
- 2012-02-05
- Sufiyan Ghori
-
您可以(也许)以重复形式结束投票吗?You can (maybe) close vote as dublicate?
- 0
- 2012-02-05
- kaiser
-
@Xufyan该代码向我显示以下错误,当我在注释致命错误之后使用它时:调用未定义的函数odd_title()@Xufyan that code shows me ethe following error when i used it after the comment Fatal error: Call to undefined function odd_title()
- 0
- 2012-02-05
- Felix
-
抱歉,在上面的代码中将" ODD"替换为"the"sorry, replace 'ODD' with 'the' in the above code
- 1
- 2012-02-05
- Sufiyan Ghori
-
@Xufyan如果我在制作错误行我什么都没有,我只得到五颗没有项目或标题的子弹@Xufyan If i yake that error making line I get no post i just get five bullets with no post or title
- 0
- 2012-02-05
- Felix
-
错误已从代码中删除,现在可以正常运行(已测试),请从我的答案中复制修改后的代码the error is removed from the code and now it is working perfectly fine (Tested), copy the modified code from my answer
- 1
- 2012-02-05
- Sufiyan Ghori
-
@Xufyan但是现在只显示带有永久链接的帖子标题:\ 有小费吗??@Xufyan But now it shows only the post title with perma link :\ any tips??
- 0
- 2012-02-05
- Felix
-
这意味着您已经删除了这行代码,, 将其添加回原处it means you have removed this line of code, , add it back where it was
- 1
- 2012-02-05
- Sufiyan Ghori
-
@Xufyan不,我不脱线,再次尝试仍然显示带有永久链接的标题.@Xufyan no I dint take off the line.Tried again still it shows me the title with perma link.
- 0
- 2012-02-05
- Felix
-
让我们[继续聊天中的讨论](http://chat.stackexchange.com/rooms/2392/discussion-between-felix-and-xufyan)let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/2392/discussion-between-felix-and-xufyan)
- 0
- 2012-02-05
- Felix
-
这比您链接的答案更好This is way better than the answer to which you linked
- 0
- 2014-09-04
- Claudiu Creanga
-
- 2018-07-02
这是另一个干净且非常灵活的选项:
将此代码放入您的functions.php文件中
function example_cats_related_post() { $post_id = get_the_ID(); $cat_ids = array(); $categories = get_the_category( $post_id ); if(!empty($categories) && is_wp_error($categories)): foreach ($categories as $category): array_push($cat_ids, $category->term_id); endforeach; endif; $current_post_type = get_post_type($post_id); $query_args = array( 'category__in' => $cat_ids, 'post_type' => $current_post_type, 'post_not_in' => array($post_id), 'posts_per_page' => '3' ); $related_cats_post = new WP_Query( $query_args ); if($related_cats_post->have_posts()): while($related_cats_post->have_posts()): $related_cats_post->the_post(); ?> <ul> <li> <a href="<?php the_permalink(); ?>"> <?php the_title(); ?> </a> <?php the_content(); ?> </li> </ul> <?php endwhile; // Restore original Post Data wp_reset_postdata(); endif; }
现在,您可以使用以下方法在站点中的任何地方简单地调用该函数:
<?php example_cats_related_post() ?>
您可能需要删除列表元素或根据需要设置其样式.
Here another clean and very flexible option:
Put this code in your functions.php file
function example_cats_related_post() { $post_id = get_the_ID(); $cat_ids = array(); $categories = get_the_category( $post_id ); if(!empty($categories) && is_wp_error($categories)): foreach ($categories as $category): array_push($cat_ids, $category->term_id); endforeach; endif; $current_post_type = get_post_type($post_id); $query_args = array( 'category__in' => $cat_ids, 'post_type' => $current_post_type, 'post_not_in' => array($post_id), 'posts_per_page' => '3' ); $related_cats_post = new WP_Query( $query_args ); if($related_cats_post->have_posts()): while($related_cats_post->have_posts()): $related_cats_post->the_post(); ?> <ul> <li> <a href="<?php the_permalink(); ?>"> <?php the_title(); ?> </a> <?php the_content(); ?> </li> </ul> <?php endwhile; // Restore original Post Data wp_reset_postdata(); endif; }
Now you can simply call the function anywhere in your site using:
<?php example_cats_related_post() ?>
You may want to remove the list elements or style them as per your need.
在我的画廊网站上,我想显示当前图片下的其他图片(单篇文章).我看到了更多代码,但我要求指定类别,但我不想在代码中手动指定类别,我希望代码本身获取类别ID或名称.如果我获得完整的帖子,对我来说会容易得多而不是帖子标题,以便我可以将其显示在首页和类别中