- This topic has 11 replies, 2 voices, and was last updated 1 year, 7 months ago by
Elvin.
-
AuthorPosts
-
November 14, 2019 at 6:21 am #1062908
Marc
Hello,
I would like for the Posted year to appear on the same line as the Post Title, right after it
i.e. Title 2019thanx
November 14, 2019 at 8:07 am #1063146David
StaffCustomer SupportHi 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; }
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/November 15, 2019 at 9:54 am #1064241Marc
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?
November 15, 2019 at 10:00 am #1064246Leo
StaffCustomer SupportTry 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
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/November 15, 2019 at 11:42 am #1064329Marc
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…
November 15, 2019 at 12:47 pm #1064379David
StaffCustomer SupportMaybe this will help:
https://wordpress.stackexchange.com/a/26782
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/November 15, 2019 at 5:42 pm #1064492Marc
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 againhere 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; }
November 16, 2019 at 2:45 am #1064654David
StaffCustomer SupportAwesome – happy to help where we can
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/June 10, 2021 at 11:38 pm #1818198Rekindle
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!
June 10, 2021 at 11:48 pm #1818201Elvin
StaffCustomer SupportQuick 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
andgenerate_after_entry_title
.A wise man once said:
"Have you cleared your cache?"June 10, 2021 at 11:50 pm #1818204Rekindle
Thanks Elvin! That helps me understand how it works.
June 11, 2021 at 12:03 am #1818211Elvin
StaffCustomer SupportNo problem. Always glad to be of any help. 🙂
A wise man once said:
"Have you cleared your cache?" -
AuthorPosts
- You must be logged in to reply to this topic.