如何在简码中修改页面标题? [英] How to modify page title in shortcode?

查看:29
本文介绍了如何在简码中修改页面标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何修改短代码中特定页面的页面标题?

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 executes.

function assignPageTitle(){
  return "Title goes here";
}
add_filter('wp_title', 'assignPageTitle');

有没有办法在短代码函数中调用上述内容?我知道如何使用 do_shortcode() 但上面是一个过滤器.

Is there a way to call the above in a shortcode function? I know how to use do_shortcode() but the above is a filter.

我的目标是根据 URL 参数修改页面标题.这只发生在特定页面上.

My goal is to modify the page title based on a URL parameter. This only happens for specific pages.

推荐答案

摘自 WordPress Codex

WordPress 2.5 中引入了 Shortcode API,这是一组简单的用于创建用于发布内容的宏代码的函数.

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.

你到底想做什么?使用 Yoast SEO 插件,您可以在每个帖子中设置帖子和页面标题,如果这是您想要的做什么?

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?

您可以根据您的 URL 参数创建您的自定义插件,如下所示:

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');

这篇关于如何在简码中修改页面标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