theme development - Is_single not working properly in genesis

admin2025-06-02  1

I am currently creating a child theme for my website which uses Genesis as a parent theme. Now I am making single pages as per my choosing. So, I am removing the entry header from within HTML main and posting it after entry-header. my code looks like this.

remove_action( 'genesis_entry_header', 'genesis_do_post_title' );
remove_action( 'genesis_entry_header', 'genesis_post_info', 12 );
add_action( 'genesis_after_header', 'gp_page_header');

function gp_page_header(){
    $image = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'single-post-thumbnail' );
    ?>
    <div class="post-hero" style="background-repeat: no-repeat;background-size:cover;background-image: -webkit-radial-gradient(left top, circle cover, rgba(100, 66, 255, 0.9) 15%, rgba(0, 108, 255, 0.9) 50%, rgba(12, 180, 206, 0.9) 85%), url('<?php echo $image[0]; ?>')">
        <div class="wrap">
        <?php
        the_title( '<h1 class="entry-title" itemprop="headline">', '</h1>' ); 
        genesis_post_info();
        ?>

        </div>
    </div>
    <?php
 }

If didn't want to create this style for homepage and should be only applied to single pages. Here is what i am doing now.

remove_action( 'genesis_entry_header', 'genesis_do_post_title' );
remove_action( 'genesis_entry_header', 'genesis_post_info', 12 );
if(is_single()) {
add_action( 'genesis_after_header', 'gp_page_header');

function gp_page_header(){
    $image = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'single-post-thumbnail' );
    ?>
    <div class="post-hero" style="background-repeat: no-repeat;background-size:cover;background-image: -webkit-radial-gradient(left top, circle cover, rgba(100, 66, 255, 0.9) 15%, rgba(0, 108, 255, 0.9) 50%, rgba(12, 180, 206, 0.9) 85%), url('<?php echo $image[0]; ?>')">
        <div class="wrap">
        <?php
        the_title( '<h1 class="entry-title" itemprop="headline">', '</h1>' ); 
        genesis_post_info();
        ?>

        </div>
    </div>
    <?php
 }
}

But this way it doesn't work. If I use a conditional tag, it isn't working. I am confused at the moment. Also, I am using front-page.php as the template for home. Any help will be appreciated.

I am currently creating a child theme for my website which uses Genesis as a parent theme. Now I am making single pages as per my choosing. So, I am removing the entry header from within HTML main and posting it after entry-header. my code looks like this.

remove_action( 'genesis_entry_header', 'genesis_do_post_title' );
remove_action( 'genesis_entry_header', 'genesis_post_info', 12 );
add_action( 'genesis_after_header', 'gp_page_header');

function gp_page_header(){
    $image = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'single-post-thumbnail' );
    ?>
    <div class="post-hero" style="background-repeat: no-repeat;background-size:cover;background-image: -webkit-radial-gradient(left top, circle cover, rgba(100, 66, 255, 0.9) 15%, rgba(0, 108, 255, 0.9) 50%, rgba(12, 180, 206, 0.9) 85%), url('<?php echo $image[0]; ?>')">
        <div class="wrap">
        <?php
        the_title( '<h1 class="entry-title" itemprop="headline">', '</h1>' ); 
        genesis_post_info();
        ?>

        </div>
    </div>
    <?php
 }

If didn't want to create this style for homepage and should be only applied to single pages. Here is what i am doing now.

remove_action( 'genesis_entry_header', 'genesis_do_post_title' );
remove_action( 'genesis_entry_header', 'genesis_post_info', 12 );
if(is_single()) {
add_action( 'genesis_after_header', 'gp_page_header');

function gp_page_header(){
    $image = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'single-post-thumbnail' );
    ?>
    <div class="post-hero" style="background-repeat: no-repeat;background-size:cover;background-image: -webkit-radial-gradient(left top, circle cover, rgba(100, 66, 255, 0.9) 15%, rgba(0, 108, 255, 0.9) 50%, rgba(12, 180, 206, 0.9) 85%), url('<?php echo $image[0]; ?>')">
        <div class="wrap">
        <?php
        the_title( '<h1 class="entry-title" itemprop="headline">', '</h1>' ); 
        genesis_post_info();
        ?>

        </div>
    </div>
    <?php
 }
}

But this way it doesn't work. If I use a conditional tag, it isn't working. I am confused at the moment. Also, I am using front-page.php as the template for home. Any help will be appreciated.

Share Improve this question edited Mar 8, 2019 at 11:26 Gufran Hasan 6918 silver badges20 bronze badges asked Mar 8, 2019 at 5:50 Raashid DinRaashid Din 2182 silver badges19 bronze badges 1
  • 1 Your is_single() should be in the function - function gp_page_header() { if ( ! is_single() ) { return; } ... } – Sally CJ Commented Mar 8, 2019 at 6:20
Add a comment  | 

1 Answer 1

Reset to default 0

Please put your action add_action( 'genesis_after_header', 'gp_page_header'); in if condition not whole code.

if(is_single()){
  add_action( 'genesis_after_header', 'gp_page_header');
}

Your updated code:

if(is_single()) {
  add_action( 'genesis_after_header', 'gp_page_header');
}

function gp_page_header(){
    $image = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'single-post-thumbnail' );
    ?>
    <div class="post-hero" style="background-repeat: no-repeat;background-size:cover;background-image: -webkit-radial-gradient(left top, circle cover, rgba(100, 66, 255, 0.9) 15%, rgba(0, 108, 255, 0.9) 50%, rgba(12, 180, 206, 0.9) 85%), url('<?php echo $image[0]; ?>')">
        <div class="wrap">
        <?php
        the_title( '<h1 class="entry-title" itemprop="headline">', '</h1>' ); 
        genesis_post_info();
        ?>

        </div>
    </div>
    <?php
 }
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748830372a314078.html

最新回复(0)