1

I am new to php. I want to use a certain div "case" in the below code if there is one post here . If there is more than one I want to use another div "case2"

<php>
 $connected = new WP_Query( array(
  'connected_type' => 'posts_to_pages',
  'connected_items' => get_queried_object(),
  'nopaging' => true,
) );

if ( $connected->have_posts() ) :
?>
   <?php while ( $connected->have_posts () ) : $connected->the_post(); ?>
    <div class="case"><h5><?php the_title(); ?></h5><?php the_excerpt(); ?></div>
    <?php endwhile; ?>

<?php 
// Prevent weirdness
wp_reset_postdata();

endif;
</php>
5

Try this to get the count of posts:

<?php

 $connected = new WP_Query( array(
  'connected_type' => 'posts_to_pages',
  'connected_items' => get_queried_object(),
  'nopaging' => true,
) );

echo "<h2>Found: $connected->found_posts</h2>";

?>

You could write a variable like:

$the_count = $connected->found_posts;

Then test with an if

if ( ( $connected->have_posts() ) && ( $the_count > 1 ) ) :
1
  • Pat J - you're absolutely right with the edit! Jun 20 '14 at 20:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.