How to call in Custom Post-Type Categories?

Hello there,

I have our portfolio page here: http://www.slarc.com/portfolio-view/control-building-north-dakota-usa/

The orange links below the project title are manually inserted on the page content. Is there a way to call in a the post categories to make this automatic?

The template I’m using doesn’t have our portfolio posts under WP Posts. It’s under a custom post-type titled “Portfolio”.

I can’t find the right code to call it in to the Portfolio post template.

Thanks,
Lorne

You can use get_categories to list custom post categories.
Something like this:


<?php
$args = array(
	'type'                     => 'post',
	'child_of'                 => 0,
	'parent'                   => '',
	'orderby'                  => 'name',
	'order'                    => 'ASC',
	'hide_empty'               => 1,
	'hierarchical'             => 1,
	'exclude'                  => '',
	'include'                  => '',
	'number'                   => '',
	'taxonomy'                 => 'your_custom_taxonomy',
	'pad_counts'               => false );
$categories = get_categories($args);

echo '<ul>';

foreach ($categories as $category) {
    $url = get_term_link($category);?>
    <li><a href="<?php echo $url;?>"><?php echo $category->name; ?></a></li>
<?php
}

echo '</ul>';
?>

Hey Ronalds,

Thanks for the reply. I went ahead and put that into my single-portfolio.php page and changed “your_custom_taxonomy” to “portfolio”.

However it cut off everything on the page from the insertion point of this code down. So needless to say, it didn’t work. What am I missing?

    <div id="post-<?php the_ID(); ?>" <?php post_class('post'); ?>>
      <article class="single-post folio">
        <header class="header-title">
          <h1><?php the_title(); ?></h1>
			<?php the_content(); ?>

            <?php
				$args = array(
					'type'                     => 'post',
					'child_of'                 => 0,
					'parent'                   => '',
					'orderby'                  => 'name',
					'order'                    => 'ASC',
					'hide_empty'               => 1,
					'hierarchical'             => 1,
					'exclude'                  => '',
					'include'                  => '',
					'number'                   => '',
					'taxonomy'                 => 'portfolio',
					'pad_counts'               => false );
				$categories = get_categories($args);
				
					echo '<ul>';
					
						foreach ($categories as $category) {
							$url = get_term_link($category);?>
							<li><a href="<?php echo $url;?>"><?php echo $category->name; ?></a></li>
						<?php
						}
					
					echo '</ul>';
			?>

        </header>
          <div align="center" class="featured-thumbnail no-hover"><?php the_post_thumbnail( 'portfolio-post-thumbnail-xl' ); ?></div>
        <div class="post-content">

            <div id="YouTubeLink">
				<?php $YouTubeLink = get_post_meta($post->ID, 'YouTubeLink', true);
                    //Checking if animation YouTube Link has been included
                        if ($YouTubeLink) { ?>
                        <a href="<?php echo $YouTubeLink; ?>" class="button" title="Project Animation" target="_blank">Watch Project Animation</a>
                    <?php } //if there is no animation associated with project, then display
                    else { ?>
                        <a href="http://www.youtube.com/user/SmithLaRockArch" class="button" title="Project Animation" target="_blank">Watch Our Project Animations</a>
                <?php } ?>
            <div>

          	<?php wp_link_pages('before=<div class="pagination">&after=</div>'); ?>
        </div><!--.post-content-->
      </article>
    </div><!-- #post-## -->

    <div class="clear"></div>

    <div>
      <p>&nbsp;</p>
    </div>

Thanks,
Lorne

Do you have taxonomy portfolio registered with register_taxonomy?
From your question I understood, that portfolio is your custom post type, then custom taxonomy is something else, think of it as portfolio category.
Also, check if your wp-config.php has debug enabled:


define('WP_DEBUG', true);

Thanks for the reply. However that didn’t work? I need to turn on debugging and try to get to the bottom of this. I think it’s a custom post type and not a custom taxonomy. But I didn’t make this template, where do I find that out?

Custom post types are created with register_post_type, usually in theme’s functions.php or in some php file, which is included in functions.php.

Thanks for the prompt reply :slight_smile:

I found this in my theme.init.php file:

