• Resolved baudesign

    (@baudesign)


    Hello everyone,

    I am trying to set up a special mechanism on my home page that will show an image if a custom field is defined, that will show another image — a default one — if no custom field is defined, and finally that will show nothing if not on the home page.

    I must be doing something wrong here cause it will not work. Can someone check the code please?

    Also, i do not know the PHP trick to “display nothing”, or just “do nothing”.

    Here is the code:

    <div id="latest-post-image">
    
        <?php if (get_post_meta($post->ID, 'latest_home_img', true) && (is_home()) ) { ?>
    
        <a href="<?php printf(__ ('Permanent Link to %s', 'sandbox'), get_the_title() )?>"><img src="<?php bloginfo('stylesheet_directory'); ?>/images/latest/<?php echo get_post_meta($post->ID, "latest_home_img", $single = true); ?>" alt="<?php bloginfo('name'); ?>: Latest post" /></a>
    
    	<?php } else if (get_post_meta($post->ID, 'latest_home_img', false) && (is_home()) ) { ?>
        <img src="<?php bloginfo('stylesheet_directory'); ?>/images/latest/magpie_default.png" alt="<?php bloginfo('name'); ?>: Image ordinaire" />
    
    	<?php } else { (get_post_meta($post->ID, 'latest_home_img', false) && (!is_home()) ) ?>
    
        "Diplay noting, niet, rien du tout"
    
        <?php } ?> 
    
        </div><!-- /.latest-post-image -->
Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter baudesign

    (@baudesign)

    My problem is partly solved with this code:

    <div id="latest-post-image">
      <?php if (get_post_meta($post->ID, 'latest_home_img', true) && (is_home()) ) { ?>
    
    Show the associated image
    
      <?php } else if (!get_post_meta($post->ID, 'latest_home_img', true) && (is_home()) ) { ?>
    
    Show the default image
    
      <?php } else {  ?>
    
      <?php } ?>
    </div>

    Oddly, this works perfectly. But isn’t there suppose to be something at the end after “else”, like echo empty, or echo “nothing”, or exit or…?

    Thanks

    Patrick

    Something like:

    <?php } else {  ?>
    
     <?php } ?>

    is certainly valid, if unusual, PHP code. However, you can revamp your code like so:

    <?php if ( is_home() ) { ?>
    <div id="latest-post-image">
    <?php if ( get_post_meta($post->ID, 'latest_home_img', true) ) { ?>
    Show the associated image
    <?php } else { ?>
    Show the default image
    <?php } ?>
    </div>
    <?php } ?>

    Note how I’ve nested your if/else statement on get_post_meta() inside another if block (is_home()). Since you want to test this condition only when on the home page, it’s better to first verify this is the case, then follow through with your check on the custom field.

    Thread Starter baudesign

    (@baudesign)

    Thanks for the reply kafkaesqui.

    I have not tried your suggestion, but looking at it, it would not work.

    What I am tring to do is this:

    1. On the home page, if the latest post has an image associated to it(custom field), display that image;

    2. On the home page, if the latest post has no associated image, then display the default image (some logo, or whatever else);

    3. And if you are on another page than the home page, then, do not show any image at all.

    My code works, no problem there. But It seems strange to me (I am no PHP programmer) that there is nothing after the last “else” conditional statement:

    else { } ?>

    Should’n there be something between the brackets?

    Thanks

    Patrick

    I have not tried your suggestion, but looking at it, it would not work.

    Then you’re not reading it right. Because:

    1. On the home page, if the latest post has an image associated to it(custom field), display that image;

    2. On the home page, if the latest post has no associated image, then display the default image (some logo, or whatever else);

    3. And if you are on another page than the home page, then, do not show any image at all.

    is pretty much how the code I provided works. The logic goes:

    1. if ( is_home() ) {
    If we are on the home page, then…

    1.a. if (get_post_meta($post->ID, 'latest_home_img', true) ) {
    If the ‘latest’ post has an image associated to it, display that image (assumes “Show the associated image” echos the value for ‘latest_home_img’ as an img tag’s src attribute).

    1.b. } else {
    Display the default image when the previous if evaluates as false.

    Should’n there be something between the brackets?

    As I said before, it’s valid code, if a little unusual.

    Thread Starter baudesign

    (@baudesign)

    Sorry Kafakaesqui, you were right all the way. I had not noticed that if ( is_home ()) { was outside my main <div>. Your code looks much better than mine. 🙂 And it works perfectly well.

    Thanks again. I really appreciate.

    Patrick

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘if… elseif… else… I am lost.’ is closed to new replies.