How to show custom taxonomy (categories and tags) in Custom Post Type.

 <?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; ?>


html in which I want them to be 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(); ?>

Comments

Popular posts from this blog

Get Post Data From Simple Post custom shortcode

Top 20 Beautiful Pen For Writing

Fatal error: Maximum execution time of 300 seconds exceeded in C:\xampp\phpMyAdmin\libraries\classes\Dbi\DbiMysqli.php on line 199