Archived topic
Post year next to post title
11 replies · Started by Marc on November 14, 2019
Hello,
I would like for the Posted year to appear on the same line as the Post Title, right after it
i.e. Title 2019
thanx
Hi there,
try this PHP snippet to filter the Post title to include the Year:
add_filter('the_title', 'db_add_post_date_title', 10, 2);
function db_add_post_date_title($title) {
if (get_post_type() !== 'post') {
return $title;
} else {
$year = get_the_date( 'Y' );
$title .= ' ' . $year;
}
return $title;
}
Thanx for the reply David,
I forgot to mention that this is meant only for custom post types.
I tried modifying the code to this:
add_filter('the_title', 'db_add_post_date_title', 10, 2);
function db_add_post_date_title($title)
{
if (get_post_type() !== 'post' )
{
return $title;
}
else if (is_post_type_archive( 'portfolio'||'cv_regies'||'cv_bruits' ))
{
$year = get_the_date( 'Y' );
$title .= ' ' . $year;
}
return $title;
}
to no avail... What am I missing?
Try this:
add_filter('the_title', 'db_add_post_date_title', 10, 2);
function db_add_post_date_title($title) {
if (get_post_type() !== 'post') {
return $title;
}
if (is_post_type_archive( array( 'foo', 'bar', 'baz' ) ) ) {
$year = get_the_date( 'Y' );
$title .= ' ' . $year;
}
return $title;
}
https://codex.wordpress.org/Conditional_Tags#A_Post_Type_Archive
Thanx for the reply Leo,
unfortunately, that's not working either.
I got it working using this:
add_filter('the_title', 'db_add_post_date_title', 10, 2);
function db_add_post_date_title($title)
{
if (is_post_type_archive( array( 'portfolio', 'cv_bruits', 'cv_regies' ) ) )
{
$year = get_the_date( 'Y' );
$title .= ' ' . $year;
}
else
{
return $title;
}
return $title;
}
But I get the Year added in all my navigation buttons...
Maybe this will help:
Thanx David,
That did the trick. I also feel the need to apologize as I now realize that this is all vanilla WordPress filtering and not Generatepress specific.
It just shows how much you guys are devoted!
thanx again
here is the final code
add_filter('the_title', 'pfl_add_post_date_title', 10, 2);
function pfl_add_post_date_title($title)
{
global $id;
if (($id && get_post_type($id) == 'cv_regies')||($id && get_post_type($id) == 'cv_bruits' ))
{
$year = get_the_date( 'Y' );
$title .= ' ' . '<span class="pfl_inline_date">'.$year.'</span>';
}
else
{
return $title;
}
return $title;
}
Awesome - happy to help where we can
Hi David,
I was trying to strip a few characters from the title of a custom post type. I struggled with this issue for a while, so your response to this helped solve this issue.
Quick question on this: I added this PHP as a code snippet. Is there a way to implement this using the hook element? From my understanding, all the hooks in GeneratePress seem to be action hooks, not filter hooks - is this correct? Is there no way to refer to the_title from within the hook element?
Thanks!
Quick question on this: I added this PHP as a code snippet. Is there a way to implement this using the hook element? From my understanding, all the hooks in GeneratePress seem to be action hooks, not filter hooks – is this correct? Is there no way to refer to the_title from within the hook element?
Yes that's correct. All are action hooks.
Since what you seem to want to achieve is to modify the_title output, you'll need to use a filter.
For filters, you'll have to do them on a code snippets plugin or child theme's functions.php.
But if in case you want to hook in something after or before the title rather than modifying the actual title output, there are hooks for that. use generate_before_entry_title and generate_after_entry_title.
Thanks Elvin! That helps me understand how it works.
No problem. Always glad to be of any help. :)