- This topic has 8 replies, 3 voices, and was last updated 2 years ago by David.
-
AuthorPosts
-
March 20, 2022 at 11:19 pm #2161604victor
Hi, is there a way I can disable a snippet on a specific post?
March 21, 2022 at 12:09 am #2161638Fernando Customer SupportHi 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 theID
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. 🙂
March 21, 2022 at 12:22 am #2161652victorDoes this mean creating a new snippet?
March 21, 2022 at 12:52 am #2161681Fernando Customer SupportNo, 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. 🙂
March 21, 2022 at 1:56 am #2161725victorI 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 );March 21, 2022 at 2:00 am #2161729victorPost ID: 46906
March 21, 2022 at 2:06 am #2161739Fernando Customer SupportHere 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. 🙂
March 21, 2022 at 2:15 am #2161749victorDoesn’t work.. unfortunately. Still showing the last updated stamp.
PS; I have replaced the post ids as recommended.March 21, 2022 at 4:00 am #2161854DavidStaffCustomer SupportHi 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 )
-
AuthorPosts
- You must be logged in to reply to this topic.