[Support request] Disable snippet on a specific post

Home Forums Support [Support request] Disable snippet on a specific post

Home Forums Support Disable snippet on a specific post

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #2161604
    victor

    Hi, is there a way I can disable a snippet on a specific post?

    #2161638
    Fernando
    Customer Support

    Hi Victor,

    Yes, this is possible through a conditional statement. Specifically, through the use of is_single().

    For instance:

    if( is_single( 2578 ) ) {
    
    /* your code/logic */
    
    }

    For multiple posts:

    if( is_single( array ( 2578, 1234, 1235 ) ) ) {
    
    /* your code/logic */
    
    }

    Kindly replace 2578 and the other numbers with the ID of your specific Post/Posts.

    Reference: https://developer.wordpress.org/reference/functions/is_single/

    How to get a Post’s ID: https://pagely.com/blog/find-post-id-wordpress/#:~:text=You%20can%20also%20find%20the,%E2%80%9D%20and%20the%20%E2%80%9C%26.%E2%80%9D

    Hope this helps! If further assistance is needed, feel free to reach out. 🙂

    #2161652
    victor

    Does this mean creating a new snippet?

    #2161681
    Fernando
    Customer Support

    No, it doesn’t. You can simply edit your current snippet.

    Perhaps you’re not referring to a PHP snippet? Is this CSS or JS? If it is either CSS or JS, then the steps I provided above wouldn’t work.

    Moreover, if you prefer, you can provide the snippet and I’ll see I can assist you? Kindly make it a Code block if you would like to attach the code: https://share.getcloudapp.com/E0ugqenw

    Hope to hear from you soon. 🙂

    #2161725
    victor

    I would like to disable the last update time snippet on a certain post… This one :

    add_filter( ‘generate_post_date_output’, function( $output, $time_string ) {
    $time_string = ‘<time class=”entry-date published” datetime=”%1$s” itemprop=”datePublished”>%2$s</time>’;

    if ( get_the_time( ‘U’ ) !== get_the_modified_time( ‘U’ ) ) {
    $time_string = ‘<time class=”entry-date updated-date” datetime=”%3$s” itemprop=”dateModified”>Last Updated On %4$s</time>’;
    }

    $time_string = sprintf( $time_string,
    esc_attr( get_the_date( ‘c’ ) ),
    esc_html( get_the_date() ),
    esc_attr( get_the_modified_date( ‘c’ ) ),
    esc_html( get_the_modified_date() )
    );

    return sprintf( ‘<span class=”posted-on”>%s</span> ‘,
    $time_string
    );
    }, 10, 2 );

    #2161729
    victor

    Post ID: 46906

    #2161739
    Fernando
    Customer Support

    Here is a code you may try to apply your code to a single post:

    add_filter( 'generate_post_date_output', function( $output, $time_string ) {
    if(is_single(123)){
    $time_string = '<time class="entry-date published" datetime="%1$s" itemprop="datePublished">%2$s</time>';
    
    if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) {
    $time_string = '<time class="entry-date updated-date" datetime="%3$s" itemprop="dateModified">Last Updated On %4$s</time>';
    }
    
    $time_string = sprintf( $time_string,
    esc_attr( get_the_date( 'c' ) ),
    esc_html( get_the_date() ),
    esc_attr( get_the_modified_date( 'c' ) ),
    esc_html( get_the_modified_date() )
    );
    
    return sprintf( '<span class="posted-on">%s</span>',
    $time_string
    ); } else { return $time_string; }
    }, 10, 2 );

    For multiple posts:

    add_filter( 'generate_post_date_output', function( $output, $time_string ) {
    if(is_single( array( 123, 124, 125 ) ) ){
    $time_string = '<time class="entry-date published" datetime="%1$s" itemprop="datePublished">%2$s</time>';
    
    if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) {
    $time_string = '<time class="entry-date updated-date" datetime="%3$s" itemprop="dateModified">Last Updated On %4$s</time>';
    }
    
    $time_string = sprintf( $time_string,
    esc_attr( get_the_date( 'c' ) ),
    esc_html( get_the_date() ),
    esc_attr( get_the_modified_date( 'c' ) ),
    esc_html( get_the_modified_date() )
    );
    
    return sprintf( '<span class="posted-on">%s</span>',
    $time_string
    ); } else { return $time_string; }
    }, 10, 2 );

    As mentioned kindly replace 123, 124, 125 with the specific ID of the posts.

    Hope this clarifies. Kindly let us know how it goes. 🙂

    #2161749
    victor

    Doesn’t work.. unfortunately. Still showing the last updated stamp.
    PS; I have replaced the post ids as recommended.

    #2161854
    David
    Staff
    Customer Support

    Hi there,

    try this:

    add_filter( 'generate_post_date_output', function( $output, $time_string ) {
        if ( is_single( array( 123, 124, 125 ) ) ){
            return $time_string;
        }
        $time_string = '<time class=”entry-date published” datetime=”%1$s” itemprop=”datePublished”>%2$s</time>';
        
        if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) {
            $time_string = '<time class=”entry-date updated-date” datetime=”%3$s” itemprop=”dateModified”>Last Updated On %4$s</time>';
        }
        
        $time_string = sprintf( $time_string,
        esc_attr( get_the_date( 'c' ) ),
        esc_html( get_the_date() ),
        esc_attr( get_the_modified_date( 'c' ) ),
        esc_html( get_the_modified_date() )
        );
        
        return sprintf( '<span class=”posted-on”>%s</span> ',
        $time_string
        );
    }, 10, 2 );

    Make sure to update the array of IDs here:

    array( 123, 124, 125 )

Viewing 9 posts - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.