简体   繁体   中英

How To Display Categories and the posts inside of a Custom Post Type

I need some help here as I've exhausted every place I can trying to find information. This is what I'm trying to do:

  1. I have created a custom Post type in my admin called "Classes"
  2. That works fine, the data works great and it's inputting in the admin.
  3. I want to make a custom template to show this custom post type. However, everything I try it's not displaying properly. I've tried many code variations.

I know someones already done this and has the block of code to display this. This is what I need the code to do:

  1. List All categories in my custom post type 'classes'
  2. List all posts (show all content, not a link or excerpt) inside of each category.
  3. Display it as such (I'm using Jquery Accordion)

    • the_category()

    • the_title()
    • the_content()

========================================================

By the way, Here is the block of code I'm currently using. It does work, but it ONLY shows the posts, all of them. It does not show the category with posts inside of them.

<?php
    $type = 'classes';
    $args = array (
     'post_type' => $type,
     'post_status' => 'publish',
     'paged' => $paged,
     'posts_per_page' => 10,
     'ignore_sticky_posts'=> 1
    );
    $temp = $wp_query; // assign ordinal query to temp variable for later use
    $wp_query = null;
    $wp_query = new WP_Query($args);
    if ( $wp_query->have_posts() ) :
        while ( $wp_query->have_posts() ) : $wp_query->the_post();
            echo '<h3 class="acc1">';
            the_title();
            echo '</h3>';
            echo '<div class="sc"><div class="vs"><a href="/schedule" class="reg-but">View Schedule</a></div>';
            the_content();
            echo '</div>';
        endwhile;
    else :
        echo '<h2>Not Found</h2>';
        get_search_form();
    endif;
    $wp_query = $temp;
    ?>

Community, I need you. Please give your feedback!

What you want to do is actually start with a category query. You have to make sure you query all your categories with your custom post type:

Then for each category you would do pretty much what you have above.

    $taxonomy = 'classes';
    $args = array('hide_empty' => false,);
    $terms = get_terms( $taxonomy, $args );

    foreach($terms as $val) {
        $term_id = $val->term_id;
        $term_name = $val->name; 
// now do post query
}

You most likely would have to display the category name as a header for your accordion as well.

Here's all the args for get_terms: http://codex.wordpress.org/Function_Reference/get_terms

For that query you also most likely have to use a Simple Taxonomy Query (search for that on the page). http://codex.wordpress.org/Class_Reference/WP_Query

By adding this arg to your above query:

'tax_query' => 
    array(
        'taxonomy' => 'category',
        'field' => 'slug',
        'terms' => array( $term_name )
    )

Is that what you were looking for?

There might be a better way to do this but I just had to recently do this and did pretty much what I just outlined here.

I should have been more clear and said to put the posts query within the foreach of the terms query.

Here's the updated answer based on your last reply (I have not tested this).

<?php 
    $taxonomy = 'classes';
    $args = array('hide_empty' => false,);
    $terms = get_terms( $taxonomy, $args );


    foreach($terms as $val) {
        $term_id = $val->term_id;
        $term_name = $val->name; 

        $type = 'classes';
        $args = array (
         'post_type' => $type,
         'post_status' => 'publish',
         'paged' => $paged,
         'posts_per_page' => 10,
         'ignore_sticky_posts'=> 1,
         'tax_query' => 
            array(
                'taxonomy' => 'category',
                'field' => 'slug',
                'terms' => array( $term_name )
            )
        );
        $temp = $wp_query; // assign ordinal query to temp variable for later use  
        $wp_query = null;
        $wp_query = new WP_Query($args); 

        if ( $wp_query->have_posts() ) :
            while ( $wp_query->have_posts() ) : $wp_query->the_post();
                echo '<h3 class="acc1">'; 
                the_title();
                echo '</h3>'; 
                echo '<div class="sc"><div class="vs"><a href="/schedule" class="reg-but">View Schedule</a></div>';
                the_content();
                echo '</div>';
            endwhile;
        else :
            echo '<h2>Not Found</h2>';
            get_search_form();
        endif;
        $wp_query = $temp;
  }
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM