0

I've got a wordpress theme with a set of custom fields. One of these is named "author".

On single.php I've got a div which show the other posts with the same custom field value.

I would like to display this div only if exists other posts with the same custom field value, else I would like to display nothing.

Thanks for your help!!

This is my actual code:

<?php 

                        $myquery = array(
                        'meta_key' => 'autore',
                        'meta_value' => $autore,
                        'showposts' => 2,
                        'post__not_in' => array($post->ID)
                        );

                        if ( $myquery->have_posts() ) : ?>

                        <div class="related">

                        <h3>Altre di <?php the_field('autore'); ?></h3>

                        <ul>

                        <?php while ( $your_query->have_posts() ) : $your_query->the_post(); ?>

                                <?php        
                                echo '<li>'; ?>

                                <?php
                                $fotorel = get_field('foto_homepage');
                                list($width, $height) = getimagesize("$fotorel");
                                $relheight = $height / 2;
                                ?>

                                <div class="related-foto" style="background:url(<?php the_field('foto_homepage'); ?>) no-repeat center center; height:<?php echo $relheight.'px' ?>"></div>
                                <a href="<?php the_permalink() ?>"><?php the_title(); ?></a>


                               <?php echo '</li>';?>

                               <?php endwhile; ?>

                              <?php else : // What to do if there are no posts from that author

                              endif;?>

                                        </ul>

        </div>



                        <?php wp_reset_query(); ?>
Charles
  • 50,943
  • 13
  • 104
  • 142
Luca Frank Guarini
  • 1,193
  • 5
  • 13
  • 26

2 Answers2

0

Here an example:

http://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query

Using the condition <?php if ($pageposts): ?> you can print your div or not.

Curlas
  • 879
  • 5
  • 14
  • I've tried to add the condition but doesn't work. Now my div is not visible… I would like to display my div only if exists other posts with the same value of the current for the custom field "AUTHOR". – Luca Frank Guarini Jun 07 '12 at 17:00
0

I'm not sure how you are querying for the posts in the custom fields but the $wp_query has built in conditionals for handling queries that don't return posts.

Updated code sample:

$args = array(
         'meta_key' => 'autore',
         'meta_value' => $autore,
         'showposts' => 2,
         'post__not_in' => array($post->ID)
        );
 $your_query = new WP_Query( $args );


if ( $your_query->have_posts() ) : ?>

  <div id="your-div">

while ( $your_query->have_posts() ) : $your_query->the_post();

// Do stuff 

endwhile;

  else : // What to do if there are no posts from that author

endif;
Community
  • 1
  • 1
Chris_O
  • 3,429
  • 1
  • 22
  • 28
  • I've used your answer, you can see the code in my question. Now i get this error: Fatal error: Call to a member function have_posts() on a non-object – Luca Frank Guarini Jun 07 '12 at 18:33