简体   繁体   English

WordPress:显示当前类别中的其他帖子

[英]WordPress: Display Other Posts From Current Category

I have a function going that displays all posts under the same custom taxonomy called "issue". 我有一个函数可以显示在相同的自定义分类法(称为“问题”)下的所有帖子。 I need to adjust it so that it further narrows it down to also only display posts under the same category. 我需要对其进行调整,以使其进一步缩小范围,使其仅显示相同类别下的帖子。

I took a look at the WordPress get_the_category() function but didn't have much luck with that. 我看了一下WordPress的get_the_category()函数,但是运气并不好。

Here is the code: 这是代码:

<?php
$issueid = get_the_term_list( $post->ID, 'issue', '', ', ', '' );
$postslist = get_posts("numberposts=100&issue=$issueid");
 foreach ($postslist as $post) : 
 setup_postdata($post); ?>

<div class="sidebar-box">

<div class="sidebar-left">

<p><a href="<?php echo get_page_link($page->ID) ?>"><?php the_title(); ?></a></p>

<p><?php the_date(); ?></p>

</div>

<div class="sidebar-right">

<?php echo get_the_post_thumbnail($page->ID, 'thumbnail'); ?>

</div>

</div>

<?php endforeach; ?>

This will properly show the current category id: 这将正确显示当前类别ID:

<?php
$category = get_the_category(); 
echo $category[0]->cat_id;
?>

So I tried editing the first batch of code to only show posts within the current category id but it still returns everything: 因此,我尝试编辑第一批代码以仅显示当前类别ID内的帖子,但仍返回所有内容:

$category = get_the_category(); 
$categoryid = $category[0]->cat_id; 
$issueid = get_the_term_list( $post->ID, 'issue', '', ', ', '' );
$postslist = get_posts("numberposts=100&issue=$issueid&category=$categoryid");
 foreach ($postslist as $post) : 
 setup_postdata($post); ?>

This is the get_the_category function reference: http://codex.wordpress.org/Function_Reference/get_the_category 这是get_the_category函数参考: http ://codex.wordpress.org/Function_Reference/get_the_category

Any help would be greatly appreciated. 任何帮助将不胜感激。

Thanks, 谢谢,

Wade 韦德

get_the_term_list() returns an html string, not ID's of related categories. get_the_term_list()返回html字符串,而不是相关类别的ID。 So when you pass $issueid to get_posts() , you are including an html string, not an integer. 因此,当您将$issueid传递给get_posts() ,您将包含一个html字符串,而不是整数。 I believe the reason you are getting all posts returned is because WP is ignoring that query var because it isn't what its expecting. 我相信您得到所有​​帖子返回的原因是因为WP忽略了该查询var,因为它不是它的预期。

You want to use get_posts() and include the ID for 'issue' to get all posts assigned the category of 'issue'. 您要使用get_posts()并包含“问题”的ID,以获取所有分配了“问题”类别的帖子。

You want to use get_the_category() to get all categories related to a post. 您想使用get_the_category()获取与帖子相关的所有类别。

Can you clarify if you want to show all posts under the same categories as the current post which is under the category 'issue'? 您能否澄清是否要显示与当前帖子在“问题”类别下相同类别的所有帖子? Do you want to list the related posts right after the current post, or do you want to display ALL the related posts to ALL the 'issue' posts in the sidebar? 您是要在当前帖子之后立即列出相关帖子,还是要在边栏中将所有相关帖子显示为所有“问题”帖子?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM