• Resolved bluedrag

    (@bluedrag)


    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?

Viewing 8 replies - 1 through 8 (of 8 total)
  • Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    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(); ?>

    Thread Starter bluedrag

    (@bluedrag)

    Thread Starter bluedrag

    (@bluedrag)

    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?

    Thread Starter bluedrag

    (@bluedrag)

    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)) to if (count( $myposts ) > 1 )

    Thread Starter bluedrag

    (@bluedrag)

    This worked! I just had to change if (count( $myposts ) > 1 ) to if (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!

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘List posts, if more than one.’ is closed to new replies.