Support » Developing with WordPress » How to show custom taxonomy (categories and tags) in Custom Post Type.

  • Resolved kirasiris

    (@kirasiris)


    Hello, I just discovered how to do custom post types and custom taxonomies.(I love WordPress)

    Everything is working so damn well, I mean I publish new stuff and the corresponding post is shown as it is supposed to do, now the problem is that their custom categories and/or tags are not shown, how can I fix this?. Can you guys help me with this?

    This is my custom post type (Portfolio; Administrative Top Menu):

    
    <?php
    function custom_portfolio_type() {
    // Set UI labels for Custom Post Type
        $labels = array(
            'name'                => _x( 'Portfolios', 'Post Type General Name', 'modernizer' ),
            'singular_name'       => _x( 'Portfolio', 'Post Type Singular Name', 'modernizer' ),
            'menu_name'           => __( 'Portfolios', 'modernizer' ),
            'parent_item_colon'   => __( 'Parent Movie', 'modernizer' ),
            'all_items'           => __( 'All Portfolios', 'modernizer' ),
            'view_item'           => __( 'View Portfolio', 'modernizer' ),
            'add_new_item'        => __( 'Add New Portfolio', 'modernizer' ),
            'add_new'             => __( 'Add Portfolio', 'modernizer' ),
            'edit_item'           => __( 'Edit Portfolio', 'modernizer' ),
            'update_item'         => __( 'Update Portfolio', 'modernizer' ),
            'search_items'        => __( 'Search Portfolio', 'modernizer' ),
            'not_found'           => __( 'Not Found', 'modernizer' ),
            'not_found_in_trash'  => __( 'Not found in Trash', 'modernizer' ),
        );
         
    // Set other options for Custom Post Type
         
        $args = array(
            'label'               => __( 'portfolios', 'modernizer' ),
            'description'         => __( 'Portfolio Templates and Snippets', 'modernizer' ),
            'labels'              => $labels,
            // Features this CPT supports in Post Editor
            'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
            // You can associate this CPT with a taxonomy or custom taxonomy. 
            'taxonomies'          => array( '' ),
            /* A hierarchical CPT is like Pages and can have
            * Parent and child items. A non-hierarchical CPT
            * is like Posts.
            */ 
            'hierarchical'        => false,
            'public'              => true,
            'show_ui'             => true,
            'show_in_menu'        => true,
            'show_in_nav_menus'   => true,
            'show_in_admin_bar'   => true,
            'menu_position'       => 5,
            'can_export'          => true,
            'has_archive'         => true,
            'exclude_from_search' => false,
            'publicly_queryable'  => true,
            'capability_type'     => 'page',
        );
         
        // Registering your Custom Post Type
        register_post_type( 'portfolios', $args );
     
    }
     
    /* Hook into the 'init' action so that the function
    * Containing our post type registration is not 
    * unnecessarily executed. 
    */
     
    add_action( 'init', 'custom_portfolio_type', 0 );
    ?>
    

    This is my custom taxonomy:

    
    <?php
    //========================= Categories for Portfolio Custom Type ===========================//
    //create a custom taxonomy name it topics for your posts
     
    function portfolio_categories_taxonomy() {
     
    // Add new taxonomy, make it hierarchical like categories
    //first do the translations part for GUI
     
      $portfolio_cats = array(
        'name' => _x( 'Portfolio Categories', 'taxonomy general name' ),
        'singular_name' => _x( 'Portfolio Category', 'taxonomy singular name' ),
        'search_items' =>  __( 'Search Portfolio Categories' ),
        'all_items' => __( 'All Portfolio Categories' ),
        'parent_item' => __( 'Parent Portfolio Category' ),
        'parent_item_colon' => __( 'Parent Portfolio Category:' ),
        'edit_item' => __( 'Edit Portfolio Category' ), 
        'update_item' => __( 'Update Portfolio Category' ),
        'add_new_item' => __( 'Add New Portfolio Category' ),
        'new_item_name' => __( 'New Portfolio Category' ),
        'menu_name' => __( 'Portfolio Categories' ),
      );    
     
    // Now register the taxonomy. Replace the parameter portfolios withing the array by the name of your custom post type.
      register_taxonomy('portfolio_categories',array('portfolios'), array(
        'hierarchical' => true,
        'labels' => $portfolio_cats,
        'show_ui' => true,
        'show_admin_column' => true,
        'query_var' => true,
        'rewrite' => array( 'slug' => 'portfolio categories' ),
      ));
     
    }
    add_action( 'init', 'portfolio_categories_taxonomy', 0 );
    //========================= Tags for Portfolio Custom Type ===========================//
    
    //hook into the init action and call create_topics_nonhierarchical_taxonomy when it fires
    function portfolio_tags_taxonomy() { 
    // Labels part for the GUI
     
      $portfolio_tags = array(
        'name' => _x( 'Portfolio Tags', 'taxonomy general name' ),
        'singular_name' => _x( 'Portfolio Tag', 'taxonomy singular name' ),
        'search_items' =>  __( 'Search Portfolio Tags' ),
        'popular_items' => __( 'Popular Portfolio Tags' ),
        'all_items' => __( 'All Portfolio Tags' ),
        'parent_item' => null,
        'parent_item_colon' => null,
        'edit_item' => __( 'Edit Portfolio Tag' ), 
        'update_item' => __( 'Update Portfolio Tag' ),
        'add_new_item' => __( 'Add New Portfolio Tag' ),
        'new_item_name' => __( 'New Portfolio Tag Name' ),
        'separate_items_with_commas' => __( 'Separate portfolio tags with commas' ),
        'add_or_remove_items' => __( 'Add or remove portfolio tags' ),
        'choose_from_most_used' => __( 'Choose from the most used portfolio tags' ),
        'menu_name' => __( 'Portfolio Tags' ),
      );
     
    // Now register the non-hierarchical taxonomy like tag. . Replace the parameter portfolios withing the array by the name of your custom post type.
      register_taxonomy('portfolio_tags','portfolios',array(
        'hierarchical' => false,
        'labels' => $portfolio_tags,
        'show_ui' => true,
        'show_admin_column' => true,
        'update_count_callback' => '_update_post_term_count',
        'query_var' => true,
        'rewrite' => array( 'slug' => 'portfolio tags' ),
      ));
    }
    add_action( 'init', 'portfolio_tags_taxonomy', 0 );
    ?>
    

    and finally this is the html in which I want them to be shown:

    
    <?php if(is_single()) : ?>
    <div class="well hidden-xs" id="sidebar">
    <h4 id="sidebarh4">Informacion:</h4>
    <i class="fa fa-user"></i> <a href="<?php echo get_author_posts_url(get_the_author_meta('ID')); ?>"><?php the_author(); ?></a><br>
    <i class="fa fa-folder-open"></i> <?php the_category(','); ?><br>
    <i class="fa fa-comments" aria-hidden="true"></i> <?php comments_number( 'no responses', 'one response', '% responses' ); ?><br>
    <i class="fa fa-eye" aria-hidden="true"></i> <?php echo getPostViews(get_the_ID()); ?><br>
    <i class="fa fa-clock-o"></i> Publicado: <?php the_time('M j, Y'); ?><br>
    <i class="fa fa-pencil-square-o" aria-hidden="true"></i> Ultima edicion: <?php the_modified_date('F j, Y'); ?><br>
    </div>
    <?php endif; ?>
    
