• In the code below, is there a way I can add something like:

    If there is no featured image, then use “default.jpg”…?

    <?php $prevPost = get_previous_post(); $prevthumbnail = get_the_post_thumbnail($prevPost->ID, array(100,100) ); ?>

Viewing 7 replies - 1 through 7 (of 7 total)
  • Something like this?

    <?php
    if ( has_post_thumbnail()) {
     echo '<img src="post/thumbnail/path" alt="post thumb">';
    } else {
     echo '<img src="default/thumbnail/path" alt="Default Thumb">';
    }
    ?>

    Evan

    Thread Starter wpnewbie69

    (@wpnewbie69)

    This is what was in the theme. How can I rework this to include your code?

    <div id="next-prev-posts">
    			<?php $prevPost = get_previous_post(); $prevthumbnail = get_the_post_thumbnail($prevPost->ID, array(100,100) ); ?>
    			<?php $nextPost = get_next_post(); $nextthumbnail = get_the_post_thumbnail($nextPost->ID, array(100,100) ); ?>
    
    			<div class="inner">
    				<div class="nav-next">
    					<?php if ($nextPost) { ?>
    					<?php next_post_link('%link',"$nextthumbnail", false); ?>
    					<?php next_post_link('%link', '<span class="meta-nav">?</span> %title', false) ?>
    					<?php } else { echo "<p>You're at the beginning!</p>"; } ?>
    				</div>
    				<div class="nav-previous">
    					<?php if ($prevPost) { ?>
    					<?php previous_post_link('%link', '%title <span class="meta-nav">?</span>', false) ?>
    					<?php previous_post_link('%link',"$prevthumbnail", false); ?>
    					<?php } else { echo "<p>You're at the end!</p>"; } ?>
    				</div>
    			</div>
    		</div>

    Possibly something like this?

    You may need to tweak/adjust it.

    <div id="next-prev-posts">
    			<?php $prevPost = get_previous_post(); $prevthumbnail_img = get_the_post_thumbnail($prevPost->ID, array(100,100) ); ?>
    			<?php $nextPost = get_next_post(); $nextthumbnail_img = get_the_post_thumbnail($nextPost->ID, array(100,100) ); ?>
    
    			<?php $default_post_thumb = '<img src="default/thumbnail/path" alt="Default Thumb">';
    
    				/* Previous Thumbnail */
    				if ( $prevthumbnail_img() ) {
    					$prevthumbnail = $prevthumbnail_img;
    				} else {
    					$prevthumbnail = $default_post_thumb;
    				}
    
    				/* Next Thumbnail */
    				if ( $nextthumbnail_img() ) {
    				  $nextthumbnail = $nextthumbnail_img;
    				} else {
    					$nextthumbnail = $default_post_thumb;
    				}
    
    			?>	
    
    			<div class="inner">
    				<div class="nav-next">
    					<?php if ($nextPost) { ?>
    					<?php next_post_link('%link',"$nextthumbnail", false); ?>
    					<?php next_post_link('%link', '<span class="meta-nav">?</span> %title', false) ?>
    					<?php } else { echo "<p>You're at the beginning!</p>"; } ?>
    				</div>
    				<div class="nav-previous">
    					<?php if ($prevPost) { ?>
    					<?php previous_post_link('%link', '%title <span class="meta-nav">?</span>', false) ?>
    					<?php previous_post_link('%link',"$prevthumbnail", false); ?>
    					<?php } else { echo "<p>You're at the end!</p>"; } ?>
    				</div>
    			</div>
    		</div>
    Thread Starter wpnewbie69

    (@wpnewbie69)

    Thanks, Evan. Just one question, how would I format this line? I’m linking to the image from a child theme but this line is giving out an error. I’m not too good with PHP and I’m sure I’m not allowed to open a php statement when one’s already open.

    echo '<img src="<?php echo dirname( get_bloginfo('stylesheet_url') ); ?>/images/isa.jpg" alt="ISA">';

    Thread Starter wpnewbie69

    (@wpnewbie69)

    Ok, I figured out that I can concatenate it like this:

    echo '<img src="'.get_stylesheet_directory_uri().'/images/isa.jpg" alt="ISA" />';

    But now I’m getting an undefined function error on this line:

    if ( $prevthumbnail() ) {

    Thread Starter wpnewbie69

    (@wpnewbie69)

    Ok, never mind I figured that out too. It should be if ( $prevthumbnail ) {.

    Sorry I didn’t get back to you sooner. Glad you were able to figure that out on your own. Happy coding!

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘How can I add a conditional to show a default image when a post has no featured?’ is closed to new replies.