[Resolved] Post year next to post title

Home Forums Support Post year next to post title

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #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 2019

    thanx

    #1063146
    David
    Staff
    Customer Support

    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;
    }
    #1064241
    Marc

    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?

    #1064246
    Leo
    Staff
    Customer Support

    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

    #1064329
    Marc

    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…

    #1064379
    David
    Staff
    Customer Support
    #1064492
    Marc

    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;
    }
    #1064654
    David
    Staff
    Customer Support

    Awesome – happy to help where we can

Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.