【问题标题】:Exclude Category From wp_get_archives?从 wp_get_archives 中排除类别?
【发布时间】:2010-05-07 14:19:52
【问题描述】:

有没有办法从 wp_get_archives 中排除一个类别?我试图在侧边栏中显示月份,但我想排除不是博客条目的帖子。

$catID = get_cat_id('Projects');
$variable = wp_get_archives('type=monthly&show_post_count=1);
echo $variable;

【问题讨论】:

    标签: wordpress categories archive


    【解决方案1】:

    如果您只想在主题目录的 functions.php 中包含 wp_get_archive 函数的特定类别,请使用此选项

    add_filter( 'getarchives_where', 'customarchives_where' );
    add_filter( 'getarchives_join', 'customarchives_join' );
    
    function customarchives_join( $x ) {
    
        global $wpdb;
    
        return $x . " INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)";
    
    }
    
    function customarchives_where( $x ) {
    
        global $wpdb;
    
        $includes= '14'; // category id to include
        return $x . " AND $wpdb->term_taxonomy.taxonomy = 'category' AND $wpdb->term_taxonomy.term_id = '$includes'";
    
    }
    

    【讨论】:

      【解决方案2】:

      你可以在你的functions.php文件中写一个过滤器来改变wp_get_archive函数的默认行为。

      add_filter( 'getarchives_where', 'customarchives_where' );
      add_filter( 'getarchives_join', 'customarchives_join' );
      
      function customarchives_join( $x ) {
      
          global $wpdb;
      
          return $x . " INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)";
      
      }
      
      function customarchives_where( $x ) {
      
          global $wpdb;
      
          $exclude = '1'; // category id to exclude
      
          return $x . " AND $wpdb->term_taxonomy.taxonomy = 'category' AND $wpdb->term_taxonomy.term_id NOT IN ($exclude)";
      
      }
      

      【讨论】:

      • 6 年后复制粘贴到我的 functions.php 中,它仍然有效!
      • 其实我收回了。这仅适用于帖子排除的类别,没有其他类别。
      【解决方案3】:

      在一个项目中遇到了这个问题,但从未在网上找到解决方案——我的 PHP 不是最漂亮的,但它可以解决问题。

      这是 Katie 建议的过滤器,我也在几个支持论坛中遇到过。这进入你的functions.php

      add_filter( 'getarchives_where', 'customarchives_where' );
      add_filter( 'getarchives_join', 'customarchives_join' );
      
      function customarchives_join( $x ) {
      
          global $wpdb;
      
          return $x . " INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) 
          INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)";
      
      }
      
      function customarchives_where( $x ) {
      
          global $wpdb;
      
          $categories = get_terms( 'taxonomy-name', 'orderby=id' );
          $includeIds;
          $i = 0;
          foreach($categories as $category) {
              if($i != 0) $includeIds .= ',';
              $includeIds .= $category->term_id;
              $i++;
          } 
      
      
          return $x . " AND $wpdb->term_taxonomy.taxonomy = 'taxonomy-name' 
          AND $wpdb->term_taxonomy.term_id IN ($includeIds)";
      
      }
      

      在第二个函数中,将 taxonomy-name 替换为您的实际自定义分类的名称。

      您的自定义分类中的所有术语 ID 都被捕获在一个字符串中;其余的操作与原始函数相同——只有自定义分类法中的类别列表包含在 wp_get_archives() 列表中。您还可以调整代码以排除它们(上面的第一个示例)。

      如果您只希望wp_get_archives() 列表的一个实例执行此操作,只需跳过functions.php 中应用过滤器的前两行代码。然后,当您使用 wp_get_archives() 标记时,应用它之前的过滤器,然后将它们删除:

      <?php   
      add_filter( 'getarchives_where', 'customarchives_where' );
          add_filter( 'getarchives_join', 'customarchives_join' );
      
          wp_get_archives(); 
      
          remove_filter( 'getarchives_where', 'customarchives_where' );
          remove_filter( 'getarchives_join', 'customarchives_join' );
      ?>
      

      【讨论】:

        【解决方案4】:

        wp_get_archives() 没有基于类别排除的机制——它纯粹用于基于时间的存档(每年、每月、每天、每周)或“每个帖子”存档:postbypost 或按帖子标题排序的 post by post .

        【讨论】:

          【解决方案5】:

          有不同的方式来处理类别档案:WordPress › Support » Limit archives to category / date archives for category

          我使用 Clean Archives Reloaded WordPress › Clean Archives Reloaded « WordPress Plugins 并排除第 200 行附近的类别:

          // Get a simple array of all posts
          $rawposts = get_posts( 'numberposts=-1&category=-4,-6,-7,-9' );
          

          【讨论】:

            【解决方案6】:

            您可能想要查看get_categories 并倾向于您自己的自定义解决方案。虽然这可能会花费您更多的时间和工作;您确实会获得完全控制您想要实现的目标的结果。

            【讨论】:

              【解决方案7】:

              将下面的代码放在后面 这段代码已经为我工作了:)

              <?php
              if ( $wp_query->is_archive ){
              $wp_query->query_vars["cat"] = 14; // only the category that you want to inlcude
              $wp_query->query_vars["posts_per_page"] = 10; // for number of posts you want
              $wp_query->get_posts();}
              ?>
              

              【讨论】:

              • 这适用于主循环,不适用于 wp_get_archives() 函数
              【解决方案8】:

              您可以在 pre_get_posts 上使用过滤器挂钩吗?

              我知道这样的东西适用于 is_author、is_home 和 is_feed...

              function exclude_stuff($query) { 
                  if ( $query->is_author) {
                      $query->set('cat', '-4, -142');
                  }
                  return $query;
              }
              
              add_filter('pre_get_posts', 'exclude_stuff');
              

              取决于您是否可以为 is_archive 或 is_monthly 之类的东西这样做

              你可以把它放到一个带有插件头的 php 文件中:

              <?php
              /*
               * Plugin Name: exclude some stuff
               * Description: blah
               * Author: blah
               * Plugin URI: blah
               * Version: 0.9
               * =======================================================================
              */
                 Put the function here
              ?>
              

              然后将其上传到您的插件目录并激活它。

              【讨论】:

                【解决方案9】:

                没有正式的方法可以做到这一点。但是我尝试并测试了很多代码块,只有这个对我有用。

                    add_filter( 'getarchives_where', 'customarchives_where' );
                add_filter( 'getarchives_join', 'customarchives_join' );
                
                function customarchives_join( $x ) {
                    global $wpdb;
                    return $x . " INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)";
                }
                
                function customarchives_where( $x ) {
                    global $wpdb;
                    $include = 'your_category_id'; // category id to include
                    return $x . " AND $wpdb->term_taxonomy.taxonomy = 'category' AND $wpdb->term_taxonomy.term_id IN ($include)";
                }
                

                将 your_category_id 替换为您的帖子类别的原始 id,代码将起作用。

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2013-10-29
                  • 2014-12-27
                  • 1970-01-01
                  • 2018-05-17
                  • 2022-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多