显示的是所有类别的帖子,而不是特定类别

时间:2015-08-04 作者:dh47

创建自定义帖子类型Menu 它有CategoriesSub-Categories.

我使用此代码成功地获取了帖子的子类别。

<?php
    $wcatTerms = get_terms(\'menu-items\', array(\'hide_empty\' => 0, \'orderby\' => \'ASC\',  \'parent\' =>0));
    foreach($wcatTerms as $wcatTerm) : 
        //echo $wcatTerm->name;
        $wsubargs = array(\'hierarchical\'=>1,\'show_option_none\'=>\'\',\'hide_empty\'=>0,\'parent\'=>$wcatTerm->term_id,\'taxonomy\'=>\'menu-items\');
        $wsubcats = get_categories($wsubargs);
        foreach ($wsubcats as $wsc):
           $childcat = $wsc->name; ?>
            <h2><?php echo $childcat; ?></h2>

        endforeach;
    endforeach; 
以后根据我的要求,我想在sub-category 为此我用了这个query

<?php 
        $posts = get_posts(array(
              \'post_type\' => \'menu\',
              \'orderby\' => \'name\',
              \'order\' =>  \'DESC\',
              \'taxonomy\' => $wcatTerm->taxonomy,
              \'term\'  => $wcatTerm->slug,
              \'category\' => \'$childcat\',
              \'nopaging\' => true,
              ));
              foreach($posts as $post) {
                  //echo $wsc->name;
              setup_postdata($post); ////set up post data for use in the loop (enables the_title(), etc without specifying a post ID--as referenced in the stackoverflow link above)
            ?>
但我得到的是分类被列为它们应该列的,但是ALL 每个子类别都在重复这些帖子。

正如您在查询中看到的,我已经添加了\'category\' => \'$childcat\', 但我仍然没有得到应用的过滤器,请有人帮助我得到这个。

完整代码

<?php
$wcatTerms = get_terms(\'menu-items\', array(\'hide_empty\' => 0, \'orderby\' => \'ASC\',  \'parent\' =>0));
foreach($wcatTerms as $wcatTerm) : 
    //echo $wcatTerm->name;
    $wsubargs = array(\'hierarchical\'=>1,\'show_option_none\'=>\'\',\'hide_empty\'=>0,\'parent\'=>$wcatTerm->term_id,\'taxonomy\'=>\'menu-items\');
    $wsubcats = get_categories($wsubargs);
    foreach ($wsubcats as $wsc):
        ?><h2><?php //echo $wsc->name; ?></h2>
        <?php $childcat = $wsc->name; 
                $childid = $wsc->ID;
        ?>
        <h2><?php echo $childcat; ?></h2>
        <?php 
        $posts = get_posts(array(
      \'post_type\' => \'menu\',
      \'orderby\' => \'name\',
      \'order\' =>  \'DESC\',

      \'tax_query\' => array(
          array(
              \'taxonomy\' => $wsc->taxonomy,
              \'term\'  => $wsc->term_id,
              \'include_children\' => false,
          )
      ),
      \'nopaging\' => true,
      ));?>
                         <div class="menu-title"><?php the_title(); ?></div>
                         <div class="menu-title"><?php the_content(); ?></div>
                        <?php //endwhile; ?>
            <?php

    endforeach;
endforeach; 
?>

1 个回复
最合适的回答,由SO网友:Pieter Goosen 整理而成

在我开始之前请注意,你不应该称呼你的帖子类型menu, menu 是保留术语,因此您可能会遇到默认内置功能的问题。但是,您可以使用复数形式menus 不必担心引发冲突的问题

至于您的代码,您使用的是无效的参数,它只是被WP_Query 这也正是你无论如何都要回所有帖子的原因。对于自定义分类法,应使用tax_query 查询属于自定义分类术语的帖子

以下操作应该有效:(注意:您只需要输入正确的分类名称和正确的术语IDtax_query 针对您的特定用例

$posts = get_posts(array(
      \'post_type\' => \'menu\',
      \'orderby\' => \'name\',
      \'order\' =>  \'DESC\',
      \'tax_query\' => array(
          array(
              \'taxonomy\' => $wcatTerm->taxonomy,
              \'terms\'  => $wcatTerm->term_id,
              \'include_children\' => false,
          )
      ),
      \'nopaging\' => true,
      ));
编辑类别参数将not 在不使用内置分类法的情况下工作,category. 您使用的是自定义分类法,因此要从特定术语或自定义分类法中的术语查询帖子,您需要使用tax_query 就像我的示例代码一样。

正如我所说的,你需要给correct 您的tax_query 因为我不知道你的确切设置。这个terms 参数(默认情况下)应该是一个整数值或术语ID的整数值数组,所以要考虑到这一点

让我们以代码为例

$wsubcats = get_categories($wsubargs);
    foreach ($wsubcats as $wsc):
在这里面foreach 回路,您的tax_query 数值如下:

您的taxonomy 值将为$wsc->taxonomy

您的terms 值将为(int) $wsc->term_id

    编辑2

    筛选代码时,会出现大量错误。我已经更正了您代码中的所有错误,并在代码中对它们发表了评论,所以请务必检查我的评论。在我声明要删除的地方,请删除该特定代码行,因为它不是必需的。此外,我不知道您是否真的需要空术语,所以我保留了它,只需内置保护,以避免在实际存在空术语时出现错误和错误

    <?php
    $taxonomy = \'menu-items\'; // Do not use hypens in taxonomy names, only use underscores
    $term_args = array(
        \'hide_empty\' => 0, // Why do you need empty terms? 
        \'orderby\' => \'ASC\',  
        \'parent\' => 0
    );
    $wcatTerms = get_terms(
        $taxonomy, // Taxonomy name
        $term_args // Array of arguments to get terms by
    );
    // To avoid bugs and errors, run this only if we have a valid array of terms
    if (    !empty( $wcatTerms ) // Check that we do not have an empty array 
         && !is_wp_error( $wcatTerms ) // Check that we do not have a WP_Error object returned
    ) { 
        // If our conditional check passed, then only run the following code
        foreach($wcatTerms as $wcatTerm) { // Do not use : as it is hard to debug and to read, use old style curlies
            //echo $wcatTerm->name;
            $wsubargs = array(
                //\'hierarchical\'=>1, // This is default, not needed and can be removed
                //\'show_option_none\'=>\'\', // Invalid parameter, where do you get this from, to remove
                \'hide_empty\'=> 0, // Why do you need empty terms?
                \'parent\'=>$wcatTerm->term_id,
            );
            //$wsubcats = get_categories($wsubargs); // Why use get_categories, get_categories uses get_terms, to remove
            $wsubcats = get_terms( 
                $taxonomy, // We have set this earlier
                $wsubargs
            );
            foreach ( $wsubcats as $wsc ) { // Again, do not use : as it is hard to debug
                ?><h2><?php //echo $wsc->name; ?></h2>
                <?php $childcat = $wsc->name; ?>
                <h2><?php echo $childcat; ?></h2>
                <?php 
                   //$posts = get_posts(array( // Do not mess with $posts global, rather use $posts_array, to remove
                $args = array(
                    \'post_type\' => \'menu\', // Really bad name, reserved name in Wordpress
                    \'orderby\' => \'name\',
                    \'order\' =>  \'DESC\',
                    \'tax_query\' => array(
                        array(
                            \'taxonomy\' => $taxonomy, // We have defined this already right at the start
                            \'terms  => $wsc->term_id,
                            \'include_children\' => false,
                        )
                    ),
                    \'nopaging\' => true,
                );
                $posts_array = get_posts( $args );
    
                // You are missing the loop here, that is why you are getting the same stuff over and over again
                if ( $posts_array ) { // Only run this if we actually have posts, if we do not have posts, we will have bugs and errors
                    foreach ( $posts_array as $post ) { // You were missing this
                        setup_postdata( $post ); // VERY VERY IMPORTANT, without this, the template tags like the_content will not work
                        ?>
                                 <div class="menu-title"><?php the_title(); ?></div>
                                 <div class="menu-title"><?php the_content(); ?></div>
    
                    <?php } // endforeach $posts_array
                    wp_reset_postdata(); // VERY IMPORTANT to reset the $post global to main query
                } // endif $posts_array
            } // endforeach $wsubcats
        } // endforeach $wcatTerms
    } // endif check for valid array of terms
    ?>
    

结束

相关推荐

Categories' hierarchy in URL

我目前正在处理的网站中的帖子都有多个层次分类。例如:Source - Books -- Moby Dick -- Sherlock Holmes 永久链接设置为/%category%/%postname%/. 然而,一篇文章的URL并不包括所有的子类别——我得到的只是site.com/source/books/*postname*, 尽管这篇文章在来源上没有分类,但只在书籍+白鲸上。有人能帮我找出如何调整这种行为吗?非常感谢。