dongqing314511 2016-09-27 09:12
浏览 76
已采纳

在wordpress中查找并替换html标头标签

Is it possible to find and replace html tags in header with a function?

Here's what I need to do:

I'm using wpseo plugin and I set default to no index subpages of archives.

But I have some categories where I would like to have sub-pages indexed and the plugin adds the noindex tag to them all.

Therefore, I need to find this tag <meta name="robots" content="noindex,follow"> and replace it with <meta name="robots" content="index,follow"> ONLY on those especific categories.

ie:

// First I need to get the url
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];

// Now I will check if I'm on category and if current url has the word "page"
// This way, I know I'm on the next pages of the category

if (is_category(array(561) &&  strpos($url,'page') !== false)) {
// If I'm on paged content of category 561

    => FIND THIS: <meta name="robots" content="noindex,follow">
    => REPLACE WITH: <meta name="robots" content="index,follow">';

How can I do this?

  • 写回答

1条回答 默认 最新

  • dongwen5336 2016-09-27 12:48
    关注

    you can hook into the WP SEO 'wpseo_robots' hook like this:

    <?php
    // add the filter using an anonymous function
    add_filter( 'wpseo_robots', function ( $robotsstr ) {
    
      if ( is_category(array(561) ) &&  ! is_paged() ) {
    
        $robotsstr = '<meta name="robots" content="index,follow">';
    
      }
    
      return $robotsstr;
    
    }, 10, 1 );
    

    ...or more "traditionally like this:

    function SO_39730632_amend_robots ( $robotsstr ) {
    
      if ( is_category(array(561) ) &&  ! is_paged() ) {
    
        $robotsstr = '<meta name="robots" content="index,follow">';
    
      }
    
      return $robotsstr;
    
    }
    
    add_filter( 'wpseo_robots', 'SO_39730632_amend_robots', 10, 1 );
    

    The is_paged() function should take care of the pagination check for you.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部