按类别名称和标题对帖子进行排序

时间:2013-11-21 作者:INT

试图找出如何根据类别标题(a–z)输出帖子,其次是类别内帖子的标题:

一个类别,一篇以A开头的帖子,因为我想第二个被输出,来吧,已经输出我了

B类-另一篇以a开头的帖子-麻烦了,我想你明白了

我如何做到这一点?

4 个回复
SO网友:leromt

要按类别对其进行细分,需要循环浏览类别列表,然后查询每个类别:

$categories = get_categories( array (\'orderby\' => \'name\', \'order\' => \'asc\' ) );

foreach ($categories as $category) {

   echo "Category is: $category->name <br/>";

   $catPosts = new WP_Query( array ( \'category_name\' => $category->slug, \'orderby\' => \'title\' ) ); 

   if ( $catPosts->have_posts() ) {

       while ( $catPosts->have_posts() ) {
          $catPosts->the_post();
          echo "<a href=\'the_permalink()\'>the_title()</a>";
       }

       echo "<p><a href=\'category/$category->slug\'>More in this category</a></p>";

   } //end if
  


} //end foreach

wp_reset_postdata();

SO网友:maheshwaghmare

我想你是说GROUP Post 按类别/分类NOT SORT.

这里是一个代码GROUP 通过category/taxonomy

  1. $terms = get_terms( \'my_cat_name\' );cat_name name是分类法的名称,当您这样注册它时:例如。

    register_taxonomy( \'my_cat_name\', array( \'custom_post_name\' ), $args )
    
    在中使用它Query e、 g.:

    $args = array(
       \'post_type\'  => \'custom_post_name\',
       \'my_cat_name\' => $term->slug,
       \'posts_per_page\' => $no_of_posts,
    );
    
    完整代码:

    $terms = get_terms( \'CUSTOM_TAXONOMY_SLUG\' );
    
    if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
    
        $output .= \'<ul class="category-list">\';
        foreach ( $terms as $term ) {
    
            $output .= \'<li class="single-cat">\';
            $output .= \'    <h3>\' . $term->name . \'</h3>\';  //  Taxonomy/Category Name
    
                $args = array(
                    \'post_type\'     => \'POST_TYPE_SLUG\',
                    \'CUSTOM_TAXONOMY_SLUG\' => $term->slug,
                    \'posts_per_page\' => -1,
                );    
    
                $the_query = new WP_Query( $args );
    
                if ( $the_query->have_posts() ) {
                    $output .= \'<ul class="cat-post-list">\';
                    while ( $the_query->have_posts() ) {
                        $the_query->the_post();
                        $output .=\'     <li class="cat-single-post">\';
                        $output .=\'         <h4><a href="\'.get_the_permalink().\'">\' .get_the_title(). \'</a></h4>\';
                        $output .=\'     </li><!-- .cat-single-post -->\';
                    }
                    $output .= \'</ul><!-- .cat-post-list -->\';
                } 
                wp_reset_postdata();
            $output .= \'</li><!-- .single-cat-item -->\';
        }
        $output .= \'</ul><!-- .category-list -->\';
    }
    

SO网友:Chris Pink

扩展Maheshwaghmare的工作;

<? 
$terms = get_terms( \'CUSTOM_TAXONOMY\' );
if ( ! empty( $terms ) ){ ?>

<div class="POST_TYPE_PLURAL">

<?  foreach ( $terms as $term ) {
//print_r($term) // DEBUG;
$term_slug = $term->slug;
$term_name = $term->name;
$term_description = $term->description;
?>

    <div class="POST_TYPE_CATEGORY <?=$term_slug; ?>">

      <h1 class="section-head"> <?=$term_name; ?> </h1>
      <p><?=$term_description; ?> </p>

       <?  
        $args = array(
          \'post_type\'     => \'CUSTOM_POST_TYPE\',
          \'tax_query\' => array(
            array(
              \'taxonomy\' => \'CUSTOM_TAXONOMY\',
              \'field\'    => \'slug\',
              \'terms\'    => $term_slug,
            ),
          ),
           \'posts_per_page\' => -1,
         );    

         $the_query = new WP_Query( $args );

           if ( $the_query->have_posts() ) : ?>
              <? while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

               <div class="POST_TYPE_SINGLE">
                <h3> <? the_title(); ?> </h3>
                <? // MORE TEMPLATING CODE ?>
               </div>

             <?  endwhile;
           endif;
           wp_reset_postdata(); ?>
     </div>
 <?  } // foreach
 } //if terms
 ?>

SO网友:user564285

您可以使用orderby 新的wp_query 实例:

$query = new WP_Query( array ( \'orderby\' => \'title\', \'order\' => \'DESC\' ) );
Soo对于每个类别都使用一个单独的实例。

更多信息请参见:http://codex.wordpress.org/Class_Reference/WP_Query

结束

相关推荐

custom sortable column

我正在尝试在我的时间线自定义帖子中为年份创建一个额外的列。我可以创建额外的列,实际上我可以对它进行排序,它工作正常,但我的所有页面似乎都不工作。这是我用来创建列并使其可排序的所有代码:// Register the column add_filter( \'manage_edit-maryg_timeline_columns\', \'set_custom_edit_date_columns\' ); add_action( \'manage_maryg_timeline_posts_cu