Archived topic
Disable snippet on a specific post
8 replies · Started by victor on March 20, 2022
Hi, is there a way I can disable a snippet on a specific post?
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. :)
Does this mean creating a new snippet?
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. :)
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 );
Post ID: 46906
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. :)
Doesn’t work.. unfortunately. Still showing the last updated stamp.
PS; I have replaced the post ids as recommended.
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 )