WordPress Custom Post Type Getting List of Categories


#1

Hi Friends,

I have custom post type named portfolio with taxonomy. i want to show list of categories of
custom post type “portfolio” on the out of wordpress loop.

Need Help


#2

Here you go:


    <?php $categories = get_categories("taxonomy=yourtaxonomy"); foreach ($categories as $category) : echo '<li>' . $category->name . '</li>'; endforeach; ?>

#3
ThemeProvince said

Here you go:


    <?php $categories = get_categories("taxonomy=yourtaxonomy"); foreach ($categories as $category) : echo '<li>' . $category->cat_title . '</li>'; endforeach; ?>

here is my postypes and taxonomy settings:

register_post_type( 'portfolio' , $args );
		register_taxonomy("headline", array("portfolio"), array("hierarchical" => true, "label" => "News Categories", "singular_label" => "Headline", "rewrite" => true));  

your code not working :frowning:


    <?php $categories = get_categories("taxonomy=portfolio"); foreach ($categories as $category) : echo '' . $category->cat_title . ''; endforeach; ?>

#4

Try this.

<?php
$orderby = 'name';
$show_count = 0; 
$pad_counts = 0;
$hierarchical = 1; 
$taxonomy = 'portfolio';
$title = '';

$args = array(
  'orderby' => $orderby,
  'show_count' => $show_count,
  'pad_counts' => $pad_counts,
  'hierarchical' => $hierarchical,
  'taxonomy' => $taxonomy,
  'title_li' => $title
);
?>
    <?php wp_list_categories($args); ?>

#5
FamousThemes said

Try this.

<?php
$orderby = 'name';
$show_count = 0; 
$pad_counts = 0;
$hierarchical = 1; 
$taxonomy = 'portfolio';
$title = '';

$args = array(
  'orderby' => $orderby,
  'show_count' => $show_count,
  'pad_counts' => $pad_counts,
  'hierarchical' => $hierarchical,
  'taxonomy' => $taxonomy,
  'title_li' => $title
);
?>
    <?php wp_list_categories($args); ?>

Great It works :slight_smile: :smiley: Thankx


#6
webdesignerart said
FamousThemes said

Try this.

<?php
$orderby = 'name';
$show_count = 0; 
$pad_counts = 0;
$hierarchical = 1; 
$taxonomy = 'portfolio';
$title = '';

$args = array(
  'orderby' => $orderby,
  'show_count' => $show_count,
  'pad_counts' => $pad_counts,
  'hierarchical' => $hierarchical,
  'taxonomy' => $taxonomy,
  'title_li' => $title
);
?>
    <?php wp_list_categories($args); ?>

Great It works :slight_smile: :smiley: Thankx

You are welcome :slight_smile:


#7
FamousThemes said
webdesignerart said
FamousThemes said

Try this.

<?php
$orderby = 'name';
$show_count = 0; 
$pad_counts = 0;
$hierarchical = 1; 
$taxonomy = 'portfolio';
$title = '';

$args = array(
  'orderby' => $orderby,
  'show_count' => $show_count,
  'pad_counts' => $pad_counts,
  'hierarchical' => $hierarchical,
  'taxonomy' => $taxonomy,
  'title_li' => $title
);
?>
    <?php wp_list_categories($args); ?>

Great It works :slight_smile: :smiley: Thankx

You are welcome :slight_smile:

can we display post of taxonomy categories :wink:


#8

Friend i need post title as well :frowning:


#9

You will have to make a query

<?php 
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts('post_type=portfolio&order=ASC&paged=' . $paged .'');
?>

<?php if (have_posts()) : ?>

	<?php while (have_posts()) : the_post(); ?> 
	
		

<?php the_title(); ?>

<?php the_content(); ?> <?php endwhile; ?>
<?php next_posts_link('« Older Entries') ?>
<?php previous_posts_link('Newer Entries »') ?>
<?php endif; ?>

I have included also the pagination. I am sure you will need it :slight_smile:

And the title is

<?php the_title(); ?>


#10
FamousThemes said

You will have to make a query


		<?php 
        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
        query_posts('post_type=portfolio&order=ASC&paged=' . $paged .'');
        ?>
        
        <?php if (have_posts()) : ?>
        
			<?php while (have_posts()) : the_post(); ?> 
            
                

<?php the_title(); ?>

<?php the_content(); ?> <?php endwhile; ?>
<?php next_posts_link('« Older Entries') ?>
<?php previous_posts_link('Newer Entries »') ?>
<?php endif; ?>

I have included also the pagination. I am sure you will need it :slight_smile:

And the title is

<?php the_title(); ?>

Thanks :smiley: but the formatting i need is example here:

Taxonomy category (1)

category (1) Posts
category (1) Posts
category (1) Posts

category(2)
category(2) posts
category(2) posts
category(2) posts

means i wants category with its post now i am getting both are seprate


#11

Moved to ‘ThemeForest’.


#12

Friends here is my working that i want to use:

