Wordpress 使用短代码更改页面标题

Wordpress 使用短代码更改页面标题,wordpress,Wordpress,我正在编写一个插件,我使用一个简短的代码来呈现页面内容。我想更改页面标题。我用了这个: // Change the title of the page of the charts to add the current month. add_filter('the_title', 'charts_title', 10, 2); function charts_title($title, $id) { $title .= ' ' . date('F Y'); return $title;

我正在编写一个插件,我使用一个简短的代码来呈现页面内容。我想更改页面标题。我用了这个:

// Change the title of the page of the charts to add the current month. 
add_filter('the_title', 'charts_title', 10, 2);
function charts_title($title, $id) { 
  $title .= ' ' . date('F Y');
  return $title;
}
但所有的帖子和页面都是如此。我可以只对包含我创建的短代码的页面执行此操作吗?我试过这么做,但没用

add_shortcode('charts-vote', 'charts_vote');
function charts_vote($atts) {
    // Add the filter only in the short code function callback.
    add_filter('the_title', 'charts_title', 10, 2);   

    // ... // 
    return $content;
}

有人能帮我吗?

我知道您的设置可能需要检查短码,但可能需要使用自定义字段:

add_filter( 'the_title', 'charts_title_so_15312385', 10, 2 );

function charts_title_so_15312385( $title, $post_id ) 
{ 
    // Admin area, bail out
    if( is_admin() )
        return $title;

    // Custom field not set, bail out
    $mod_title = get_post_meta( $post_id, 'mod_title', true );
    if( !$mod_title )
        return $title;

    // Ok, modify title
    $title .= ' ' . date( 'F Y' );
    return $title;
}
短代码甚至可以与扩展配置的自定义字段“对话”


为便于使用,您可以制作一个或使用类似的插件。

打印页眉后,您的快捷码将呈现。一般来说,这就是短代码的工作原理。也就是说,它们在
内容运行时渲染。这会很棘手的。谢谢你,先生,你是个救命恩人:)很乐意帮忙,巴普蒂斯特先生;)