簡體   English   中英

獲取所有類別,然后顯示每個學期中的所有帖子

[英]get all categories then display all posts in each term

我創建了自定義帖子和分類法。 現在我想在以下格式的類別下顯示帖子。

    分類名稱1
  • 發布1
  • 發布2
  • 發布3
  • 全部發布
    分類名稱2
  • 發布1
  • 發布2
  • 發布3
  • 全部發布
    分類名稱4
  • 發布1
  • 發布2
  • 發布3
  • 全部發布

並繼續這樣。 我找到了一個代碼,但是沒有用。

<?php  
$taxonomy = 'category';   
$param_type = 'category__in';
$term_args=array(
    'orderby' => 'name',  
    'order' => 'ASC');
$terms = get_terms($taxonomy,$term_args);
if ($terms) { 
    foreach( $terms as $term ) {
        $args=array(
            "$param_type" => array($term->term_id),
            'post_type' => 'post', 
            'post_status' => 'publish',
            'posts_per_page' => -1,
            'caller_get_posts'=> 1 
        );
        $my_query = null; 
        $my_query = new WP_Query($args);    
        if( $my_query->have_posts() ) {  ?> 
            <div class="category section">  
            <h3><?php echo 'Category '.$term->name;?></h3>  
            <ul><?php  while ($my_query->have_posts()) : $my_query->the_post(); ?>  
                        <li><a href="<?php the_permalink() ?>" rel="bookmark" 
    title="Permanent Link to <?php the_title_attribute(); ?>">
                        <?php the_title(); ?></a></li>
                    <?php endwhile; ?>
            </ul>
        </div>
    <?php} 
}}

wp_reset_query();  // Restore global post data stomped by the_post().?>

所以請幫助我。 如何解決。 謝謝

我假設您正在嘗試從Taxonomy Category獲取任何帖子。

<?php
$cat_terms = get_terms(
                array('category'),
                array(
                        'hide_empty'    => false,
                        'orderby'       => 'name',
                        'order'         => 'ASC',
                        'number'        => 6 //specify yours
                    )
            );

if( $cat_terms ) :

    foreach( $cat_terms as $term ) :

        //var_dump( $term );
        echo '<h3>'. $term->name .'</h3>';

        $args = array(
                'post_type'             => 'post',
                'posts_per_page'        => 10 //specify yours
                'post_status'           => 'publish',
                'tax_query'             => array(
                                            array(
                                                'taxonomy' => 'category',
                                                'field'    => 'slug',
                                                'terms'    => $term->slug,
                                            ),
                                        ),
                'ignore_sticky_posts'   => true //caller_get_posts is deprecated since 3.1
            );
        $_posts = new WP_Query( $args );

        if( $_posts->have_posts() ) :
            while( $_posts->have_posts() ) : $_posts->the_post();

                echo '<h3>'. get_the_title() .'</h3>';

            endwhile;
        endif;
        wp_reset_postdata(); //important


    endforeach;

endif;

請注意,從3.1開始不推薦使用caller_get_posts參數。 請始終向Codex查詢最新的代碼說明,並且不要使用不推薦使用的代碼。

WP_Query() -WordPress Codex

您可以嘗試這樣做,您可能希望將其移動到模板中而不是將其包含在函數中,確保$tax是正確的分類法,如果您不使用本機wordpress,請更改post_type

function get_taxonomy_and_post() {
    $tax = 'category'; // Your Taxonomy, change it if you not using wordpress native category
    $terms = get_terms( $tax ,array( // get all taxonomy terms
        'orderby'    => 'name',
        'order'      => 'ASC',
        'hide_empty' => 0,
    ));

    //Loop throug each taxonomy terms, 
    foreach ( $terms as $term ) {

        //Query argument for post
        $args = array( 
            'post_type' => 'post', // Or Custom Post Type, 
            'order' => 'DESC', 
            'orderby' => 'date',
            'taxonomy' => $tax,
            'term' => $term->slug, // Query posts for each term based on term slug
        );
        $query = new WP_Query( $args ); 
        $posts = $query->get_posts();  
        echo '<div class="category section"><h3>Category '.$term->name.'</h3><ul>';
        if ( $posts ) {
            foreach ( $posts as $post ) {
                echo '<li><a href="'.$post->guid /**use get_permalink( $post->ID ) if you want the custom permalink**/.'">'.$post->post_title.'</a><li>';
            }
        }
        echo '</ul></div>';

    }
}
add_action('wp_head', 'get_taxonomy_and_post');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM