[Resolved] Last Updated Date on a Specific Category

Home Forums Support [Resolved] Last Updated Date on a Specific Category

Home Forums Support Last Updated Date on a Specific Category

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #843808
    Chantal

    Hi there,

    I am using the following function which works great, the only thing is that it applies this to ALL posts. I would like it to apply to a SPECIFIC category only. Could you amend the code for me please? The Category ID is 1139, or is it better to use the slug?

    Many thanks.

    GeneratePress 2.2.2
    GP Premium 1.7.8
    #843871
    Leo
    Staff
    Customer Support

    Hi there,

    Is this what you are using currently?
    https://docs.generatepress.com/article/show-the-updated-post-date/

    If so I think you need to add the category class in the body tag. Maybe something like this:
    https://philipnewcomer.net/2012/10/add-post-categories-to-the-body-class-in-wordpress/

    Then we should be able to adjust the CSS if you can link/guide me to an example post.

    Let me know 🙂

    #843893
    Chantal

    Hi Leo,

    It would help if I posted the function! Doh!

    function show_last_modified_date( $content ) {
    $original_time = get_the_time('U');
    $modified_time = get_the_modified_time('U');
    if ($modified_time >= $original_time + 86400) {
    $updated_time = get_the_modified_time('h:i a');
    $updated_day = get_the_modified_time('jS F, Y');
    $modified_content .= '<p class="last-modified">This information was updated on '. $updated_day . '</p>';
    }
    $modified_content .= $content;
    return $modified_content;
    }
    add_filter( 'the_content', 'show_last_modified_date' );
    #843942
    Tom
    Lead Developer
    Lead Developer

    Try this instead:

    function show_last_modified_date( $content ) {
        if ( ! in_category( 1139 ) ) {
            return $content;
        }
    
        $original_time = get_the_time('U');
        $modified_time = get_the_modified_time('U');
        $modified_content = '';
    
        if ($modified_time >= $original_time + 86400) {
            $updated_time = get_the_modified_time('h:i a');
            $updated_day = get_the_modified_time('jS F, Y');
            $modified_content .= '<p class="last-modified">This information was updated on '. $updated_day . '</p>';
        }
    
        $modified_content .= $content;
        return $modified_content;
    }
    add_filter( 'the_content', 'show_last_modified_date' );
    #844098
    Chantal

    Hi Tom,

    I get the following error notice at the top of the page:

    Notice: Undefined variable: modified_content in /dom7210/wp-content/plugins/code-snippets/php/snippet-ops.php(361) : eval()’d code on line 15

    The code on line 15 is:
    $modified_content .= $content;

    #844711
    Tom
    Lead Developer
    Lead Developer

    Looks like an issue with the original code. I just adjusted it above.

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