/* Portfolio */
function my_post_type_portfolio() {
	register_post_type( 'portfolio',
                array(
				'label' => __('Portfolio'),
				'singular_label' => __('Porfolio Item', 'theme1592'),
				'_builtin' => false,
				'public' => true,
				'show_ui' => true,
				'show_in_nav_menus' => true,
				'hierarchical' => true,
				'capability_type' => 'page',
				'menu_icon' => get_template_directory_uri() . '/includes/images/icon_portfolio.png',
				'rewrite' => array(
					'slug' => 'portfolio-view',
					'with_front' => FALSE,
				),
				'supports' => array(
						'title',
						'editor',
						'thumbnail',
						'excerpt',
						'custom-fields',
						'comments')
					)
				);
	register_taxonomy('portfolio_category', 'portfolio', array('hierarchical' => true, 'label' => 'Portfolio Categories', 'singular_name' => 'Category', "rewrite" => true, "query_var" => true));
}

add_action('init', 'my_post_type_portfolio');


So th actual categories I’m trying to pull are form the custom postype: Portfolio. But the taxonomy for the categories looks like: “portfolio_category”. Is that right? I’ll try messing with it when what you originally posted. If you have any suggestions based on the above code let me know.

Thanks,
Lorne

Ok with this code I got it to work…sort of.

            <?php
                $args = array(
                    'type'                     => 'post',
                    'child_of'                 => 0,
                    'parent'                   => '',
                    'orderby'                  => 'name',
                    'order'                    => 'ASC',
                    'hide_empty'               => 1,
                    'hierarchical'             => 1,
                    'exclude'                  => '',
                    'include'                  => '',
                    'number'                   => '',
                    'taxonomy'                 => 'portfolio_category',
                    'pad_counts'               => false );
                $categories = get_categories($args);

                    echo '<ul>';

                        foreach ($categories as $category) {
                            $url = get_term_link($category);?>
                            <li><a href="<?php echo $url;?>"><?php echo $category->name; ?></a></li>
                        <?php
                        }

                    echo '</ul>';
            ?>

However, it is pulling in ALL CATEGORIES, and not just the active checked ones. I changed hide empty to ‘0’ but didn’t work.

Ideas?

Thanks,
Lorne

I’m not sure, what do you mean by “the active checked ones”, by default there isn’t such option to set category active or not.
Parameter hide_empty should be 1, not to display categories without any portfolios associated with them.

Yea it’s still bringing in ALL categories. See: http://www.slarc.com/portfolio-view/central-control-building-east-texas/

The actual categories selected/check on the protfolio edit post page are: Command & Control, Administration, Blast-Resistant, & On The Boards

I’ve tried the hide_empty at 1 and 0. Both show ALL categories, even the unchecked ones.

OK What I think this code does is pulls in the custom taxonomy categories that are being used SITE-WIDE. I just want the links to show the categories from this post ONLY. So if the category of Warehouse is not checked on this post, then that category is not listed. Make sense?

Right now ALL of them are showing up: http://www.slarc.com/portfolio-view/control-building-north-dakota-usa/

I have tried changing hide_empty to true, false, 0, and 1. I have also tried changing hierarchical to true, false, 0, 1. None of the combinations work. Thoughts?

Ok, sorry, I know I keep posting updates but this is where I stand.

I added a new category for my portfolio titled test. I made sure NO posts were using this category. Then I set my hide_empty => true and hierarchical => false.

It did not show test. So that shows that this get_categories is pulling categories from ALL my posts. I need to get this working for the current portfolio page ONLY. Is that possible?

Thanks,
Lorne

Of course, this gets all custom categories, that are registered to portfolio post type and regular post categories, if those are registered with register_taxonomy_for_object_type.
I think you are confusing checkin/unchecking categories with something else, bulk actions, perhaps.
You can use include parameter, to show only categories in list of comma separated ids.

Well each post has different categories per project. So basically using ids for the include parameter won’t work because it varies.

Is there another command to use in place of get_categories to only be applied for the current post?

You can get portfolio categories with get_the_terms and then pass the list of ids to get_categories in include parameter.

Thanks for the reply,

Get_the_terms worked for me, however it made ALL the terms one large link. So I used get_the_terms_links code and go it to work.

However as with everything else I do in web design I want to through a curveball in the mix.