Viewing 5 replies - 1 through 5 (of 5 total)
  • Hello @kirasiris,

    If you want to display list of custom taxonomies that are related to particular post on your single page, you can try with below code :

    <php 
    global $post;
    $terms = wp_get_post_terms($post->ID, 'portfolio_tags');
    if ($terms) {
        $output = array();
        foreach ($terms as $term) {
            $output[] = '<a>slug .'" href="' .get_term_link( $term->slug, 'portfolio_tags') .'">' .$term->name .'</a>';
        }
        echo join( ', ', $output );
    }
    ?>

    Let me know if this works for you.

    [Moderator Note: Code fixed. To ensure your code is usable by others, always enclose your code in `backticks`, or use the ‘code’ button.]

    • This reply was modified 4 years, 8 months ago by anurag.deshmukh.
    • This reply was modified 4 years, 8 months ago by anurag.deshmukh.
    • This reply was modified 4 years, 8 months ago by bcworkz.

    Hello,
    You will get your solution from the below link.
    https://wordpress.stackexchange.com/questions/84921/how-do-i-query-a-custom-post-type-with-a-custom-taxonomy
    Let me know if it works for your site.
    One Question, where you want to show this, archive page or single page.
    thanks.

    Thread Starter kirasiris

    (@kirasiris)

    Hello Themes Grove, I followed the steps in the link that you pot on your answer but somehow I’m still unabel to make it work.
    This is the code that I have, but in this way, neither the Portfolios or Cat/tags are shown:

    
    <?php 
    /*
    Template Name: Portfolio
    Template Post Type: page
    */ ?>
    <?php get_header();?>
    <?php include("includes/articleheader.php"); ?>
        <div>
            <div>
            <!-- Filter -->
            <div class="mbr-gallery-filter container gallery-filter-active">
            <ul buttons="0">
            <li class="mbr-gallery-filter-all"><a class="btn btn-md btn-primary-outline active display-4" href="">All</a></li>
            </ul>
            </div>
            <!-- Gallery -->
            <div class="mbr-gallery-row">
            <div class="mbr-gallery-layout-default">
            <div>
            <div>
    		<?php
            $portfolio_query = new WP_Query(array(
                'post_type' => 'portfolios',
                'order' => 'DESC',
    'tax_query' => array(
    array(
    'taxonomy' => 'portfolio_categories',   // taxonomy name
    'field' => 'portfolio categories',           // term_id, slug or name
    'terms' => 'portfolio-category',                  // term id, term slug or term name
    )
    )
            ))
            ?>
            <?php  if($portfolio_query->have_posts()) : while ($portfolio_query->have_posts()) : $portfolio_query->the_post(); ?>
            
            <div class="mbr-gallery-item mbr-gallery-item--p0" data-video-url="false" data-tags="<?php the_category(',') ?>">
            <div href="#lb-gallery3-2a" data-slide-to="0" data-toggle="modal">
            <?php if(has_post_thumbnail()) : ?>
            <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); ?></a>
            <?php endif; ?>
            <span class="icon-focus"></span>
            </div>
            </div>
    		
    		<?php endwhile; ?>
            <?php else : ?>
            <div class="alert alert-danger">No Portfolio Found</div>
            <?php endif; ?>
            </div>
            </div>
            <div class="clearfix"></div>
            </div>
            </div>
        </div>
    <?php get_footer(); ?>
    

    The only way to at least show the portfolios is by commenting the following; otherwise the custom page just show the no portfolio found:

    
    /*'tax_query' => array(
    array(
    'taxonomy' => 'portfolio_categories',   // taxonomy name
    'field' => 'portfolio categories',           // term_id, slug or name
    'terms' => 'portfolio-category',                  // term id, term slug or term name
    )
    )*/
    

    I would like to say that I’m using a custom page.

    http://demo3pro.blogpersonal.net/portfolio/

    Hello, You have to set your custom post type taxonomy name portfolio_categories in this way. then you will get result.
    thanks.

    Thread Starter kirasiris

    (@kirasiris)

    Yes, thank you I already fixed it 🙂

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘How to show custom taxonomy (categories and tags) in Custom Post Type.’ is closed to new replies.