简体   繁体   English

限制具有超过 10 个关联帖子的标签的 Wordpress 标签页

[英]Limiting Wordpress Tag Pages for Tags with more than 10 associated posts

as the title said, I'm trying to write a custom WordPress filter which limits the creation of Tag pages so that Tag pages are only created if the Tag has more than 10 associated posts.正如标题所说,我正在尝试编写一个自定义 WordPress 过滤器,它限制标签页面的创建,以便仅在标签有超过 10 个相关帖子时创建标签页面。 This is because we have so many tags with <10 associated posts, and it's creating a lot of noise.这是因为我们有太多标签,相关帖子少于 10 个,这会产生很多噪音。

I've not worked with WordPress for almost 5 years now, so I'm a bit rusty.我已经快 5 年没有使用 WordPress 了,所以我有点生疏了。

Here's what I'm trying, and it's not quite working:这是我正在尝试的,但它不太有效:

<?php
function limit_taxonomies_by_count( $args, $taxonomy, $object_type ) {

  $terms = get_terms('post_tag');

  foreach($term in $terms) {
    if ($term->count < 10) {
      $args = array(
        'public' => false
      )
    }
  }

  return $args
}
add_filter('register_taxonomy_args', 'limit_taxonomies_by_count' );
?>

Please let me know what I'm missing!请让我知道我错过了什么!

You can do as follows to achieve your job done.您可以执行以下操作来完成您的工作。 You can remove those tags link whose associated posts count are less than 10, so visitors can never click on those tags.您可以删除相关帖子数少于 10 的那些标签链接,因此访问者永远无法点击这些标签。

function modify_term_link_url( $links ) {
    global $post;
    if( !$post ) return $links;
    $terms = get_the_terms( $post->ID, 'post_tag' );
    if ( is_wp_error( $terms ) ) {
        return $terms;
    }
    if ( empty( $terms ) ) {
        return false;
    }
    $links = array();
    foreach ( $terms as $term ) {
        if( $term->count < 10 ){
            $link = '';
        }else{
            $link = get_term_link( $term, 'post_tag' );
            if ( is_wp_error( $link ) ) {
                return $link;
            }
        }
        $links[] = '<a href="' . esc_url( $link ) . '" rel="tag">' . $term->name . '</a>';
    }
    return $links;
}
add_filter( 'term_links-post_tag', 'modify_term_link_url' );

Codes goes to your active theme's functions.php代码转到您的活动主题的功能。php

Instead of preventing the admins / editors from adding new tags, you could just "hide" Tag archive pages that don't meet the criteria (10 or more posts assigned to them).您可以“隐藏”不符合条件的标签存档页面(分配给他们的 10 个或更多帖子),而不是阻止管理员/编辑添加新标签。 This way, admins/editors can still create / use new tags that might eventually reach 10 or more posts which will then make them visible to visitors.这样,管理员/编辑仍然可以创建/使用最终可能达到 10 个或更多帖子的新标签,这将使它们对访问者可见。

To do so, you can use the template_redirect action hook to do something before the Tag archive page is loaded on screen (that something is explained next), then the is_tag() function to check whether the visitor is trying to access a Tag archive page, and finally the wp_redirect() function to do the actual redirection:为此,您可以在标记存档页面加载到屏幕上之前使用template_redirect操作挂钩来执行某些操作(接下来会解释),然后使用is_tag () function 检查访问者是否正在尝试访问标记存档页面,最后是wp_redirect() function 进行实际重定向:

/**
 * Redirects visitors to the homepage for Tags with
 * less than 10 posts associated to them.
 */
function wp76515_tag_maybe_redirect(){
    // We're viewing a Tag archive page
    if ( is_tag() ) {
        // Get Tag object
        $tag = get_tag(get_queried_object_id());
        // Tag's post count
        $post_count = $tag->count;

        // This tag has less than 10 posts,
        // redirect visitor
        if ( $post_count < 10 ) {
            wp_redirect(
                home_url(), // The URL we're sending the visitor to
                '302' // The HTTP status, 302 = 'Moved Temporarily'
            );
        }
    }
}
add_action('template_redirect', 'wp76515_tag_maybe_redirect', 5);

You may want to change the redirect code to 301 (Moved Permanently) to remove existing Tag pages with less than 10 posts from Google's index as well.您可能希望将重定向代码更改为301 (永久移动)以从 Google 的索引中删除帖子少于 10 个的现有标签页。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM