0

I have a website I am setting up for a client of mine which has a portfolio page which you can see here...

http://lenhamsportscars.com/cars-for-sale

You will notice that there is an option at the time to filter by category (Cars For Sale OR Cars Sold). I need to amend the code so it automatically just shows the portfolio items from the category I choose, rather than showing both on the same page.

Here is the current code that will need amending...

    <?php
/*
Template Name: Portfolio Template

*/
?>
<?php get_header(); ?>
<div class="wrapper">
    <div class="portfolio-content"> 
    <h1 class="post-title-page"><?php the_title(); ?></h1>

    <?php 
        $categories=get_categories('taxonomy=section');
       if ( !empty( $categories ) ){
          ?>

      <ul class="portfolio-categ filter">
      <li>view by:</li>
      <li class="all"><a href="#">All</a></li>
         <?php  wp_list_categories('taxonomy=section&title_li=');    ?>
        </ul>

   <?php  } else {    $categories = array(); } ?>

            <?php 
                $count = 0;
            //   custom_query_posts(array('post_type' => 'portfolioitems','posts_per_page'  => 50))
    global $wp_query;
    wp_reset_query();
    $paged = get_query_var('paged') ? get_query_var('paged') : 1;
    $wp_query = new WP_Query(array('post_type' => 'portfolioitems','posts_per_page' => 50,'paged'=> $paged));

            ?>
            <ul class="portfolio-area"> 

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

            <?php   
                    $custom = get_post_custom($post->ID);
                    $bimg = $custom["bimg"][0];
                    $shortxt = $custom["shortxt"][0];

                    $terms = get_the_terms( get_the_ID(), 'section' );
                        ?>      

                    <li class="portfolio-item2" data-id="id-<?php echo $count; ?>" data-type="cat-item-<?php foreach ($terms as $term) { echo $term->term_id; } ?>">    
                    <div>
                   <span class="image-block">
                    <a class="image-zoom" href="<?php the_permalink() ?>" rel="" title="<?php the_title_attribute(); ?>"><?php the_post_thumbnail('portfolio-img', array('alt' => ''.get_the_title().'', 'title' => ''.get_the_title().'')); ?>
                    <span class="mglass"></span>
                    </a>
                    </span>
                   <div class="home-portfolio-text">
                    <h2 class="post-title-portfolio" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
                    <p class="post-subtitle-portfolio"><?php echo $shortxt; ?></p>
                    </div>

                    </div>  
                    </li>               

                <?php $count++;  ?> 
            <?php endwhile; ?> 

            <?php wp_reset_query(); ?>
            <div class="column-clear"></div>
                    </ul><!--end portfolio-area -->
        </div><!--end portfolio-content -->

  <div class="prev-next">
        <?php if(function_exists('pagenavi')) :pagenavi();  ?>


       <?php else : ?>
  <ul class="other-entries">
            <li class="newer-entries"><span><?php previous_posts_link('&laquo; Previous') ?></span></li>
            <li class="older-entries"><span><?php next_posts_link('Next &raquo;') ?></span></li>
          </ul>

<?php endif; ?>

</div>

</div><!-- end wrapper -->    
<?php get_footer(); ?>

I have researched this for days and played around but am not great with code and can't get it to work. Any help would be much appreciated as the client wants this site finished a.s.a.p.

Thanks!

Here is the code for the functions.php file:

<?php
/**
 * @package WordPress
 * @subpackage Granit
 */


// Add custom post type
include("include/portfolio-custom-post-type.php");

// Add latest tweets custom widget
include("include/widget-twitter.php");

// Disable WordPress’s auto-formating filters
include("include/disable-autoformat.php");

if ( !function_exists( 'optionsframework_init' ) ) {

/*-----------------------------------------------------------------------------------*/
/* Options Framework Theme
/*-----------------------------------------------------------------------------------*/

/* Set the file path based on whether the Options Framework Theme is a parent theme or child theme */

if ( STYLESHEETPATH == TEMPLATEPATH ) {
    define('OPTIONS_FRAMEWORK_URL', TEMPLATEPATH . '/admin/');
    define('OPTIONS_FRAMEWORK_DIRECTORY', get_bloginfo('template_directory') . '/admin/');
} else {
    define('OPTIONS_FRAMEWORK_URL', STYLESHEETPATH . '/admin/');
    define('OPTIONS_FRAMEWORK_DIRECTORY', get_bloginfo('stylesheet_directory') . '/admin/');
}

require_once (OPTIONS_FRAMEWORK_URL . 'options-framework.php');

}

