I have a custom post type. What I’d like to do is to display the project categories just on top of the projects so visitors could then filter projects accordingly.
In my functions.php
I have:
<?php
require_once('portfolio-type.php');
add_filter('excerpt_length', 'my_excerpt_length');
function my_excerpt_length($length) {
return 25;
}
add_filter('excerpt_more', 'new_excerpt_more');
function new_excerpt_more($text){
return ' ';
}
function portfolio_thumbnail_url($pid){
$image_id = get_post_thumbnail_id($pid);
$image_url = wp_get_attachment_image_src($image_id,'screen-shot');
return $image_url[0];
}
?>
In portfolio-type.php
:
<?php
if ( function_exists( 'add_theme_support' ) ) {
add_theme_support( 'post-thumbnails' );
set_post_thumbnail_size( 270, 170, true ); // Normal post thumbnails
add_image_size( 'screen-shot', 720, 540 ); // Full size screen
}
add_action('init', 'portfolio_register');
function portfolio_register() {
$args = array(
'label' => __('Portfolio'),
'singular_label' => __('Project'),
'public' => true,
'show_ui' => true,
'capability_type' => 'post',
'hierarchical' => false,
'rewrite' => true,
'supports' => array('title', 'editor', 'thumbnail')
);
register_post_type( 'portfolio' , $args );
}
register_taxonomy("project-type", array("portfolio"), array("hierarchical" => true, "label" => "Project Types", "singular_label" => "Project Type", "rewrite" => true));
?>
And finally in my index.php I have:
<!-- Start Projects -->
<div id="posts" class="row isotope">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php
$title= str_ireplace('"', '', trim(get_the_title()));
$desc= str_ireplace('"', '', trim(get_the_content()));
?>
<div class="item post item span4 isotope-item">
<a class="project-wrp fancybox" title="<?=$title?>" rel="lightbox[work]" href="<?php print portfolio_thumbnail_url($post->ID) ?>"><div class="profile-photo"><div class="profile-icon">f</div><?php the_post_thumbnail(array('230','170'),array('alt' => '')); ?> </div>
<div class="project-name"><?php echo $title; ?></div>
<div class="project-client"><?php echo $desc; ?></div>
</a>
</div>
<?php endwhile; endif; ?>
</div>
Remove your code from portfolio-type.php that registers post type and taxonomy (line 9 onwards).
Use the following code (in portfolio-type.php) to register the post type “portfolio”
Use the following code (in portfolio-type.php) to register a taxonomy “portfolio_categories” for the post type “portfolio”, make it hierarchical (like categories)
Then use the following code to retrieve taxonomy terms in template files (like index.php)
Let me know if you need any clarification.
For this portfolio post categories list use:
This code works.