Quantcast
Channel: WordPress.org Forums » All Topics
Viewing all articles
Browse latest Browse all 59525

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

$
0
0

Replies: 0

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 all articles
Browse latest Browse all 59525

Trending Articles