Ask your WordPress questions! Pay money and get answers fast! (more info)

Can someone help complete my code for use on a page... WordPress

  • SOLVED

I have been trying to piece together this code from bits from the internet, but i'm stuck on the last bit, basically I want to show the thumbnail of the child theme, but if it doesn't have a thumbnail image then show the default.jpg:

Can someone help me complete the if / else part. Thank you


If child page has thumbnail then show


<a href="<?php echo get_permalink($pageChild->ID); ?>" rel="bookmark" title="<?php echo $pageChild->post_title; ?>"><?php echo get_the_post_thumbnail($pageChild->ID, 'thumbnail'); ?></a>


else


<a href="<?php echo get_permalink($pageChild->ID); ?>" rel="bookmark" title="<?php echo $pageChild->post_title; ?>"><img src="<?php bloginfo('template_directory'); ?>/images/default.jpg" alt="<?php the_title(); ?>" /></a>

Answers (3)

2013-02-18

Dbranes answers:

You could try the <strong>has_post_thumbnail()</strong> function:

http://codex.wordpress.org/Function_Reference/has_post_thumbnail

with this kind of if/else:

<?php
if(has_post_thumbnail($pageChild->ID)){
?>
<a href="<?php echo get_permalink($pageChild->ID); ?>" rel="bookmark" title="<?php echo $pageChild->post_title; ?>"><?php echo get_the_post_thumbnail($pageChild->ID, 'thumbnail'); ?></a>
<?php
}else{
?>
<a href="<?php echo get_permalink($pageChild->ID); ?>" rel="bookmark" title="<?php echo $pageChild->post_title; ?>"><img src="<?php bloginfo('template_directory'); ?>/images/default.jpg" alt="<?php the_title(); ?>" /></a>
<?php
}
?>

2013-02-18

Christianto answers:

Have you set your $pageChild object?

please try this:
<?php
if(has_post_thumbnail($pageChild->ID)){ ?>
<a href="<?php echo get_permalink($pageChild->ID); ?>" rel="bookmark" title="<?php echo $pageChild->post_title; ?>"><?php echo get_the_post_thumbnail($pageChild->ID, 'thumbnail'); ?></a>
<?php } else { ?>
<a href="<?php echo get_permalink($pageChild->ID); ?>" rel="bookmark" title="<?php echo $pageChild->post_title; ?>"><img src="<?php bloginfo('template_directory'); ?>/images/default.jpg" alt="<?php the_title(); ?>" /></a>
<?php } ?>


Christianto comments:

And 1st place goes to Dbranes ;)

2013-02-18

Arnav Joy answers:

try this


<?php

$posts_array = get_posts( 'post_parent' => get_the_ID() , 'posts_per_page' => 1 );

if( has_post_thumbnail($posts_array[0]->ID) ) {
?>
<a href="<?php echo get_permalink($posts_array[0]->ID); ?>" rel="bookmark" title="<?php echo $posts_array[0]->post_title; ?>"><?php echo get_the_post_thumbnail($posts_array[0]->ID, 'thumbnail'); ?></a>
<?php } else { ?>

<a href="<?php echo get_permalink($posts_array[0]->ID); ?>" rel="bookmark" title="<?php echo $posts_array[0]->post_title; ?>"><img src="<?php bloginfo('template_directory'); ?>/images/default.jpg" alt="<?php echo $posts_array[0]->post_title; ?>" /></a>

<?php } ?>