• Resolved drreen

    (@drreen)


    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?

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author Sybre Waaijer

    (@cybr)

    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 ($args and $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 https://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 8 years, 1 month ago by Sybre Waaijer.
    • This reply was modified 8 years, 1 month ago by Sybre Waaijer. Reason: Typo in code
    Thread Starter drreen

    (@drreen)

    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! ??

    • This reply was modified 8 years, 1 month ago by drreen.
    • This reply was modified 8 years, 1 month ago by drreen.
    Plugin Author Sybre Waaijer

    (@cybr)

    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!

    Thread Starter drreen

    (@drreen)

    It’s supposed to be a completely custom title!

    Custom Title beginning – Current Month and Year

    Eg. Website title – January 2017

    Plugin Author Sybre Waaijer

    (@cybr)

    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 ??

    Thread Starter drreen

    (@drreen)

    Thanks for the help, I really appreciate it! ??
    I will consider your advice and make a decision!

    Plugin Author Sybre Waaijer

    (@cybr)

    Anytime! If you have any more questions, feel free to ask ??

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Use custom code for custom page title’ is closed to new replies.