WordPress Yoast SEO- 根据GET参数在片段中生成动态标题和描述

huangapple go评论72阅读模式
英文:

WordPress Yoast SEO- dynamic title and description in snippet based on GET parameter

问题

我有一个安装了Yoast SEO的WordPress网站。我有一个特定的页面,/charts,它接受GET参数id。根据id的值,我想从服务器上的JSON文件中加载标题和描述,并将其设置为在Twitter、Facebook、Slack等粘贴链接时显示的标题和描述。

Yoast会用Edit Page面板上为该特定页面设置的任何内容来覆盖它,我无法弄清楚是否可以设置Yoast中的自定义变量,根据id的值从JSON文件中设置标题和描述。最终,我尝试了此解决方案来绕过Yoast的限制,但结果是,当我粘贴链接时根本没有显示任何片段。

请注意,除了这个/charts页面之外,我不希望在网站的任何其他地方改变Yoast的行为。是否有可能找到一个解决方案,根据GET参数中的id值动态更改页面和片段的标题和元描述?

目前,我只使用以下代码(用于标题)来覆盖Yoast并显示我们自定义的标题:

add_action( 'template_redirect', 'remove_wpseo' );
function remove_wpseo() {
    if ( is_page('301') ) {
        $front_end = YoastSEO()->classes->get( Yoast\WP\SEO\Integrations\Front_End_Integration::class );
        remove_action( 'wpseo_head', [ $front_end, 'present_head' ], -9999 );
    }
}

add_filter('wpseo_title', 'filter_product_wpseo_title');
function filter_product_wpseo_title($title) {
    if((is_page('301')) && (isset($_GET['sf']))){
        return 'custom title for ' . $_GET['sf'];
    }
}
英文:

I have a WordPress site with Yoast SEO installed on it. I have a certain page, /charts, which accepts the GET parameter id. Based on the value of id, I want to load a title and description from a JSON file on the server, and set it as the title and description that shows up on a snippet when you paste the link on Twitter, Facebook, Slack, etc.

Yoast overrides that with whatever is set up for that particular page on the Edit Page panel, and I could not figure out if a custom variable in Yoast could be set up that sets the title and description from the JSON file based on id's value. I ended up trying this solution to bypass Yoast for that particular page but as a result, no snippet shows up at all when I paste the link.

Note that apart from this /charts page, I do not want to alter Yoast's behaviour anywhere on the site. Is a solution possible to the same that changes the title and meta description - on the page and on snippets - dynamically with the id value in the GET parameter?

Currently I just use this code (for the title) to override Yoast and show our custom title:

add_action( 'template_redirect', 'remove_wpseo' );
function remove_wpseo() {
    if ( is_page('301') ) {
        $front_end = YoastSEO()->classes->get( Yoast\WP\SEO\Integrations\Front_End_Integration::class );
        remove_action( 'wpseo_head', [ $front_end, 'present_head' ], -9999 );
    }
}

add_filter('wpseo_title', 'filter_product_wpseo_title');
function filter_product_wpseo_title($title) {
    if((is_page('301')) && (isset($_GET['sf']))){
        return 'custom title for ' . $_GET['sf'];
    }
} 

答案1

得分: 0

我成功解决了这个问题 - 你仍然需要在 functions.php 中添加动作 remove_wpseo,以覆盖页面上的 Yoast。

原来应用程序上的链接预览由 Open Graph 元标签控制,这是我遗漏的部分。我在我的页面代码中添加了 <meta property = 'og:title' content = '<基于ID的动态标题内容>' />,然后它就起作用了。如果你的页面标题没有定义 'og:title' 属性,应用程序上的链接预览可能不起作用。

你可以使用现有的 <meta name='description' 属性来动态添加描述。

英文:

I was able to solve the issue - you still need to add the action remove_wpseo above to functions.php to override Yoast on the page.

Turns out link previews on apps are dictated by the Open Graph meta tags, which is what I was missing. I added <meta property = 'og:title' content = '<dynamic title content based on the ID>' /> to my page code, and it worked. Without the 'og:title' property defined for your page title, link previews may not work for an app.

You can use the existing <meta name='description' property to add a description dynamically.

huangapple
  • 本文由 发表于 2023年7月10日 13:49:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76650941.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定