admin管理员组文章数量:1122846
The two functions below are designed to return a list of posts that share the same category as the current post being viewed in single.php
However, when I var_dump on $cat (should be a list of the categories assigned to the current post), I'm not getting what I'm expecting. Do I need to deserialize the array before passing it to the get_posts query?
function get_cats()
{
$post_cats= array();
$categories = get_the_category();
foreach($categories as $cat){
array_push($post_cats, $cat->cat_ID);
}
return $post_cats;
}
//get related posts by category
function ce4_get_related_by_category()
{
global $post;
$cat = implode(',',get_cats());
$catHidden=get_cat_ID('hidden');
$myqueryCurrent = new WP_Query();
$myqueryCurrent->query(array('cat' => "$cat,-$catHidden",'post__not_in' => get_option('sticky_posts')));
$totalpostcount = $myqueryCurrent->found_posts;
if($totalpostcount > 0)
{
echo "<ul>";
$myposts = get_posts(array('cat' => "$cat,-$catHidden",'numberposts' => $cb2_current_count));
foreach($myposts as $idx=>$post)
{
?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><?php the_excerpt(); ?></li>
<?php
}
echo "</ul>";
}
}
The two functions below are designed to return a list of posts that share the same category as the current post being viewed in single.php
However, when I var_dump on $cat (should be a list of the categories assigned to the current post), I'm not getting what I'm expecting. Do I need to deserialize the array before passing it to the get_posts query?
function get_cats()
{
$post_cats= array();
$categories = get_the_category();
foreach($categories as $cat){
array_push($post_cats, $cat->cat_ID);
}
return $post_cats;
}
//get related posts by category
function ce4_get_related_by_category()
{
global $post;
$cat = implode(',',get_cats());
$catHidden=get_cat_ID('hidden');
$myqueryCurrent = new WP_Query();
$myqueryCurrent->query(array('cat' => "$cat,-$catHidden",'post__not_in' => get_option('sticky_posts')));
$totalpostcount = $myqueryCurrent->found_posts;
if($totalpostcount > 0)
{
echo "<ul>";
$myposts = get_posts(array('cat' => "$cat,-$catHidden",'numberposts' => $cb2_current_count));
foreach($myposts as $idx=>$post)
{
?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><?php the_excerpt(); ?></li>
<?php
}
echo "</ul>";
}
}
Share
Improve this question
edited Jan 19, 2011 at 16:34
Scott B
asked Jan 19, 2011 at 15:19
Scott BScott B
5,69614 gold badges94 silver badges148 bronze badges
2 Answers
Reset to default 2You can use get_the_category()
It will return an array of category IDs belonging to the current post.
$post_cats= array();
$categories = get_the_category();
foreach($categories as $cat) :
array_push($post_cats, $cat->cat_id);
endforeach;
Then the $post_cats array will have a list of all the ids.
You can fetch array of IDs alone like this with wp_get_object_terms()
:
wp_get_object_terms($id, 'category', array('fields' => 'ids'))
Note that get_the_category()
caches results and getting IDs from its return might actually be more efficient than fetching IDs separately with this.
本文标签: plugin developmentHow do I obtain a list of categories assigned to the current post
版权声明:本文标题:plugin development - How do I obtain a list of categories assigned to the current post? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736306373a1932934.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论