[Resolved] Advanced modification of entry meta

Home Forums Support [Resolved] Advanced modification of entry meta

Home Forums Support Advanced modification of entry meta

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #2226418
    Tim

    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&#8221; 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

    #2227054
    Leo
    Staff
    Customer Support

    Hi 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 ๐Ÿ™‚

    #2227367
    Tim

    Hi 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?

    #2227558
    David
    Staff
    Customer Support

    Hi 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’;`

    #2228117
    Tim

    Thank 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.

    #2228163
    Ying
    Staff
    Customer Support

    Hi 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 );
    #2229005
    Tim

    Excellent, that worked. Thank you.

    #2230177
    Ying
    Staff
    Customer Support

    No problem ๐Ÿ™‚

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