Change:
foreach($myposts as $post) : ?>
to
foreach($myposts as $post) :
setup_postdata($post);?>
MichaelH, thanks a lot for your reply, but it doesn’t work correctly yet.
How can I exclude parent and child categories of every current category?
What do you think?
Actually I was ignoring
I have to exclude parent and child category?
because it didn’t make sense to me, so maybe someone else understands and can help you.
I will add this bit of code and info from notes:
In a sidebar, if single post, for the first category of that post, display 5 posts in that category, excluding the single post.
Also
http://wordpress.org/search/category+single+sidebar?forums=1
http://wordpress.org/support/topic/257858?replies=4
http://wordpress.org/support/topic/179138?replies=4
http://wordpress.org/support/topic/252228
<?php
if ( is_single() ) {
$cats = wp_get_post_categories($post->ID);
if ($cats) {
$first_cat = $cats[0];
$args=array(
'cat' => $first_cat, //cat__not_in wouldn't work
'post__not_in' => array($post->ID),
'showposts'=>5,
'caller_get_posts'=>1
);
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
echo 'Related Posts';
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
<?php
endwhile;
} //if ($my_query)
} //if ($cats)
wp_reset_query(); // Restore global post data stomped by the_post().
} //if (is_single())
?>
thanks a lot MichaelH!
Marco
dear Michael, sorry for my english.
Have you got a moment for me, again?
My first problem, I think, is that the structure of the categories is made of parents and many children.
Infact the code, in the sidebar, works correctly when the current post is the last children category , but doesn’t work when the post is in a parent category with some children.
And then it shows me the posts of that category and the posts of the category child. Too many posts.
And then my question is:
is there a way to exclude the children by the current category?
thanks in advance, again!
Marco
Michael, It’s Incredible! it’s working correctly now!
I matched several solutions I found in this forum and now I don’t know why it works now!
I found one of your post in a forum, and… the mistery:
<?php
global $post;
if(is_category() || is_single()){
foreach(get_the_category() as $category)
{
$current = $category->cat_ID;
$current_name = $category->cat_name;
//query_posts("cat=". $current);
$myposts = get_posts(array('category__in' => array($current)));
//$myposts = get_posts('numberposts=50&category='.$current);
//query_posts(array('category__in' => array(11)));
}
}
foreach($myposts as $post) :
setup_postdata($post);
?>
<li>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?></a>
</li>
<?php endforeach; ?>
thanks a lot for your care!
Marco
I’m using the code directly above me from ruzzino.
as seen here http://www.bpdfriends.com/section/blog/
however for some reason its showing a post there /blog/ is a parent category with no post in it… but its still choosing to display a post from the http://www.bpdfriends.com/section/blog/bipolar-blog/
both of the following work perfect (they are child categories)
http://www.bpdfriends.com/section/blog/bipolar-blog/
http://www.bpdfriends.com/section/blog/bpd-blog/
any way of altering the code to now show the post
This code MichaelH posted works great for me;
$cats = wp_get_post_categories($post->ID);
if ($cats) {
$first_cat = $cats[0];
$args=array(
'cat' => $first_cat, //cat__not_in wouldn't work
'post__not_in' => array($post->ID),
'showposts'=>5,
'caller_get_posts'=>1
);
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
echo 'Related Posts';
while ($my_query->have_posts()) : $my_query->the_post(); ?>
but how can you display the 5 previous posts to the current post date within the same category?