• I have template where a header-image is inserted via (style=background-image) based on “has-image” (“image” here means a post/page thumbnail) or “no-image”.
    But now I need anoter if condition: If it’s a single post it should take one particular image which I want to put there just hard coded.
    But this whole code is too complicated for me, so I don’t know how to realize this if-condtion.

    Here is the code part that inserts the background-image. And here I need to add the “is_singular”-condition:

    
    $style = "";
                    if ( $item['image'] ) {
                        $style = ' style="background-image: url(\''.esc_url( $item['image'] ).'\');" ';
                    }
    

    I want to add a hard-coded background image if its a single post. I already tried a code but it didn’t work…that’s just too complicated for me…here is what I tried:

    
    $style = "";
                    if ( $item['image'] ) {
                        $style = ' style="background-image: url(\''.esc_url( $item['image'] ).'\');" ';
                    }
                    if (is_single()) {style="background-image:url(http://www.mydomain.com/picture.jpg);
    

    Maybe I have a typo or maybe the whole code or code position is wrong??

    And here is the whole code-part from the template. It is a header-image area:

    
    <section id="page-header-cover" class="<?php echo esc_attr(  join( ' ', $classes ) ); ?>" >
            <div class="swiper-container" data-autoplay="0">
                <div class="swiper-wrapper">
                    <?php
                    $style = "";
                    if ( $item['image'] ) {
                        $style = ' style="background-image: url(\''.esc_url( $item['image'] ).'\');" ';
                    }
    
                    $html = '<div class="swiper-slide slide-align-'.esc_attr( $item['position'] ).'"'.$style.'>';
    
                    $style  = '';
                    if  ( $item['pd_top'] != '' ) {
                        $style .='padding-top: '.floatval( $item['pd_top'] ).'%; ';
                    }
                    if  ( $item['pd_bottom'] != '' ) {
                        $style .='padding-bottom: '.floatval( $item['pd_bottom'] ).'%; ';
                    }
                    if ( $style != '' ) {
                        $style = ' style="'.$style.'" ';
                    }
                    $html .= '<div class="swiper-slide-intro">';
                    $html .= '<div class="swiper-intro-inner"'.$style.'>';
                    if ( $item['title'] ) {
                        $html .= '<h2 class="swiper-slide-heading">'.wp_kses_post( $item['title'] ).'</h2>';
                    }
                    if ( $item['desc'] ) {
                        $html .= '<div class="swiper-slide-desc">'.apply_filters( 'screenr_content_text', $item['desc'] ).'</div>';
                    }
    
                    $html .= '</div>';
                    $html .= '</div>';
                    $html .= '<div class="overlay"></div>';
                    $html .= '</div>';
    
                    echo $html;
                    ?>
                </div>
            </div>
        </section>
    
    • This topic was modified 9 years, 7 months ago by Fantasmo.
    • This topic was modified 9 years, 7 months ago by Fantasmo.
    • This topic was modified 9 years, 7 months ago by Fantasmo.

The topic ‘How to check is_single() in this PHP-Code’ is closed to new replies.