AMP-CHANGE REL=“Canonical”from unctions.php

时间:2017-05-09 作者:techno

中的源操作

/amp/includes/amp后模板操作。php是:

add_action( \'amp_post_template_head\', \'amp_post_template_add_canonical\' );
function amp_post_template_add_canonical( $amp_template ) {
?>

<link rel="canonical" href="<?php echo esc_url( $amp_template->get( \'canonical_url\' ) ); ?>" />

<?php
}
如何编辑来自函数的rel。php不编辑主文件?谢谢

1 个回复
最合适的回答,由SO网友:birgire 整理而成

通常,您可以在注册操作后取消注册该操作的回调,然后在调用之前注册自己的回调do_action( \'...\' ).

下面是一个示例:

// Add your own with later priority, e.g. 11 or wrap it in another later hook:
add_action( \'amp_post_template_head\', \'wpse_amp_post_template_add_canonical\', 11 ); 

function wpse_amp_post_template_add_canonical( $amp_template ) 
{
    // Remove the unwanted callback:
    remove_action( \'amp_post_template_head\', \'amp_post_template_add_canonical\' );

    // Modify this new output to your needs:
    printf( 
        \'<link rel="canonical" href="%s" />\', 
         esc_url( $amp_template->get( \'canonical_url\' ) )
    );

}

结束

相关推荐

Functions are causing errors

我在插件的PHP上声明一个函数。插件在每个帖子的顶部加载代码(帖子布局是插件的名称)。无法在/home/content/08/10290908/html/wp-content/plugins/post-layout/plugin.php(181):eval()\'d code:4)中重新声明vote\\u-up()(之前在/home/content/08/102908/html/wp-content/plugins/post-layout/plugin.php中声明)。php(181):第36行的eval