按自定义分类法而不是按时间顺序对自定义POST类型循环进行排序

时间:2014-03-03 作者:N-WS

我只想列出按各自流派(使用下面的代码进行分类)组织的书籍(自定义帖子类型)。我有下面列出的所有正在使用的代码。

我在页面模板中使用以下代码来调用自定义帖子类型“book”:

<?php
    if ( get_query_var(\'paged\') ) { $paged = get_query_var(\'paged\'); } 
    elseif ( get_query_var(\'page\') ) { $paged = get_query_var(\'page\'); } 
    else { $paged = 1; }
    $myposts = array(
    \'post_type\' => \'book\',
    \'posts_per_page\' => \'40\',
    \'orderby\' => \'id\',
    \'order\' => \'ASC\',
    \'paged\'=> $paged
   );
  query_posts($myposts);
?>
这是函数中的自定义post类型原点。php文件:

add_action( \'init\', \'register_cpt_book\' );

function register_cpt_book() {

    $labels = array( 
        \'name\' => _x( \'Books\', \'book\' ),
        \'singular_name\' => _x( \'Book\', \'book\' ),
        \'add_new\' => _x( \'Add New\', \'book\' ),
        \'add_new_item\' => _x( \'Add New Book\', \'book\' ),
        \'edit_item\' => _x( \'Edit Book\', \'book\' ),
        \'new_item\' => _x( \'New Book\', \'book\' ),
        \'view_item\' => _x( \'View Book\', \'book\' ),
        \'search_items\' => _x( \'Search Books\', \'book\' ),
        \'not_found\' => _x( \'No books found\', \'book\' ),
        \'not_found_in_trash\' => _x( \'No books found in Trash\', \'book\' ),
        \'parent_item_colon\' => _x( \'Parent Book:\', \'book\' ),
        \'menu_name\' => _x( \'Books\', \'book\' ),
    );

    $args = array( 
        \'labels\' => $labels,
        \'hierarchical\' => true,
        \'description\' => \'A collection of books\',
        \'supports\' => array( \'title\', \'editor\', \'excerpt\', \'author\',  \'custom-fields\' ),
        \'taxonomies\' => array( \'author\', \'genre\' ),
        \'public\' => true,
        \'show_ui\' => true,
        \'show_in_menu\' => true,
        \'show_in_nav_menus\' => true,
        \'publicly_queryable\' => true,
        \'exclude_from_search\' => false,
        \'has_archive\' => true,
        \'query_var\' => true,
        \'can_export\' => true,
        \'rewrite\' => true,
        \'capability_type\' => \'post\'
    );

    register_post_type( \'book\', $args );

}
下面是函数中的自定义分类“流派”。php:

// Add new taxonomy, make it hierarchical (like categories)
    $labels = array(
        \'name\'              => _x( \'Genres\', \'genre\' ),
        \'singular_name\'     => _x( \'Genre\', \'genre\' ),
        \'search_items\'      => __( \'Search Genres\' ),
        \'all_items\'         => __( \'All Genres\' ),
        \'parent_item\'       => __( \'Parent Genre\' ),
        \'parent_item_colon\' => __( \'Parent Genre:\' ),
        \'edit_item\'         => __( \'Edit Genre\' ),
        \'update_item\'       => __( \'Update Genre\' ),
        \'add_new_item\'      => __( \'Add New Genre\' ),
        \'new_item_name\'     => __( \'New Genre Name\' ),
        \'menu_name\'         => __( \'Genre\' ),
    );

    $args = array(
        \'hierarchical\'      => true,
        \'labels\'            => $labels,
        \'show_ui\'           => true,
        \'show_admin_column\' => true,
        \'query_var\'         => true,
        \'rewrite\'           => array( \'slug\' => \'genre\' ),
    );

    register_taxonomy( \'genre\', array( \'book\' ), $args );
代码工作得很好。正如我所说,我只想列出按各自流派组织的书籍(上述分类法)。

因此,书籍循环将如下所示:

Philosophy

哲学书1、哲学书2、哲学书3

Psychology

心理学第一册,心理学第二册,心理学第三册

History

历史书1、历史书2、历史书3

我在谷歌搜索时尝试了几种方法,但都没有找到任何有效的方法。如果有人能给我一个可行的答案,我将不胜感激。谢谢

1 个回复
SO网友:helgatheviking

结果证明我根本没有做循环的循环。这是几年前的事了。我甚至不知道我是怎么想到这一点的,因为条款/限制和DB之类的东西仍然让我有点不知所措。不管怎样,我希望它能帮助一些人。。。我确信我有一个循环的循环,但这也不难写。

//remove limits from article CPT archive
function kia_archive_limits( $limits ) {

  if( ! is_admin() && ( is_post_type_archive( \'article\' ) ) ) {
     // remove limits
     $limits = \'\';
  }

  return $limits;
}
add_filter(\'post_limits\', \'kia_archive_limits\' );


//complicated queries for articles depending on query var
function kia_article_clauses( $clauses, $wp_query ) {
    global $wpdb;

    if( is_post_type_archive( \'article\' ))  {

        //join term_relationships to posts, and term_relationships to term_taxonomy and term_taxonomy to terms
        $clauses[\'join\'] .= "LEFT OUTER JOIN {$wpdb->term_relationships} ON {$wpdb->posts}.ID={$wpdb->term_relationships}.object_id
LEFT OUTER JOIN {$wpdb->term_taxonomy} USING (term_taxonomy_id)
LEFT OUTER JOIN {$wpdb->terms} USING (term_id)" ;

        //group posts by term name
        $clauses[\'groupby\'] = "object_id";

        //include posts with and without a subject term
        $clauses[\'where\'] .= " AND (taxonomy = \'subject\' OR taxonomy IS NULL)";
        $clauses[\'orderby\']  = "GROUP_CONCAT({$wpdb->terms}.name ORDER BY name ASC) ";

    } 

    return $clauses;
}
add_filter( \'posts_clauses\', \'kia_article_clauses\', 10, 2 );
我的循环代码如下所示:

<div class="column">

<?php

$prev = \'\'; $counter = 1; $column = 1;

while ( have_posts() ) : the_post();  

    $subhead =  array_shift(wp_get_post_terms(get_the_ID(), \'subject\', array("fields" => "names")));
    $subhead = $subhead ? $subhead : __(\'No Subject\',\'peterwade\');

    if($counter >= 20) { 
        //reset the counter
        $counter = 1;

        $column++;

        //what column are we on?
        if($column == 5 ){
            $column = 1; //reset column count
            $hr = \'<hr class="clear"/>\';
        } else { 
            $hr = false; 
        }

        if ($column == 4) { 
            $class = "column last";
        } else {
            $class = "column";
        }

     ?>
    </div><!--.column-->

    <?php if($hr) echo $hr;?>

    <div class="<?php echo $class;?>">

    <h3 class="subhead"><?php printf(\'%s <span class="continued">%s</span>\', $subhead, __(\'continued\'));?></h3>
    <?php 
        }


    if($subhead != $prev) { 
        $counter++; ?>
        <h3 class="subhead"><?php echo $subhead;?></h3>
        <ul>
    <?php } ?>

        <li><a href="<?php echo get_permalink();?>" title="<?php echo __(\'Permalink to\') . \' \' . get_the_title();?>"><?php the_title();?></a></li>
    <?php 

    if($subhead != $prev) { ?>
        </ul>
    <?php }

    $prev = $subhead;
    $counter++;

endwhile; ?>

</div><!--.column-->
现在看看我链接到你的页面上的来源,他似乎又回到了以前的做事方式。。。。又称作一张巨大的桌子。我不能说这是不是因为我的代码停止工作了。

Alternative

这将是一个循环的循环。循环浏览术语,然后查询每个术语中的帖子。您必须进行基准测试,以确定哪种方法更快。我强烈建议将查询存储在某种瞬态中,但现在是吃饭的时候了!

$terms = get_terms( \'genre\' );

if( $terms ):

foreach( $terms as $term ){

    // The Query
    $args = array(
      \'post_type\' => \'book\',
      \'nopaging\' => true,
      \'tax_query\' => array(
         array( 
            \'taxonomy\' => \'genre\',
            \'field\' => \'id\',
            \'terms\' => $term->term_id 
         )
      )    
);

    $the_query = new WP_Query( $args );

    // The Loop
    if ( $the_query->have_posts() ) {
        echo \'<h2>\' . ucwords( $term->name ) . \'</h2>\';
            echo \'<ul>\';
        while ( $the_query->have_posts() ) {
            $the_query->the_post();
            echo \'<li>\' . get_the_title() . \'</li>\';
        }
        echo \'</ul>\';
    } 
}

endif;

/* Restore original Post Data */
wp_reset_postdata();

结束

相关推荐