• Resolved dukearmi

    (@dukearmi)


    Hi there,

    I’ve tried this code to no avail:

    //* Shortcode to display the current year in WordPress
    //* shortcode: [year]
    add_shortcode( 'year' , 'current_year' );
        function current_year() {
        $year = date("Y");
        return "$year";
    }
    
    //* Activate shortcode function in Post Title
    add_filter( 'the_title', 'do_shortcode' );

    I’m thinking it might in conflict with another custom code to display the title in a page hero:

    function my_custom_page_hero() {
        // Check if it's not the homepage
    if (!is_front_page()) {
        // Output page or post title and post meta
        add_action('generate_after_header', function() {
            ?>
            <div class="page-hero">
                <div class="container">
                    <h1><?php
                        if (is_category()) {
                            single_cat_title();
                        } elseif (is_author()) {
                            the_author();
                        } else {
                            echo get_the_title();
                        }
                    ?></h1>

    Is there a way to fix this?

Viewing 6 replies - 1 through 6 (of 6 total)
  • ying

    (@yingscarlett)

    Hi there,

    I’m not sure I understand what are you trying to achieve, do you want to add the current year to all titles? Or some specific title?

    Thread Starter dukearmi

    (@dukearmi)

    Hi Ying,

    I’m trying to add the current year to specifc titles using shortcode, if possible.

    • This reply was modified 6 months ago by dukearmi.
    ying

    (@yingscarlett)

    Oh, in that case, you need to activate shortcode function for the title using the the_title filter.

    function enable_shortcodes_in_titles($title) {
        // Check if we are in the main query and not in the admin area
        if ( ! is_admin()  && is_main_query() ) {
            $title = do_shortcode($title);
        }
        return $title;
    }
    add_filter('the_title', 'enable_shortcodes_in_titles');
    Thread Starter dukearmi

    (@dukearmi)

    Just perfect. Thank you so much!

    Thread Starter dukearmi

    (@dukearmi)

    Oh actually even the code use first worked. I just realized I had the PHP code running only in the header. My bad. Thank you again!

    ying

    (@yingscarlett)

    No Problem ??

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Dynamically Display Current Year in Post Title’ is closed to new replies.