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.

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

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度