<?php
//get all categories then display all posts in each term
$taxonomy = 'headline';
$param_type = 'headline__in';
$term_args=array(
  'orderby' => 'name',
  'order' => 'ASC'
);
$terms = get_terms($taxonomy,$term_args);
if ($terms) {
  foreach( $terms as $term ) {
    $args=array(
      "$param_type" => array($term->term_id),
      'post_type' => 'post',
      'post_status' => 'publish',
      'posts_per_page' => -1,
      'caller_get_posts'=> 1
      );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {  ?>
      

<?php echo 'Category '.$term->name;?>

    <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
  • <?php the_title(); ?>
  • <?php endwhile; ?>
<?php } } } wp_reset_query(); // Restore global post data stomped by the_post(). ?>

but problem is its outpu result:

Category games

My new post FF Check
dddddaaang
my tset new post

Category Sports

My new post FF Check
dddddaaang
my tset new post

its shows categories nice but associate their post same or wrong.

anyone have an idea!


#13

i Want to do this with custom post type:


-Sub Category 1
--Post 1
--Post 2

-Sub Category 2
--Post 1
--Post 2

-Sub Category 3
--Post 1
--Post 2

<?php
//get all child categories for category 15, then for each child category display the posts
$parent_cat = 1;
$taxonomy = 'headline';
$cat_children = get_term_children( $parent_cat, $taxonomy );

if ($cat_children) {
  echo '

Category 15

'; foreach($cat_children as $category) { $args=array( 'cat' => $category, 'post_type' => 'portfolio', 'post_status' => 'publish', 'posts_per_page' => -1, 'caller_get_posts'=> 1 ); $my_query = null; $my_query = new WP_Query($args); if( $my_query->have_posts() ) { echo 'List of Posts belonging to Category ' . $category; while ($my_query->have_posts()) : $my_query->the_post(); ?>

<?php the_title(); ?>

<?php endwhile; } } } wp_reset_query(); // Restore global post data stomped by the_post(). ?>

its not working with custom post type.


#14

not working? it means that your code is wrong :smiley:

‘cat’ => $category is for CATEGORIES, not for custom taxonomy!!!

read this page! http://codex.wordpress.org/Class_Reference/WP_Query

try this (not tested)

<?php //get all child categories for category 15, then for each child category display the posts
$parent_cat = 1;
$taxonomy = 'headline';
$cat_children = get_term_children( $parent_cat, $taxonomy );

if ($cat_children) {
  echo '<?>Category 15';
    foreach($cat_children as $category) {
    $term = get_term_by( 'id', $category, $taxonomy );
    $args=array(
      'headline' =>  $term->name,
      'post_type' => 'portfolio',
      'post_status' => 'publish',
      'posts_per_page' => -1,
      'ignore_sticky_posts'=> 1
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      echo 'List of Posts belonging to Category ' . $term->name;
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
        

<?php the_title(); ?>

<?php endwhile; } } } wp_reset_query(); // Restore global post data stomped by the_post(). ?>

#15
PrimaThemes said

not working? it means that your code is wrong :smiley:

‘cat’ => $category is for CATEGORIES, not for custom taxonomy!!!

read this page! http://codex.wordpress.org/Class_Reference/WP_Query

try this (not tested)

<?php //get all child categories for category 15, then for each child category display the posts
$parent_cat = 1;
$taxonomy = 'headline';
$cat_children = get_term_children( $parent_cat, $taxonomy );

if ($cat_children) {
  echo '<?>Category 15';
    foreach($cat_children as $category) {
    $term = get_term_by( 'id', $category, $taxonomy );
    $args=array(
      'headline' =>  $term->name,
      'post_type' => 'portfolio',
      'post_status' => 'publish',
      'posts_per_page' => -1,
      'ignore_sticky_posts'=> 1
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      echo 'List of Posts belonging to Category ' . $term->name;
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
        

<?php the_title(); ?>

<?php endwhile; } } } wp_reset_query(); // Restore global post data stomped by the_post(). ?>

not working !!! :frowning:


#16

just an advice from me, please don’t write too many “not working” words here… :smiley:

we are trying to help you and we can not always give you the correct and perfect answer because we don’t see your full code.

we also provide some reference for you, for example read this page carefully…

http://codex.wordpress.org/Class_Reference/WP_Query

you can get all what you need by reading that page…

SO, the most important thing, learn how to debug your PHP code please:wink:

for example, you can add additional code to print your $args value, for example

    $args=array(
      'headline' =>  $term->name,
      'post_type' => 'portfolio',
      'post_status' => 'publish',
      'posts_per_page' => -1,
      'ignore_sticky_posts'=> 1
    );
    print_r($args);

if you get ‘headline’ => empty, then it means there is a problem from the previous code, and it makes WP_Query will not return the right posts.

just my two cents.


#17

My code works, used it hundreds of times. :slight_smile:


#18
ThemeProvince said

My code works, used it hundreds of times. :slight_smile:

+1, but he need more now, to display posts from every custom taxonomy item. :smiley:


#19
PrimaThemes said
ThemeProvince said

My code works, used it hundreds of times. :slight_smile:

+1, but he need more now, to display posts from every custom taxonomy item. :smiley:

I know :slight_smile: I was just defending my snippet library :smiley:


#20
ThemeProvince said

My code works, used it hundreds of times. :slight_smile:

Hy Prima themes, Theme province code works very nice that i asked first time now the situation is change. soorry theme theme province if you feel bad. And Prima Themes thnkx for your suggestion.

But at the end i am stay in Zero! :frowning: