How to modify page title in shortcode?

前端 未结 2 639
半阙折子戏
半阙折子戏 2021-01-22 00:37

How do I modify a page title for specific pages in shortcode?

The following will change the title but it executes for every page. I need more control over where it exec

2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-22 01:38

    Taken from the WordPress Codex

    Introduced in WordPress 2.5 is the Shortcode API, a simple set of functions for creating macro codes for use in post content.

    This would suggest that you can't control page titles using shortcodes as the shortcode is run inside the post content at which point the title tag has already been rendered and is then too late.

    What is it exactly that you want to do? Using the Yoast SEO Plugin you can set Post and Page titles within each post if this is what you want to do?

    You could create your custom plugin based on your URL parameters as so:

    function assignPageTitle(){
    
    if( $_GET['query'] == 'something' ) { return 'something'; }
    
    elseif( $_GET['query'] == 'something-else' ) { return 'something-else'; }
    
    else { return "Default Title"; }
    
    }
    
    add_filter('wp_title', 'assignPageTitle');
    

提交回复
热议问题