- This topic has 7 replies, 4 voices, and was last updated 1 year, 11 months ago by Ying.
-
AuthorPosts
-
May 20, 2022 at 3:30 am #2226418Tim
Hi,
I have a custom setup in place for the entry meta on the site shared in the “private information” section, but was just wondering if someone could please help me modify it a little further in the following two ways:
* Remove all dates (updated and published dates) for posts older than 2 years, except for posts in the “XYZ” category.
* Remove the “Leave a comment” link unless one or more comments have been made on the post.
Here’s the code I’m already using for the entry meta (located in the “Snippets” plugin settings):
add_action( ‘wp’, function() {
if ( is_single() ) {
add_filter( ‘generate_show_comments’, ‘__return_true’ );
}
} );add_filter( ‘generate_post_author_output’, ‘tu_remove_by_byline’ );
function tu_remove_by_byline() {
printf( ‘ <span class=”byline”>%1$s</span>’, // WPCS: XSS ok, sanitization ok.
sprintf( ‘<span class=”author vcard” itemtype=”https://schema.org/Person” itemscope=”itemscope” itemprop=”author”>%1$s <span class=”author-name” itemprop=”name”>%4$s</span></span>’,
__( ‘By’, ‘generatepress’ ),
esc_url( get_author_posts_url( get_the_author_meta( ‘ID’ ) ) ),
/* translators: 1: Author name */
esc_attr( sprintf( __( ‘View all posts by %s’, ‘generatepress’ ), get_the_author() ) ),
esc_html( get_the_author() )
)
);
}add_filter( ‘generate_header_entry_meta_items’, function() {
return array(
‘author’,
‘date’,
‘comments-link’,
);
} );Thank you, appreciate any help you can provide here.
Tim
May 20, 2022 at 10:25 am #2227054LeoStaffCustomer SupportHi Tim,
The newest and best method now would be to create a post meta block element:
https://docs.generatepress.com/article/block-element-post-meta-template/Then you wouldn’t need to write any functions at all.
Let me know if this helps ๐
May 20, 2022 at 10:22 pm #2227367TimHi Leo,
Thank you, that was really helpful. Didn’t even know about all of that block element post meta stuff, it’s brilliant!
That said, the only thing it didn’t solve is how to remove the post meta dates on posts that haven’t been published or updated within two years. I don’t want to show dates for older posts.
Is there a code-based solution for this?
May 21, 2022 at 5:13 am #2227558DavidStaffCustomer SupportHi there,
1. edit your post meta block element and select the Headline block that is displaying the Date. In Advanced > Additional CSS Class(es) add:
post-date
2. Add this PHP Snippet to your site.
add_filter( 'generate_dynamic_element_text', function( $text, $block ) { global $post; if ( ! empty( $block['attrs']['className'] ) && 'post-date' === $block['attrs']['className'] && strtotime( $post->post_date ) < strtotime('-730 days') ) { $text = null; } return $text; }, 10, 2 );
Notes:
a. this is where the time comparison dis set:
strtotime('-730 days')
b. you can replace$text = null;
with a different string if you wish eg.$text =
Older than 2 years’;`May 21, 2022 at 9:13 pm #2228117TimThank you David, that worked well in terms of removing the older dates.
However, the :before elements (“Last Updated:” and “Published:”) in the headline blocks remain visible and I’ve tried tinkering to make them disappear but couldn’t figure it out in the end.
Would you mind please taking a look at this for me?
Here’s a post it’s happening on as an example:
https://waclasses.siterubix.com/example-article-4/
Thank you for your time here.
May 21, 2022 at 11:53 pm #2228163YingStaffCustomer SupportHi Tim,
Try this snippet instead:
add_filter( 'generate_dynamic_element_text_output', function( $post_date, $block ) { global $post; if ( ! empty( $block['attrs']['className'] ) && 'post-date' === $block['attrs']['className'] && strtotime( $post->post_date ) < strtotime('-10 days') ) { $post_date = null; } return $post_date; }, 10, 2 );
May 22, 2022 at 8:31 pm #2229005TimExcellent, that worked. Thank you.
May 23, 2022 at 5:48 pm #2230177YingStaffCustomer SupportNo problem ๐
-
AuthorPosts
- You must be logged in to reply to this topic.