0

I'd like to show different post thumbnails in archive.php and category.php like this:

  • user logged out & post has tag "private" -> shows a "default-private" image
  • user logged in & post has tag "private" -> show the actual featured image attached to the post
  • no matter the logged in status and all other posts with no "private" tag, if no image set -> show "default" image

This is what I've come up with so far:

<?php if (!is_user_logged_in() && has_tag('private')) { ?>

    <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
        <img src="<?php bloginfo('template_directory'); ?>/img/default-login.jpg" alt="<?php the_title(); ?>" />
    </a>

    <?php } else {                  

         // check if the post has a Post Thumbnail assigned to it.
                if ( has_post_thumbnail() ) {
                    the_post_thumbnail('news-thumb', 
                       array('class' => 'news-thumb'));
                } else { ?>
        <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"> 
        <img src="<?php bloginfo('template_directory'); ?>/img/default.jpg" alt="<?php the_title(); ?>" />
                </a>
<?php } ?>

This is not working.

1

Hey you have to use the below logic to handle the above conditions, it will work.

if (!is_user_logged_in() && has_tag('private')) { ?>

    <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
        <img src="<?php bloginfo('template_directory'); ?>/img/default-login.jpg" alt="<?php the_title(); ?>" />
    </a>

<?php } else if( is_user_logged_in() && has_tag('private') ) {                  
    // show the actual featured image
    // check if the post has a Post Thumbnail assigned to it.
    if ( has_post_thumbnail() ) {
        the_post_thumbnail('news-thumb', 
        array('class' => 'news-thumb'));
    }
    //if you want to show the default image if featured image is not attached you can add here in else 
}else{
    ?>
         <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
            <img src="<?php bloginfo('template_directory'); ?>/img/default.jpg" alt="<?php the_title(); ?>" />
        </a>

    <?php
}
| improve this answer | |
  • Thanks! That works almost perfectly. Still doesn't address the 3rd condition: if user is logged in or not logged in, no matter what the tag. I wanted to add that to the second statement but there's again something wrong with the syntaxis: – Phantasmix Jun 4 at 15:32
  • else if( is_user_logged_in() && has_tag('private')) || (is_user_logged_in() && !has_tag() ) { – Phantasmix Jun 4 at 15:33
  • I have given you logic how you should write it. You can change the logic according to your scenario. – Baikare Sandeep Jun 6 at 2:25
  • I did. I came up with the final statement. Thank you – Phantasmix Jun 6 at 2:31
  • Great to know that 👍 – Baikare Sandeep Jun 6 at 2:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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