- This topic has 11 replies, 2 voices, and was last updated 2 years, 10 months ago by Elvin.
-
AuthorPosts
-
November 14, 2019 at 6:21 am #1062908Marc
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 #1063146DavidStaffCustomer 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; }
November 15, 2019 at 9:54 am #1064241MarcThanx 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 #1064246LeoStaffCustomer 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
November 15, 2019 at 11:42 am #1064329MarcThanx 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 #1064379DavidStaffCustomer SupportMaybe this will help:
November 15, 2019 at 5:42 pm #1064492MarcThanx 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 #1064654DavidStaffCustomer SupportAwesome – happy to help where we can
June 10, 2021 at 11:38 pm #1818198RekindleHi 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 #1818201ElvinStaffCustomer 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
.June 10, 2021 at 11:50 pm #1818204RekindleThanks Elvin! That helps me understand how it works.
June 11, 2021 at 12:03 am #1818211ElvinStaffCustomer SupportNo problem. Always glad to be of any help. 🙂
-
AuthorPosts
- You must be logged in to reply to this topic.