【问题标题】:Exclude Wordpress Category排除 Wordpress 类别
【发布时间】:2019-02-11 09:45:57
【问题描述】:

我有一个循环来显示归档页面上的不同类别。

你怎么能只显示类别一次

目前,如果两个帖子属于同一类别,则该类别有两次

这是我下面的代码

        <div class="row ptb-20">

            <?php

            $args = array(
                'category_name' => 'actualites',
            );

            // Custom query.
            $query = new WP_Query( $args );

            // Check that we have query results.
            if ( $query->have_posts() ) {

                // Start looping over the query results.
                while ( $query->have_posts() ) {

                    $query->the_post();?>

                    <div class="category-filter">
                        <div class="single-filter">

                            <?php

                            $categories = get_the_category();
                            $separator = ", ";
                            $output = ' ';

                            if ($categories) {

                                foreach ($categories as $category) {

                                    $output .= '<li><a href="' . get_category_link($category->term_id) . '">' . $category->cat_name . '</a></li>';

                                }

                                echo trim($output, $separator);

                            }

                            ?>

                        </div>   
                    </div>

                    <?php

                } // End while 
            } // End if

            else { echo '<p>Aucune actualité trouvée</p>'; } ?>

            <?php wp_reset_postdata(); ?>

        </div>

【问题讨论】:

    标签: php wordpress categories


    【解决方案1】:

    方法一:使用插件从 WordPress 中排除类别 您需要做的第一件事是安装并激活 Ultimate Category Excluder 插件。有关更多详细信息,您应该按照我们关于如何安装 WordPress 插件的指南进行操作。

    激活后,您需要转到设置 » 类别排除页面。它将显示您的 WordPress 博客上可用的所有类别。

    方法2:使用代码从WordPress主页中排除类别 此方法要求您将代码添加到 WordPress 文件中。如果您以前没有这样做过,请参阅我们的指南,了解如何在 WordPress 中复制和粘贴代码 sn-ps。

    您需要将以下代码添加到主题的 functions.php 文件或特定于站点的插件中。

       function exclude_category_home( $query ) {
        if ( $query->is_home ) {
        $query->set( 'cat', '-5' );
        }
        return $query;
        }
    
    
    add_filter( 'pre_get_posts', 'exclude_category_home' );
    

    不要忘记将 ID (-5) 替换为您的类别 ID。它将隐藏主页上属于与此 ID 匹配的类别的所有博客文章。

    注意:确保在类别 ID 中添加减号 (-)。

    请参考:https://www.wpbeginner.com/wp-tutorials/how-to-exclude-a-category-from-your-wordpress-homepage/

    【讨论】:

      【解决方案2】:

      如果我正确理解您的问题,您可以引入一个变量,您可以在其中记住已使用的类别,因此您不要多次包含它们。

          <div class="row ptb-20">
      
              <?php
      
              $args = array(
                  'category_name' => 'actualites',
              );
      
              // Custom query.
              $query = new WP_Query( $args );
      
              // Check that we have query results.
              if ( $query->have_posts() ) {
      
                  // Start looping over the query results.
                  while ( $query->have_posts() ) {
      
                      $query->the_post();?>
      
                      <div class="category-filter">
                          <div class="single-filter">
      
                              <?php
      
                              $categories = get_the_category();
                              $categories_displayed = [];
                              $separator = ", ";
                              $output = ' ';
      
                              if ($categories) {
      
                                  foreach ($categories as $category) {
                                      if (!in_array($category->cat_name, $categories_displayed)) { 
                                          $output .= '<li><a href="' . get_category_link($category->term_id) . '">' . $category->cat_name . '</a></li>';
      
                                          $categories_displayed[] = $category->cat_name;
                                      }
                                  }
      
                                  echo trim($output, $separator);
      
                              }
      
                              ?>
      
                          </div>   
                      </div>
      
                      <?php
      
                  } // End while 
              } // End if
      
              else { echo '<p>Aucune actualité trouvée</p>'; } ?>
      
              <?php wp_reset_postdata(); ?>
      
          </div>
      

      【讨论】:

      • 您好,感谢您的回答。目前,您的代码说:-> 语法错误,(url) 中的意外 '[' -> 高于 $separator
      • 对不起,我没有测试它,但它应该很容易解决。马上更新
      • 感谢您的更新,但又出现了错误,在同一个地方(开头,abose $separator)
      • 我还没有找到解决办法
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-10-29
      • 2014-12-27
      • 1970-01-01
      • 2015-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多