[Resolved] Post year next to post title

Home Forums Support [Resolved] Post year next to post title

Home Forums Support Post year next to post title

Viewing 12 posts - 1 through 12 (of 12 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

    #1818198
    Rekindle

    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!

    #1818201
    Elvin
    Staff
    Customer Support

    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?

    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 and generate_after_entry_title.

    A wise man once said:
    "Have you cleared your cache?"

    #1818204
    Rekindle

    Thanks Elvin! That helps me understand how it works.

    #1818211
    Elvin
    Staff
    Customer Support

    No problem. Always glad to be of any help. 🙂

    A wise man once said:
    "Have you cleared your cache?"

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