Use custom code for custom page title
-
For a specific page I would like to use an SEO title which includes a php code snippet so that the current month is displayed.
Is there a way to achieve that through a functions.php entry?
-
Hi @drreen,
That’s indeed possible π
It will only work if your theme is outputting the title correctly, or when the title has been fixed through the Title Fix extension.
This is what the code will look like, you can ignore the 2nd and 3rd parameters (
$argsand$escape).add_filter( 'the_seo_framework_pro_add_title', 'my_pro_title_alterations', 10, 3 ); /** * Alter title after it has been processed. * * @param string $title The current title. * @param array $args The title args * @param bool $escape Whether the title is being escaped in this call. * @return string The full title. */ function my_pro_title_alterations( $title = '', $args = array(), $escape = true ) { //* If it's a post: if ( is_single() ) { // You can change 'F' with any of these date codes: // @link http://php.net/manual/en/function.date.php $title = get_the_date( 'F' ) . ' - ' . $title; } //* If it's a page: if ( is_page() ) { // Code here } //* If it's an archive: if ( is_archive() ) { // Code here } //* If the current Post ID is 3 // Don't use get_the_ID as it can cross with archives. if ( 3 === get_queried_object_ID() ) { // Per your request, with i18n support: $title = date_i18n( 'F', true, false ) . ' - ' . $title; } return $title; }If you have any questions, feel free to ask! Cheers π
Edit: After re-reading your request, I’ve updated the code snippet.
-
This reply was modified 9 years, 3 months ago by
Sybre Waaijer.
-
This reply was modified 9 years, 3 months ago by
Sybre Waaijer. Reason: Typo in code
Thx!
Actually I intended to use not the current month as SEO title for whole website. The date is part of the SEO title for the whole website.
The desired SEO title for whole website:
Title is here - <?php echo date_i18n('F Y') ?>//edit: sorry for the confusion – it’s actually supposed to be for seo title for whole website
Yet the code is super useful and great for customization in the future! πHi @drreen,
Could you list a few example titles of what you wish it to become?
For example, any of these:
Post Title - Current Date - My website Post Title - Post Date - My website Post Title - Post Date Post Title - Current Date Current Date - Post Title - My Website // Only if archive: Archive Title - Month updated - My Website etc.Thanks!
It’s supposed to be a completely custom title!
Custom Title beginning – Current Month and Year
Eg. Website title – January 2017
Hi @ddreen,
Then I believe you require this (note, it’s now “pre”):
add_filter( 'the_seo_framework_pre_add_title', 'my_pre_title_alterations', 10, 3 ); /** * Alter title before it is processed. * * @param string $title The current title. * @param array $args The title args * @param bool $escape Whether the title is being escaped in this call. * @return string The title part. */ function my_pre_title_alterations( $title = '', $args = array(), $escape = true ) { return date_i18n( 'F Y', time(), false ); }Please be aware that if you have multiple pages on your website, the code above will effectively eventually mark your website as spam.
Dynamic titles (based on time alone) are also highly discouraged.
I recommend only altering a single page with the filter above.Nevertheless, I hope this helps π
-
This reply was modified 9 years, 3 months ago by
Sybre Waaijer.
Thanks for the help, I really appreciate it! π
I will consider your advice and make a decision!Anytime! If you have any more questions, feel free to ask π
-
This reply was modified 9 years, 3 months ago by
The topic ‘Use custom code for custom page title’ is closed to new replies.