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.
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); ?>
&开发者_Go百科lt;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:
<?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:
$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
Any help would be greatly appreciated.
Thanks,
Wade
get_the_term_list()
returns an html string, not ID's of related categories. So when you pass $issueid
to get_posts()
, you are including an html string, not an integer. 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.
You want to use get_posts()
and include the ID for 'issue' to get all posts assigned the category of 'issue'.
You want to use get_the_category()
to get all categories related to a post.
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?
精彩评论