if ( function_exists('register_sidebar') ) {
    register_sidebar(array(
        'name' => 'Sidebar',
        'before_widget' => '<li id="%1$s" class="widget %2$s">',
        'after_widget' => '</li>',
        'before_title' => '<h3 class="widgettitle">',
        'after_title' => '</h3>',
    ));
    register_sidebar(array(
        'name' => 'Footer1',
        'before_widget' => '<li id="%1$s" class="widget %2$s">',
        'after_widget' => '</li>',
        'before_title' => '<h5 class="widgettitle2">',
        'after_title' => '</h5>',
    ));
}

// Register Post Thumbnail
add_theme_support( 'post-thumbnails' ); 
set_post_thumbnail_size(605,200,true);
add_image_size( 'blog-img', 605, 200, true );
add_image_size( 'portfolio-img', 225, 140, true );

// Register Custom Menus
add_action('init', 'register_my_menu');
function register_my_menu() {
register_nav_menu('menu-1', __('Menu 1'));
}


function custom_blog_posts(array $query = array())
{
    global $wp_query;
    wp_reset_query();

    $paged = get_query_var('paged') ? get_query_var('paged') : 1;

    $defaults = array(
        'paged'             => $paged,
        );
    $query += $defaults;

    $wp_query = new WP_Query($query);
}



function custom_query_posts(array $query = array())
{
    global $wp_query;
    wp_reset_query();

    $paged = get_query_var('paged') ? get_query_var('paged') : 1;

    $defaults = array(
        'paged'             => $paged
    );
    $query += $defaults;

    $wp_query = new WP_Query($query);
}

/*-----------------------------------------------------------------------------------*/
/*  Get related posts by taxonomy
/*-----------------------------------------------------------------------------------*/

function related_projects_taxonomy($post_id, $taxonomy, $args=array()) {
  $query = new WP_Query();
  $terms = wp_get_object_terms($post_id, $taxonomy);
  if (count($terms)) {
    // Assumes only one term for per post in this taxonomy
    $post_ids = get_objects_in_term($terms[0]->term_id,$taxonomy);
    $post = get_post($post_id);
    $args = wp_parse_args($args,array(
      'post_type' => $post->post_type, // The assumes the post types match
      'post__in' => $post_ids,
      'taxonomy' => $taxonomy,
      'term' => $terms[0]->slug,
      'posts_per_page' => 4
    ));
    $query = new WP_Query($args);
  }
  return $query;
}

function wpe_excerptlength_blog($length) { return 60; }
function wpe_excerptlength_index($length) { return 25;  }
function wpe_excerptmore($more) { return '...'; }
function wpe_excerpt($length_callback='', $more_callback='') {
    global $post;
    if(function_exists($length_callback)){
    add_filter('excerpt_length', $length_callback);
    }
    if(function_exists($more_callback)){
    add_filter('excerpt_more', $more_callback);
    }
    $output = get_the_excerpt();
    $output = apply_filters('wptexturize', $output);
    $output = apply_filters('convert_chars', $output);
    $output = '<p>'.$output.'</p>';
    echo $output;
    }

function mytheme_comment($comment, $args, $depth) {
   $GLOBALS['comment'] = $comment; ?>
   <li <?php comment_class(); ?> id="li-comment-<?php comment_ID() ?>">
     <div id="comment-<?php comment_ID(); ?>">
      <div class="comment-author vcard comment-left">
         <?php echo get_avatar($comment,$size='30',$default=''); ?>
</div>
        <div class="comment-right">
         <?php printf(__('<cite class="fn">%s</cite>'), get_comment_author_link()) ?>

      <?php if ($comment->comment_approved == '0') : ?>
         <em><?php _e('Your comment is awaiting moderation.') ?></em>
         <br />
      <?php endif; ?>

      <div class="comment-meta commentmetadata"><?php printf(__('%1$s at %2$s'), get_comment_date(),  get_comment_time()) ?> <?php edit_comment_link(__('(Edit)'),'  ','') ?></div>

      <?php comment_text() ?>
    </div>
    <div class="column-clear"></div>

      <div class="reply">
         <?php comment_reply_link(array_merge( $args, array('depth' => $depth, 'max_depth' => $args['max_depth']))) ?>
      </div>

     </div><!-- end comment -->
<?php } 

