如何在自定义邮政类型存档上按分类进行分组

时间:2017-07-01 作者:user1388610

我一直在努力使用自定义帖子类型的存档模板。我想按一种称为countryname的自定义分类法按字母顺序显示帖子。我尝试了半打以上的例子,最终找到了一个几乎有效的。

这是到目前为止我的代码。当我完成这项工作后,我会尽快完成,但现在我很高兴能在正确的国家获得头衔。

我想要的是这样的

Canada
 - a post 
 - another post
England 
 - this post 
United States
 - short post
 - some post 
 - that post
但现在看起来是这样的:

Canada
 - a post 
 - a post
England 
 - a post 
United States
 - a post
 - a post 
 - a post
我添加了wp\\u reset\\u postdata(),但这也不起作用。有人能帮我解决这个问题吗?问题似乎必须出在查询中,才能返回此分类类别中的每个自定义帖子。

非常感谢。

$taxonomy = array( "name" => \'countryname\' , "slug" => \'countryname\');
$custom_post_type = "tours";

if ( have_posts() )
the_post();
?>

<?php
// Query your specified taxonomy to get, in order, each category
$categories = get_terms($taxonomy[\'name\'], \'orderby=title\');
foreach( $categories as $category ) {
?>

<div id="content">    
<h2 class="page-title">
<?php echo $category->name; ?>
</h2>

<?php
// Setup query to return each custom post within this taxonomy category
$o_queried_posts = get_posts(array(
\'nopaging\' => true,
\'post_type\' => $custom_post_type,
\'taxonomy\' => $category->taxonomy,
\'term\' => $category->slug,
));
?>

<div id=\'archive-content\'>

<?php
// Loop through each custom post type
foreach($o_queried_posts as $o_post) {
?>

<div id="post-<?php the_ID(); ?>">

<h2 class="entry-title"><?php the_title(); ?></h2>


</div><!-- #post -->
<?php wp_reset_postdata();
} // foreach($o_queried_posts as $o_post)  

?>
</div> <!-- archive-content -->
</div> <!-- #content -->
<?php } // foreach( $categories as $category ) 

2 个回复
最合适的回答,由SO网友:Den Isahac 整理而成

你就快到了。自从the_title 在自定义循环中有一个Post template标记,您需要分配全局$post 使用自定义查询中的新数据。

<?php
    global $post; // Access the global $post object.

    // Setup query to return each custom post within this taxonomy category
    $o_queried_posts = get_posts(array(
        \'nopaging\' => true,
        \'post_type\' => $custom_post_type,
        \'taxonomy\' => $category->taxonomy,
        \'term\' => $category->slug,
    ));
?>

<div id=\'archive-content\'>

<?php
// Loop through each custom post type
foreach($o_queried_posts as $post) : 
    setup_postdata($post); // setup post data to use the Post template tags. ?>

    <div id="post-<?php the_ID(); ?>">

       <h2 class="entry-title"><?php the_title(); ?></h2>


    </div><!-- #post -->
<?php endforeach; wp_reset_postdata(); ?>

</div> <!-- archive-content -->

SO网友:Bikash Waiba

Try this:

<?php 
    $taxonomy = array( "name" => \'countryname\' , "slug" => \'countryname\');
    $custom_post_type = "tours";

    if ( have_posts() ):

        // Query your specified taxonomy to get, in order, each category
        $categories = get_terms($taxonomy[\'name\'], \'orderby=title\');
        foreach( $categories as $category ):
        ?>

        <div id="content">    
            <h2 class="page-title">
            <?php echo $category->name; ?>
            </h2>

            <?php
            // Setup query to return each custom post within this taxonomy category
            $args = array(
                 \'post_type\' = $custom_post_type,
                 \'tax_query\' => array(
                        array(
                            \'taxonomy\' => $taxonomy["name"],
                            \'terms\' => $category->slug;
                        )
                    )  
                );

            $query = new WP_Query( $args );
            ?>

            <div id=\'archive-content\'>

                <?php
                while( $query->have_posts() ): $query->the_post();
                ?>

                <div id="post-<?php the_ID(); ?>">

                <h2 class="entry-title"><?php the_title(); ?></h2>


                </div><!-- #post -->
                <?php
                endwhile(); 
                wp_reset_postdata();
                ?>
            </div> <!-- archive-content -->
        </div> <!-- #content -->
        <?php endforeach; // foreach( $categories as $category )
    endif;?> 
结束

相关推荐

如何在自定义邮政类型存档上按分类进行分组 - 小码农CODE - 行之有效找到问题解决它

如何在自定义邮政类型存档上按分类进行分组

时间:2017-07-01 作者:user1388610

我一直在努力使用自定义帖子类型的存档模板。我想按一种称为countryname的自定义分类法按字母顺序显示帖子。我尝试了半打以上的例子,最终找到了一个几乎有效的。

这是到目前为止我的代码。当我完成这项工作后,我会尽快完成,但现在我很高兴能在正确的国家获得头衔。

我想要的是这样的

Canada
 - a post 
 - another post
England 
 - this post 
United States
 - short post
 - some post 
 - that post
但现在看起来是这样的:

Canada
 - a post 
 - a post
England 
 - a post 
United States
 - a post
 - a post 
 - a post
我添加了wp\\u reset\\u postdata(),但这也不起作用。有人能帮我解决这个问题吗?问题似乎必须出在查询中,才能返回此分类类别中的每个自定义帖子。

非常感谢。

$taxonomy = array( "name" => \'countryname\' , "slug" => \'countryname\');
$custom_post_type = "tours";

if ( have_posts() )
the_post();
?>

<?php
// Query your specified taxonomy to get, in order, each category
$categories = get_terms($taxonomy[\'name\'], \'orderby=title\');
foreach( $categories as $category ) {
?>

<div id="content">    
<h2 class="page-title">
<?php echo $category->name; ?>
</h2>

<?php
// Setup query to return each custom post within this taxonomy category
$o_queried_posts = get_posts(array(
\'nopaging\' => true,
\'post_type\' => $custom_post_type,
\'taxonomy\' => $category->taxonomy,
\'term\' => $category->slug,
));
?>

<div id=\'archive-content\'>

<?php
// Loop through each custom post type
foreach($o_queried_posts as $o_post) {
?>

<div id="post-<?php the_ID(); ?>">

<h2 class="entry-title"><?php the_title(); ?></h2>


</div><!-- #post -->
<?php wp_reset_postdata();
} // foreach($o_queried_posts as $o_post)  

?>
</div> <!-- archive-content -->
</div> <!-- #content -->
<?php } // foreach( $categories as $category ) 

2 个回复
最合适的回答,由SO网友:Den Isahac 整理而成

你就快到了。自从the_title 在自定义循环中有一个Post template标记,您需要分配全局$post 使用自定义查询中的新数据。

<?php
    global $post; // Access the global $post object.

    // Setup query to return each custom post within this taxonomy category
    $o_queried_posts = get_posts(array(
        \'nopaging\' => true,
        \'post_type\' => $custom_post_type,
        \'taxonomy\' => $category->taxonomy,
        \'term\' => $category->slug,
    ));
?>

<div id=\'archive-content\'>

<?php
// Loop through each custom post type
foreach($o_queried_posts as $post) : 
    setup_postdata($post); // setup post data to use the Post template tags. ?>

    <div id="post-<?php the_ID(); ?>">

       <h2 class="entry-title"><?php the_title(); ?></h2>


    </div><!-- #post -->
<?php endforeach; wp_reset_postdata(); ?>

</div> <!-- archive-content -->

SO网友:Bikash Waiba

Try this:

<?php 
    $taxonomy = array( "name" => \'countryname\' , "slug" => \'countryname\');
    $custom_post_type = "tours";

    if ( have_posts() ):

        // Query your specified taxonomy to get, in order, each category
        $categories = get_terms($taxonomy[\'name\'], \'orderby=title\');
        foreach( $categories as $category ):
        ?>

        <div id="content">    
            <h2 class="page-title">
            <?php echo $category->name; ?>
            </h2>

            <?php
            // Setup query to return each custom post within this taxonomy category
            $args = array(
                 \'post_type\' = $custom_post_type,
                 \'tax_query\' => array(
                        array(
                            \'taxonomy\' => $taxonomy["name"],
                            \'terms\' => $category->slug;
                        )
                    )  
                );

            $query = new WP_Query( $args );
            ?>

            <div id=\'archive-content\'>

                <?php
                while( $query->have_posts() ): $query->the_post();
                ?>

                <div id="post-<?php the_ID(); ?>">

                <h2 class="entry-title"><?php the_title(); ?></h2>


                </div><!-- #post -->
                <?php
                endwhile(); 
                wp_reset_postdata();
                ?>
            </div> <!-- archive-content -->
        </div> <!-- #content -->
        <?php endforeach; // foreach( $categories as $category )
    endif;?> 

相关推荐