Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Hy I have this code:

    <?php 
    $id_film_array = get_post_meta(get_the_ID(),"info_film", true);
    $id_film = $id_film_array[0];
    $titlu_film = get_the_title($id_film);
?>
<?php 
    $the_query = new WP_Query( array( 'p' => $id_film) );
    while ( $the_query->have_posts() ) : $the_query->the_post();
?>


<a href="<?php echo get_permalink($id_film); ?>"><?php echo $titlu_film; ?></a>


<?php 
   endwhile;
   // Reset Post Data
   wp_reset_postdata();
?>

I recive an error if i do not have anithing select. How can i fix this.

For example i do not want to select nothing for some post, in the page i do not want to retrive nothing.

I use Custom Field, Relationship

share|improve this question
    
I want to display : No movie data..., if i don't select –  Batman Oct 21 '12 at 21:26
add comment

1 Answer

Enclose the while loop with a conditional:

$the_query = new WP_Query( array( 'p' => $id_film) );
if ( $the_query->have_posts() )
{
     while ( $the_query->have_posts() ):
        $the_query->the_post();
        // regular output
     endwhile;
     wp_reset_postdata();
}
else
{
    echo 'No movie data';
}
share|improve this answer
    
can you give more detail please :) –  Batman Oct 21 '12 at 21:35
    
where do i put that code? –  Batman Oct 21 '12 at 21:54
2  
@CopilFlavius I have extended the example. Another suggestion: Find a good PHP tutorial, and take the time to learn it. Once you are able to split a problem into the smallest logical parts that the language can express you will be much faster. :) –  toscho Oct 21 '12 at 21:59
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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