首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如果是amp,请将内部链接更改为amp版本(WordPress)

如果是amp,请将内部链接更改为amp版本(WordPress)
EN

Stack Overflow用户
提问于 2018-08-22 14:27:51
回答 3查看 1.2K关注 0票数 -3

我们使用WordPress,并希望链接amp到amp,如果链接的页面有一个amp版本。我们有这样的amp结构: test.de/test/amp

不幸的是,我的functions.php中的这段代码并不适用于post内容中硬编码的链接。我必须改变什么,这样它才能对每个内部链接起作用:

代码语言:javascript
复制
add_filter( 'post_link', function( $url, $post ) {
    static $recursing = false;
    if ( $recursing ) {
        return $url;
    }
    $recursing = true;
    if ( ! function_exists( 'post_supports_amp' ) || ! post_supports_amp( $post ) ) {
        return $url;
    }
    if ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() ) {
        $url = amp_get_permalink( $post->ID );
    }
    $recursing = false;
    return $url;
}, 10, 2 );

目前它也适用于规范链接,这对搜索引擎优化真的很不好。如何防止这种情况发生?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-08-29 21:41:24

将这些函数添加到主题的'functions.php‘中。

代码语言:javascript
复制
/* post link filter */
add_filter( 'post_link', 'change_amp_url', 10, 2 );

function change_amp_url( $url, $postobj ) {
    static $recursing = false;
    if ( $recursing ) {
        return $url;
    }

    $recursing = true;
    if ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() ) {
        if ( function_exists( 'post_supports_amp' ) && post_supports_amp( $postobj ) ) {
            $url = amp_get_permalink( $postobj->ID );           
        }
    }
    $recursing = false;
    return $url;
}

/* content link filter */
add_filter( 'the_content', 'change_amp_url_content' );

function change_amp_url_content($content)
{
    $dom = new DOMDocument();
    $dom->loadHTML($content);

    $tags = $dom->getElementsByTagName('a');
    foreach ($tags as $tag) {
        $link = $tag->getAttribute('href'); // original url
        $extralink = '';
        if(stristr($link,'#')) {
            $pagelinktemp = explode("#",$link);
            $pagelink = $pagelinktemp[0];
            $extralink = '#'.$pagelinktemp[1];
        } else {
            $pagelink = $link;
        }
        if($pagelink!="") {     
            $postid = url_to_postid($pagelink);
            $postobj = get_post($postid); // getting appropriate post object            
            if($postobj) {          
                $newlink = change_amp_url( $pagelink, $postobj ); //new url
            }
            else {
                $newlink = $link;
            }
        }
        else {
            $newlink = $link;
        }
        if($link != $newlink) // change if only links are different
        {
            $content = str_replace($link, $newlink.$extralink, $content);
        }
    }
    return $content;
}

/* override canonical link */
add_filter( 'wpseo_canonical', 'amp_override_canonical' );

function amp_override_canonical($url) {
    if ( substr($url,-4)=="/amp" ) {    
        $url = substr($url,0,-4);
    }
    return $url;
}

第一个函数将提供AMP URL (如果存在)。

第二个将遍历内容中的每个URL,并更改为AMP URL (如果有效)。

最后一个将重写通过Yoast SEO插件显示的规范URL。

票数 3
EN

Stack Overflow用户

发布于 2018-08-29 03:29:25

如果你想替换帖子内容中的硬编码链接,我建议你使用wordpress的"the_content“过滤器。

https://codex.wordpress.org/Plugin_API/Filter_Reference/the_content

代码语言:javascript
复制
add_filter( 'the_content', 'filter_function_name' )

由此,您应该能够使用正则表达式匹配链接并将/amp附加到该链接。

伪代码示例:

代码语言:javascript
复制
function my_the_content_filter($content)
{
    if (function_exists('is_amp_endpoint') && is_amp_endpoint()) {
        $patterns = array(
            //patterns
        );
        $replacements = array(
           //replacements

        );
        $content = preg_replace($patterns, $replacements, $content);
    }

    return $content;
}

add_filter('the_content', 'my_the_content_filter');
票数 1
EN

Stack Overflow用户

发布于 2018-12-07 05:30:35

我测试了Outsource WordPress提交的代码,总体上运行良好,但'amp_override_canonical函数会覆盖删除/amp的页面的所有urls。

我对这段代码做了一些修改,但它们不能像我期望的那样工作。“wpseo_canonical”函数似乎是在不同的上下文中调用的。

代码语言:javascript
复制
add_filter( 'wpseo_canonical', 'amp_override_canonical' );

function amp_override_canonical($url) {
    if ( substr($url,-4)=="/amp" ) {    
        $url = substr($url,0,-4);
    }
    return $url;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51960938

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档