function pagenavi( $p = 2 ) { // pages will be show before and after current page
  if ( is_singular() ) return; // don't show in single page
  global $wp_query, $paged;
  $max_page = $wp_query->max_num_pages;
  if ( $max_page == 1 ) return; // don't show when only one page
  if ( empty( $paged ) ) $paged = 1;
  echo '<span class="pages">Page: ' . $paged . ' of ' . $max_page . ' </span> '; // pages
  if ( $paged > $p + 1 ) p_link( 1, 'First' );
  if ( $paged > $p + 2 ) echo '... ';
  for( $i = $paged - $p; $i <= $paged + $p; $i++ ) { // Middle pages
    if ( $i > 0 && $i <= $max_page ) $i == $paged ? print "<span class='page-numbers current'>{$i}</span> " : p_link( $i );
  }
  if ( $paged < $max_page - $p - 1 ) echo '... ';
  if ( $paged < $max_page - $p ) p_link( $max_page, 'Last' );
}
function p_link( $i, $title = '' ) {
  if ( $title == '' ) $title = "Page {$i}";
  echo "<a class='page-numbers' href='", esc_html( get_pagenum_link( $i ) ), "' title='{$title}'>{$i}</a> ";
}
?>

This is the code for includes/portfolio-custom-post-type.php...

<?php
add_action('init', 'buildPortfolio');
function buildPortfolio() {

        $labels = array(
        'name' => _x('Our Portfolio', 'post type general name'),
        'singular_name' => _x('Portfolio Item', 'post type singular name'),
        'add_new' => _x('Add New Item', 'portfolio item'),
        'add_new_item' => __('Add New Portfolio Item'),
        'edit_item' => __('Edit Portfolio Item'),
        'new_item' => __('New Portfolio Item'),
        'view_item' => __('View Portfolio Item'),
        'search_items' => __('Search Portfolio'),
        'not_found' =>  __('Nothing found'),
        'not_found_in_trash' => __('Nothing found in Trash'),
        'parent_item_colon' => ''
        );

        $portfolio_args = array(
            'labels' => $labels,
            'label' => __('Portfolio2'),
            'singular_label' => __('Portfolio3'),
            'public' => true,
            'show_ui' => true,
            'capability_type' => 'post',
            'hierarchical' => false,
            'rewrite' => true,
            'supports' => array('title', 'editor', 'thumbnail', 'excerpt')
        );
        register_post_type('portfolioitems',$portfolio_args);

         $labelsPortfolioSection = array(
    'name' => _x( 'Sections', 'taxonomy general name' ),
    'singular_name' => _x( 'Section', 'taxonomy singular name' ),
    'search_items' =>  __( 'Search Sections' ),
    'all_items' => __( 'All Sections' ),
    'parent_item' => __( 'Parent Section' ),
    'parent_item_colon' => __( 'Parent Section:' ),
    'edit_item' => __( 'Edit Section' ),
    'update_item' => __( 'Update Section' ),
    'add_new_item' => __( 'Add New Section' ),
    'new_item_name' => __( 'New Section Name' ),
  );    

  register_taxonomy('section','portfolioitems',array(
    'hierarchical' => true,
    'labels' => $labelsPortfolioSection
  ));

}

add_action("admin_init", "addPortfolio");
add_action('save_post', 'updateDetails');

function addPortfolio(){
    add_meta_box("portfolio_details", "Portfolio Options", "portfolioOptions", "portfolioitems", "normal", "low");
}

