WordPress.org

Ready to get started?Download WordPress

Forums

Display Custom Post Type with Custom Taxonomies (9 posts)

  1. mrangelmarino
    Member
    Posted 3 years ago #

    Anyone have any idea how to display a list of custom taxonomies registered under a custom post type on the post type archive page?

    Or, how to display the custom taxonomy on the custom post type's single page?

    I'd also like to append the custom taxonomy tags as a div class to each post.

    You can see what's going on here: http://test.njfoodbank.org/events

  2. David
    Member
    Posted 3 years ago #

    are you just wanting to list the tags associated with that post?

  3. mrangelmarino
    Member
    Posted 3 years ago #

    Well, I guess it's three things, really.

    1. List all custom taxonomies as text on the custom post type archive page.

    2. List a post's associated custom taxonomy terms on a custom post single page.

    3. Add a class of the custom taxonomy to the post div class, and add custom taxonomy text to links.

    ****

    To elaborate, I want a list of custom categories at the top of the custom post archive page. But I want to customize their link text because I want to use this jQuery sorting plugin.

    So ideally, the <a href> for each listed category would be a hash tag (#) followed by the taxonomy term, like this:

    <li><a href="#custom-taxonomy-name-one">Custom Taxonomy Name One</a></li>
    <li><a href="#custom-taxonomy-name-two">Custom Taxonomy Name Two</a></li>

    THEN, each post would have the taxonomy term as a class, like this:

    <article class="custom-taxonomy-name-one">
    <!--stuff-->
    </article>
    
    <article class="custom-taxonomy-name-two">
    <!--stuff-->
    </article>

    Does this make sense?

  4. mrangelmarino
    Member
    Posted 3 years ago #

    I'm going to put this here because this is terribly documented, practically un-Googleable. I found a partial solution to adding the custom taxonomy tags to the post classes:

    <?php add_filter( 'post_class', 'mysite_post_class', 10, 3 );
    if( !function_exists( 'mysite_post_class' ) ) {
        /**
         * Append taxonomy terms to post class.
         * @since 2010-07-10
         */
        function mysite_post_class( $classes, $class, $ID ) {
            $taxonomy = 'you_custom_taxonomy_term';
            $terms = get_the_terms( (int) $ID, $taxonomy );
            if( !empty( $terms ) ) {
                foreach( (array) $terms as $order => $term ) {
                    if( !in_array( $term->slug, $classes ) ) {
                        $classes[] = $term->slug;
                    }
                }
            }
            return $classes;
        }
    }?>

    Just add it before the post tags after the loop starts.

    More documentation here: http://davebonds.com/blog/add-css-classes-for-custom-taxonomies-in-wordpress.html

  5. David
    Member
    Posted 3 years ago #

    I tried googling a solution as well, no luck im afraid. If you don't find a solution here maybe try posting this question in the advanced section. http://wordpress.org/support/forum/wp-advanced.

  6. mrangelmarino
    Member
    Posted 3 years ago #

    How does one gain access to the wp-advanced forum? It says you have to be a moderator to post there.

  7. David
    Member
    Posted 3 years ago #

    I just had a read and they say to add the modlook tag to your post, hopefully they'll come along and have a look, soryr I couldn't help, good luck

  8. mrangelmarino
    Member
    Posted 3 years ago #

    Thanks, David!

  9. timfarley
    Member
    Posted 2 years ago #

    Thanks a ton @mrangelmarino - your code worked perfectly for what I wanted to do!

    I just pasted it into functions.php, added the name of my custom taxonomy and bingo, now when I use post_class() I get the tax terms too!

    Cheers.

Topic Closed

This topic has been closed to new replies.

About this Topic