WordPress 自动生成“规范” links - 如何添加自定义 URL 参数?

发布于 2024-08-24 11:27:08 字数 737 浏览 3 评论 0原文

有谁知道如何修改 WordPress 规范链接以添加自定义 URL 参数?

我有一个 Wordpress 站点,其中一个页面查询单独的(非 Wordpress)数据库。我传递了 URL 参数“pubID”来显示个别书籍,并且工作正常。

示例: http://www.uglyducklingpresse.org/catalog/browse/item /?pubID=63

但是各个图书页面在 Google 中无法正确显示 - ?pubID 参数被删除。

我认为这可能是因为所有项目页面在源代码中都具有相同的自动生成的“规范”URL 链接标记 - 一个删除了“pubID”参数的链接标记。

示例:链接 rel='canonical' href='http://www.uglyducklingpresse.org/ Catalog/browse/item/'

有没有办法编辑 .htaccess 以向 Wordpress 添加自定义 URL 参数,以便该参数不会被永久链接和“规范”链接删除?

或者也许还有另一种解决方案......谢谢您的任何想法!

Does anyone know how to modify the Wordpress canonical links to add a custom URL parameter?

I have a Wordpress site with a page that queries a separate (non-Wordpress) database. I passed the URL parameter "pubID" to display individual books and it is working OK.

Example: http://www.uglyducklingpresse.org/catalog/browse/item/?pubID=63

But the individual book pages are not showing up properly in Google - the ?pubID parameter is stripped out.

I think maybe this is because all the item pages have the same auto-generated "canonical" URL link tag in the source - one with the "pubID" parameter stripped out.

Example: link rel='canonical' href='http://www.uglyducklingpresse.org/catalog/browse/item/'

Is there a way to perhaps edit .htaccess to add a custom URL parameter to Wordpress, so that the parameter is not stripped out by permalinks and the "canonical" links?

Or maybe there's another solution ... Thank you for any ideas!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

≈。彩虹 2024-08-31 11:27:08

您应该能够将 WordPress 的 rel_canonical 操作函数替换为您自己的函数,在该函数中(当满足您的条件时)您创建一个附加查询字符串变量的规范链接。尽管您可能需要更改条件以满足您的需求,但以下内容应该可行。

remove_action('wp_head', 'rel_canonical');
add_action('wp_head', 'my_rel_canonical');

function my_rel_canonical() {
    if (is_page('item') && isset($_GET['pubID'])) {
        global $post;
        $link = get_permalink($post->ID) . '?pubID=' . absint($_GET['pubID']);
        echo "<link rel='canonical' href='$link' />\n";
    } else {
        rel_canonical();
    }
}

You should be able to replace Wordpress's rel_canonical action function with your own function in which (when your conditions are meet) you create a canonical link appending the query string variable. The following should work, although you'll probably need to change the conditions to meet your needs.

remove_action('wp_head', 'rel_canonical');
add_action('wp_head', 'my_rel_canonical');

function my_rel_canonical() {
    if (is_page('item') && isset($_GET['pubID'])) {
        global $post;
        $link = get_permalink($post->ID) . '?pubID=' . absint($_GET['pubID']);
        echo "<link rel='canonical' href='$link' />\n";
    } else {
        rel_canonical();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文