Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Other How to make PHP "if" code to display:none if a featured image is not set

  • This topic is empty.
Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #253153
    Konspaul
    Participant

    This site https://jaanavuola.com/portfolio/ is very dependent of a featured image set on posts and pages. The featured image is set in function.php file with this code

    // Add featured image on single post
    add_action( 'genesis_before_content', 'jaana_featured_image', 1 );
    function jaana_featured_image() {
    
        $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full' );
    
        ?>
    
        
    
        <?php
    
    }
    

    Everything is fine as long as the post/page HAS a featured image. But if it doesn’t then the page looks like this https://jaanavuola.com/oma-tili/ It takes the big space for the featured image and leaves it as an empty gray space.

    I’m asking your help to make a PHP if statement something like this:

    if

    /* The div which is showing a featured image */
    .featured-images-in-posts
    

    is

    /* CSS generated code when the PHP is not finding a featured image */
    background-image: url((unknown));
    

    make the

    .featured-images-in-posts {
      display: none
    }
    

    Is this possible?

    #253158
    Atelierbram
    Participant

    Would it be possible for you to edit the single.php file, or the one with the .featured-images-in-posts div, and wrap that div within a conditional tag (checking if the thumbnail is set), something like:

    <?php if(has_post_thumbnail()): ?><?php endif; ?> 
    

    Where the featured-image-in-posts div will be before the endif (can’t format this over here, sorry)

    #253173
    Konspaul
    Participant

    Thanks for your help.

    This was actually solved simply by adding an if statement to the php like so:

    <?php
    // Add featured image on single post
    add_action( 'genesis_before_content', 'jaana_featured_image', 1 );
    function jaana_featured_image() {
    
        $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full' );
    
        if ($thumb):
    
            ?>
    
              
    
        <?php
    
            else:
    
            ?>
    
                
    
            <?php
    
            endif;
    
    }
    ?>
    
    #253178
    Atelierbram
    Participant

    Using frameworks for WordPress makes these things a bit harder, for me in my mind that is, because it adds this extra layer of abstraction. So ISO editing a template file, one is filtering functions in functions.php, … a matter of preference?

    First checking if something is “set” with a conditional statement before applying some code seems to be the sensible way to prevent unnecessary output.

    #253244
    Konspaul
    Participant

    Yes. WordPress adds extra “layer”. For me this was very hard in the beginning when I just started developing WordPress themes after regular HTML/CSS pages where things are very straight forward.

    I prefer keep all my small edits in function.php file. Makes things more easy than separate code in template files. Template files works better on other scenarios.

    #253277
    Atelierbram
    Participant

    Misunderstanding; I referred to the Genesis framework (but there are more of them) you are using: do you really need it?

    #253284
    Konspaul
    Participant

    Genesis? Yes. It is best thing in WordPress. Very solid framework and child theme editing is extremely easy.

    #253291
    Atelierbram
    Participant

    Have different experiences myself, and would never recommend it to beginners in HTML/CSS/PHP. For I think then they will be learning how to use “the framework” while skipping on learning the basics, and getting in trouble later on …

    But as they say: to each their own ;\

    #255370
    braddalton
    Participant

    You could also use genesis_get_image as this is the function for featured images in Genesis.

    Then you can add a if statement, ternary or filter to control the output when the featured image is not added.

Viewing 9 posts - 1 through 9 (of 9 total)
  • The forum ‘Other’ is closed to new topics and replies.