I am getting the links working on this page for “portfolio_category” here: http://www.slarc.com/portfolio-view/central-control-building-east-texas

However if you look at the link for Administration Buildings it returns this link for the category: http://www.slarc.com/portfolio_category/administration-buildings I have a specific page I want to direct it to: http://www.slarc.com/projects/administration-buildings

How can I do this? The code I’m using now is:

			<?php
                echo '<h3 id="Proj_Categories"><ul>';
                    echo get_the_term_list( $post->ID, 'portfolio_category', '<li>', '</li><li>', '</li>' );
                echo '</ul></h3>';
            ?>

I’d love to add this echo to the code to add the first part of the URL to jump to the specific page and not the category page:

<a href="http://www.slarc.com/projects/<?php echo $portfolio_category; ?>">

Any ideas? Thanks for all the help thus far!
Lorne

edit: FYI, the slugs on my portfolio pages, all match the portfolio_category slugs exactly. So If I can echo that portfolio_category at the end of the

http://www.slarc.com/project/

Well, if you would have checked the documentation, you would have noticed, that get_the_terms return value is array|false|wp_error, meaning, that you have to loop through the array.
That also will allow to put anything in the url.


$terms = get_the_terms($args);
if ($terms && !is_wp_error($terms)) {
    foreach($terms as $term) {
        echo '<a href="http://www.slarc.com/projects/'.$term->slug.'">'.$term->name.'</a>';
    }
}

Thanks for the post. I did read the link you gave me but most of this PHP stuff is way over my head. So I appreciate the help!

I used your code above and got on one taxonomy to show up?

            <?php
				$terms = get_the_terms( $post->ID, 'portfolio_category' );
						
					if ( $terms && ! is_wp_error( $terms ) ) : 

				$portfolio = array();
			
				foreach ( $terms as $term ) {
					$portfolio[] = $term->name;
				}
									
				$portfolio_category = join( " | ", $portfolio );
			?>
			
                <h3 id="Proj_Categories"><ul>
					<?php echo '<a href="http://www.slarc.com/projects/'.$term->slug.'">'.$term->name.'</a>'; ?>
                </ul></h3>
			
			<?php endif; ?>

How can I pull in all the taxonomies??

FYI-Ignore the links on top that show up. These are brough in with the get_the_terms_list

			<?php
                echo '<h3 id="Proj_Categories"><ul>';
                    echo get_the_term_list( $post->ID, 'portfolio_category', '<li>', '</li><li>', '</li>' );
                echo '</ul></h3>';
            ?>

But those need the http:// www. slarc.com/ projects in front of the links.

Thanks,
Lorne

Does anyone know why this is only pulling in one category and not all of them? You’ll see a duplicate link on this page: http://www.slarc.com/portfolio-view/central-control-building-east-texas/

That link is being pulled in by the new code, but only one category when I need all checked categories visible?

    <?php
                $terms = get_the_terms( $post->ID, 'portfolio_category' );

                    if ( $terms && ! is_wp_error( $terms ) ) :

                $portfolio = array();

                foreach ( $terms as $term ) {
                    $portfolio[] = $term->name;
                }

                $portfolio_category = join( " | ", $portfolio );
            ?>

                <h3 id="Proj_Categories"><ul>
                    <?php echo '<a href="http://www.slarc.com/projects/'.$term->slug.'">'.$term->name.'</a>'; ?>
                </ul></h3>

            <?php endif; ?>

Thanks for the help,
Lorne

Just an update…I got the working code thanks to combine efforts from you ronalds and help from jfacemyer at wordpress.stackexchange.com

Here’s the code that works!

            <?php
				$terms = get_the_terms( $post->ID, 'portfolio_category' );
						
					if ( $terms && ! is_wp_error( $terms ) ) :

				$portfolio = array();
			
				foreach ( $terms as $term ) {
					$portfolio[] = $term->name;
				}
									
				$portfolio_category = join( " | ", $portfolio );
			?>

            <h3 id='Proj_Categories'>
			<?php
				$url = site_url();
					foreach ( $terms as $term ) {
						$linklist .= "<li><a href='" . $url . "/projects/" . $term->slug . "'>" . $term->name . "</a></li>\
";
					}
				echo "<ul>" . $linklist . "</ul>";
			?>
            </h3>
			
			<?php endif; ?>