Support » Fixing WordPress » List posts, if more than one.
List posts, if more than one.
-
I have this code that displays all the posts as a list in the same category as the post you are on.
<h2 class="bluebottom"><?php $categories = get_the_category(); foreach($categories as $category) { echo $category->name . ' '; } ?></h2> <ul> <?php global $post; $category = get_the_category($post->ID); $category = $category[0]->cat_ID; $myposts = get_posts(array('numberposts' => 99, 'category__in' => array($category), 'post__not_in' => array($post->ID),'post_status'=>'publish')); foreach($myposts as $post) : setup_postdata($post); ?> <li> <a href="<?php the_permalink(); ?>"> <?php the_title(); ?> </a> </li> <?php endforeach; ?> <?php wp_reset_query(); ?> </ul>
How would I modify this code so the <h2></h2> at the top only appears if there is more than one post in the category?
-
test to see if the sizeof categories is non-zero and only output the <h2> if it is.
Try this:
<?php global $post; $category = get_the_category( $post->ID ); $category = $category[0]; $category_id = $category->cat_ID; $myposts = get_posts( array( 'numberposts' => 99, 'category__in' => array( $category_id ), 'post__not_in' => array( $post->ID ), 'post_status' => 'publish' ) ); if ( count( $myposts > 1 ) ) : ?> <h2 class="bluebottom"> <?php echo $category->name; ?> </h2> <?php endif; foreach( $myposts as $post ) : setup_postdata($post); ?> <li> <a href="<?php the_permalink(); ?>"> <?php the_title(); ?> </a> </li> <?php endforeach; wp_reset_postdata(); ?>
JakePT, this code doesn’t seem to be working, I tried it exactly as you posted and then modified it a bit:
<?php global $post; $category = get_the_category( $post->ID ); $category = $category[0]; $category_id = $category->cat_ID; $myposts = get_posts( array( 'numberposts' => 99, 'category__in' => array( $category_id ), 'post__not_in' => array( $post->ID ), 'post_status' => 'publish' ) ); if (count($myposts > 1)) { echo 'Test'; } ?> <?php foreach( $myposts as $post ) : setup_postdata($post); ?> <li> <a href="<?php the_permalink(); ?>"> <?php the_title(); ?> </a> </li> <?php endforeach; wp_reset_postdata(); ?>
My website doesn’t crash, but the if statement doesn’t seem to be doing anything, the word “Test” shows up regardless of how many posts the category has.
Thanks
Does the rest of it work? The list of posts?
Yes everything else works. Just trying to display the title if there are 2 or more posts in the category.
Ah, whoops, I see it. Try this:
<?php global $post; $category = get_the_category( $post->ID ); $category = $category[0]; $category_id = $category->cat_ID; $myposts = get_posts( array( 'numberposts' => 99, 'category__in' => array( $category_id ), 'post__not_in' => array( $post->ID ), 'post_status' => 'publish' ) ); if (count( $myposts ) > 1 ) { echo 'Test'; } ?> <?php foreach( $myposts as $post ) : setup_postdata($post); ?> <li> <a href="<?php the_permalink(); ?>"> <?php the_title(); ?> </a> </li> <?php endforeach; wp_reset_postdata(); ?>
I changed
if (count($myposts > 1))
toif (count( $myposts ) > 1 )
This worked! I just had to change
if (count( $myposts ) > 1 )
toif (count( $myposts ) >= 1 )
because the title was not showing up when one post was listed (so categories with two posts, not including the post page you are on)Thanks!
- The topic ‘List posts, if more than one.’ is closed to new replies.