function portfolioOptions(){
    global $post;
    $custom = get_post_custom($post->ID);
    $shortxt = $custom["shortxt"][0];
    $bimg = $custom["bimg"][0];
    $year = $custom["year"][0];
    $client = $custom["client"][0];
    $performance = $custom["performance"][0];
    $lnk = $custom["lnk"][0];


?>

    <style>
        #portfolio-options label { font-size: 12px; margin: 0 15px 0 0; }
        #portfolio-options input { border: 1px #ddd solid; padding: 8px; width: 300px; }
        .opt2{margin-top:10px;}
    </style>

    <div id="portfolio-options">
        <div><label>Short Text:</label><input name="shortxt" value="<?php echo $shortxt; ?>" /></div>       
        <div class="opt2"><label>Big Image URL:</label><input name="bimg" value="<?php echo $bimg; ?>" /></div> 
        <div class="opt2"><label>Project Year:</label><input name="year" value="<?php echo $year; ?>" /></div>      
        <div class="opt2"><label>Client Name:</label><input name="client" value="<?php echo $client; ?>" /></div>       
        <div class="opt2"><label>Performance:</label><input name="performance" value="<?php echo $performance; ?>" /></div>     
        <div class="opt2"><label>Site Link:</label><input name="lnk" value="<?php echo $lnk; ?>" /></div>   


    </div><!--end portfolio-options-->   

<?php

}
function updateDetails(){
    global $post;
    update_post_meta($post->ID, "shortxt", $_POST["shortxt"]);
    update_post_meta($post->ID, "bimg", $_POST["bimg"]);
    update_post_meta($post->ID, "year", $_POST["year"]);
    update_post_meta($post->ID, "client", $_POST["client"]);
    update_post_meta($post->ID, "performance", $_POST["performance"]);
    update_post_meta($post->ID, "lnk", $_POST["lnk"]);

} 

add_filter("manage_edit-portfolio_columns", "portfolio_edit_columns");

function portfolio_edit_columns($portfolio_columns){
    $portfolio_columns = array(
        "cb" => "<input type=\"checkbox\" />",
        "title" => "Project Title",
    );
    return $portfolio_columns;
}
?>
0

Depending on whether you intend to use this template to display other categories elsewhere, you may need a copy of the Portfolio Template file, named for this category.

To select a single category for display, from post type 'portfolioitems', modify the following line:

$wp_query = new WP_Query(array('post_type' => 'portfolioitems','posts_per_page' => 50,'paged'=> $paged));

To:

$wp_query = new WP_Query(array('post_type' => 'portfolioitems', 'cat=4', 'posts_per_page' => 50,'paged'=> $paged));

This assumes the ID for your desired category is 4. Simply adding an extra key/value to the WP_Query array will include that filter and exclude any post that does not match the extra criteria.

You can filter category by name, ID, etc. The various methods available are detailed here.
WP Codex ref for category parameters.

** EDIT to taxonomy query ** Based on this additional information, you appear to need a taxonomy filter specific to the custom post type 'portfolioitems'.

$wp_query = new WP_Query(array('post_type' => 'portfolioitems', 'section' => 'sectionname', 'posts_per_page' => 50,'paged'=> $paged));

Replace 'sectionname' with the name of your desired section. Full details on taxonomy parameters

  • Hi I have tried this, with no luck. The portfolio items are grouped into what's called 'sections' as apposed to 'categories' which you would group posts into. Does this make a difference? – Jamie Ludlow Jul 23 '14 at 13:49
  • I would need to see how the portfolio items are defined in the theme. Looks like you are using the 'Plixus' theme? matchthemes.com/demowp/plixus/portfolio – jdm2112 Jul 23 '14 at 13:53
  • Yes it is a Plixus theme. How do I show you this or give you access without giving everyone access to the site? – Jamie Ludlow Jul 23 '14 at 13:54
  • Must admit I thought it would be something to do with this... <li class="portfolio-item2" data-id="id-<?php echo $count; ?>" data-type="cat-item-<?php foreach ($terms as $term) { echo $term->term_id; } ?>"> – Jamie Ludlow Jul 23 '14 at 13:58
  • 1
    Thank you so much for your help, yes that worked perfect!! You are a life saver. – Jamie Ludlow Jul 23 '14 at 14:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.