Site logo

Archived topic

Last Updated Date on a Specific Category

5 replies · Started by Chantal on March 19, 2019

Viewing posts 1–6 of 6

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.

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' );

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' );

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;

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

This archived topic is closed to new replies.