简体   繁体   English

WordPress:覆盖 YoastSEO 中的规范 URL

[英]WordPress: Overwrite the canonical URL in YoastSEO

I'm using a snippet to overwrite the canonical URL in YoastSEO for WordPress/WooCommerce.我正在使用一个片段覆盖 WordPress/WooCommerce 的 YoastSEO 中的规范 URL。 The snippet is based on the official docs example: https://developer.yoast.com/features/seo-tags/canonical-urls/api/该片段基于官方文档示例: https://developer.yoast.com/features/seo-tags/canonical-urls/api/

Here's my code:这是我的代码:

function prefix_filter_canonical_example( $canonical ) {
 
    if (is_shop() && is_paged() ) :
 
        $canonical = get_permalink(woocommerce_get_page_id( 'shop' )).'page/'.get_query_var('paged').'/';
    
    elseif(WCV_Vendors::is_vendor_page()):
    
        $vendor_shop = urldecode( get_query_var( 'vendor_shop' ) );
        $vendor_id   = WCV_Vendors::get_vendor_id( $vendor_shop );
        $canonical  = WCV_Vendors::get_vendor_shop_page( $vendor_id );
    
    endif;
    
    return $canonical;
    
}   
add_filter( 'wpseo_canonical', 'prefix_filter_canonical_example' );

The code doesn't do anything to the canonical URL regardless of the content I return.无论我返回的内容如何,该代码都不会对规范的 URL 做任何事情。 But the if/else works fine and if I echo the content of $canonical I see the correct URLs.但是 if/else 工作正常,如果我回显$canonical的内容,我会看到正确的 URL。

I tried it already with the basic storefront theme and I deactivated nearly all plugins.我已经用基本的店面主题尝试过了,并且我停用了几乎所有的插件。 But the snippet won't work.但该片段将不起作用。 Is there anything I miss?有什么我想念的吗?

If you don't pass any priority to the action wordpress will take it as 10. Yoast SEO also call this filter to add canonical url so wordpress executed function in the order in which they were added to the action. If you don't pass any priority to the action wordpress will take it as 10. Yoast SEO also call this filter to add canonical url so wordpress executed function in the order in which they were added to the action.

So change this line所以改变这一行

add_filter( 'wpseo_canonical', 'prefix_filter_canonical_example' );

With this有了这个

add_filter( 'wpseo_canonical', 'prefix_filter_canonical_example', 20 );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM