【问题标题】:apply wpseo_next_rel_link filter to specific category将 wpseo_next_rel_link 过滤器应用于特定类别
【发布时间】:2017-02-02 21:52:06
【问题描述】:

我在我的网站上使用来自 yoast 的 wp-seo 插件,我只想在特定类别中操作此过滤器。

这是我尝试这样做但不起作用的方式。我该怎么做?

add_filter('wpseo_next_rel_link', 'my_custom_rel_next');
function my_custom_rel_next($my_c_next_link) {
    //get the last part of the URL
    $parts = parse_url($url);
    parse_str($parts['query'], $query);
    $page_number_var = $_GET['cpage'];
    $page_number_plus_1_var = $_GET['cpage']+1;
    // get url section ex: "mystars", "studios", etc..
    $segments = explode('/', trim(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), '/'));
    $numSegments = count($segments); 
    $currentSegment = $segments[$numSegments - 1];
    // define links to show
        // define links to show
        $next_link_2_next_pages = '<link rel="next" href="'.get_site_url().'/'.$currentSegment.'/?cpage='.$page_number_plus_1_var.'" />';
        if (is_category(561)) {
        return $next_link_2_page_1;
        }else{
        return 'regular next_link outputted by the plugin'; 
        }
}

【问题讨论】:

  • 代码中是否有 my_custom_rel_next 函数?过滤器会在钩子执行时尝试调用它。
  • @bswatson 不确定我是否理解你。你能澄清一下吗?
  • 当然!在与wpseo_next_rel_link 挂钩的add_filter 定义中,您指定回调函数为my_custom_rel_next,这是应用过滤器时WordPress 将使用的函数。我希望看到一个定义了function my_custom_rel_next( $link ) {} 的函数来处理修改链接。我的猜测是,add_filter 调用下的所有内容都应该包含在该函数中,或者您在其他地方定义了上述问题中未包含的该函数。
  • @bswatson 抱歉……那是我的spinet 中缺少的部分。我在上面编辑了我的问题。谢谢!
  • 修改后,上面的代码还有问题吗?

标签: wordpress function filter


【解决方案1】:

正确的做法:

add_filter( 'wpseo_head', 'custom_change_wpseo_next' , '20' );
function custom_change_wpseo_next( $link ) {
if ( is_page_template(array('page-1.php', 'page-2.php', 'page-3.php')) ) {
 add_filter( 'wpseo_canonical', '__return_false' );
            if ( class_exists('WPSEO_Frontend') ) {
                    $frontend = WPSEO_Frontend::get_instance();
                    global $wp_query;
      if ( get_query_var('paged') ) {
          $paged = get_query_var('paged');
                            } elseif ( get_query_var('page') ) {
                                $paged = get_query_var('page');
                            } else {
                                $paged = 1;
                                echo '<link rel="canonical" href="'.get_pagenum_link( $paged).'" />' . PHP_EOL;
                                echo '<link rel="next" href="'.get_pagenum_link( $paged +1 ).'" />' . PHP_EOL;  
                            }
                            if ( $paged > 1 ) {
                                    //$link0 = '<meta name="robots" content="noindex,follow"/>' . PHP_EOL;
                                    echo '<link rel="canonical" href="'.get_pagenum_link( $paged).'" />' . PHP_EOL; 
                                    echo '<link rel="prev" href="'.get_pagenum_link( $paged -1 ).'" />' . PHP_EOL;  
                                    echo '<link rel="next" href="'.get_pagenum_link( $paged +1 ).'" />' . PHP_EOL;  
                            }
                    }
}
    echo $link;
}

根据需要进行调整。

【讨论】:

    猜你喜欢
    • 2011-11-01
    • 2022-11-03
    • 2014-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-11
    • 2017-09-03
    • 2023-03-04
    相关资源
    最近更新 更多