dongqing314511 2016-09-27 17: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 20: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.

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

报告相同问题?

悬赏问题

  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换