Skip to content

Instantly share code, notes, and snippets.

@rtthemes
Created January 24, 2018 23:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rtthemes/411d5dcd92e76666cf64d10b8ef1d280 to your computer and use it in GitHub Desktop.
Save rtthemes/411d5dcd92e76666cf64d10b8ef1d280 to your computer and use it in GitHub Desktop.
custom portfolio filter nav for RT-18
function rt_portfolio( $atts, $content = null ) {
//[portfolio_box id="" item_width="5" pagination="" portf_list_orderby="" portf_list_order="" item_per_page="9" filterable="" categories="" display_descriptions ="" display_titles ="" display_embedded_titles =""]
global $rt_item_width, $post, $wp_query, $rt_display_descriptions, $rt_display_titles, $rt_display_embedded_titles, $paged;
$sortNavigation = $filter_holder = "";
//counter
$counter = 1;
//defaults
extract(shortcode_atts(array(
"id" => 'portfolio-'.rand(100000, 1000000),
"item_width" => 4,
"pagination" => "",
"portf_list_orderby" => "date",
"portf_list_order" => "DESC",
"item_per_page"=> 9,
"filterable" => "false",
"categories" => "",
"display_descriptions" => true,
"display_titles" => true,
"display_embedded_titles" => false
), $atts));
//item width
$item_width = ! empty( $item_width ) ? $item_width : 4;
//pagination
$pagination = $pagination == "false" ? false : $pagination;
//filters
$filterable = $filterable == "false" ? false : $filterable;
//displays
$display_embedded_titles = $display_embedded_titles == "false" ? "" : $display_embedded_titles;
$display_titles = $display_titles == "false" ? "" : $display_titles;
$display_descriptions = $display_descriptions == "false" ? "" : $display_descriptions;
//categories - turn into an array
$categories = ! empty( $categories ) ? explode(",", $categories) : "";
//global values for portfolio items
$rt_item_width = $item_width;
$rt_display_descriptions = $display_descriptions;
$rt_display_titles = $display_titles;
$rt_display_embedded_titles = $display_embedded_titles;
//add scripts and nav if filterable
if( ! empty( $filterable ) ){
wp_enqueue_script('isotope-js', RT_THEMEURI . '/js/jquery.isotope.min.js', array(), "", true );
wp_enqueue_script('rt-run-isotope', RT_THEMEURI . '/js/rt_isotope.js', array(), "", true );
//js script to run
printf('
<script type="text/javascript">
/* <![CDATA[ */
// run isotope
jQuery(window).load(function() {
jQuery("#%s").rt_run_isotope_custom();
});
/* ]]> */
</script>
',$id);
if( ! empty( $categories ) ){
if(is_array($categories)){
foreach ($categories as $arrayorder => $termID) {
$sortCategories = get_term_by('id', $termID, 'portfolio_categories');
$sortNavigation .= '<li><a href="#" data-filter=".filter-'.$sortCategories->term_taxonomy_id.'">'.$sortCategories->name.'</a></li>';
}
}
}else{
$sortCategories = get_terms( 'portfolio_categories', 'orderby=name&hide_empty=1&order=ASC' );
$sortCategories = is_array($sortCategories) ? $sortCategories : "";
foreach ($sortCategories as $key => $term) {
$sortNavigation .= '<li><a data-filter=".filter-'.$term->term_taxonomy_id.'">'.$term->name.'</a></li>';
}
}
$filter_holder = ! empty( $sortNavigation ) ? sprintf('<div class="filter-holder"><ul class="filter_navigation">%s %s</ul></div>','',$sortNavigation) : "";
}
//layout name values
$layout_values = array('','one', 'two', 'three', 'four', 'five');
//paged
if( $pagination ){
if (get_query_var('paged') ) {$paged = get_query_var('paged');} elseif ( get_query_var('page') ) {$paged = get_query_var('page');} else {$paged = 1;}
}else{
$paged=0;
}
//create a post status array
$post_status = is_user_logged_in() ? array( 'private', 'publish' ) : "publish";
//general query
$args=array(
'post_status' => $post_status,
'post_type' => 'portfolio',
'orderby' => $portf_list_orderby,
'order' => $portf_list_order,
'posts_per_page' => $item_per_page,
'paged' => $paged
);
if( ! empty ( $categories ) ){
$args = array_merge($args, array(
'tax_query' => array(
array(
'taxonomy' => 'portfolio_categories',
'field' => 'id',
'terms' => $categories,
'operator' => "IN"
)
),
) );
}
ob_start();
$theQuery = query_posts($args);
//add class
$add_class = "";
//get page & post counts
$page_count = rt_get_page_count();
$post_count = $page_count['post_count'];
//add holder class
$add_holder_class = ! empty( $filterable ) ? "filterable" : "";
echo '<div id="'.$id.'" class="portfolio_holder clearfix '.$add_holder_class.' " data-rt-animate="animate" data-rt-animation-type="fadeIn" data-rt-animation-group="single">';
echo $filter_holder;
echo '<ul class="portfolio_boxes fluid fixed" data-filter="'.$item_width.'">';
while ( have_posts() ) : the_post();
//get post format
$portfolio_format = get_post_meta( $post->ID, RT_COMMON_THEMESLUG.'_portfolio_post_format', true);
//add first last classes if filterable is off
$add_class = "";
if( $counter % $item_width == 1 || $item_width == 1 ){
$add_class .= " first";
}
if( ( $counter % $item_width == 0 || $post_count == $counter ) && $add_class == "" ){
$add_class .= " last";
}
// selected term list of each post
$term_list = get_the_terms($post->ID, 'portfolio_categories');
//add terms as class name
$addTermsClass = $filter_ids = "";
if($term_list){
if(is_array($term_list)){
foreach ($term_list as $termSlug) {
$addTermsClass .= " ". sanitize_html_class($termSlug->slug);
$filter_ids .= ! empty( $filterable ) ? " filter-".$termSlug->term_taxonomy_id : "";
}
}
}
printf('<li class="box %s %s %s %s"><div class="portfolio_item_holder">'."\n",$layout_values[$item_width],$add_class,$addTermsClass, $filter_ids);
get_template_part( 'portfolio-contents/content', $portfolio_format );
echo '</div></li>'."\n";
$counter++;
endwhile;
echo '</ul></div>';
if( $pagination ){
rt_get_pagination( $wp_query );
}
wp_reset_query();
$output_string = ob_get_contents();
ob_end_clean();
return $output_string;
}
add_action( "wp_footer", function(){
?>
<script>
(function($){
"use strict";
$.fn.rt_run_isotope_custom = function(options) {
// cache container
var $holder = $(this).parents('.content_block_background:eq(0)');
var $container = $(this).find('.portfolio_boxes');
var $filter_navigation = $(this).find('.filter_navigation');
$(this).css("paddingTop","1px");
$(this).find(".portfolio_boxes").css({"paddingTop":"1px"});
$(this).css("opacity",1);
$container.isotope({
itemSelector:'.box',
layoutMode: 'fitRows'
});
// filter items when filter link is clicked
$filter_navigation.find("a").click(function(){
var selector = $(this).attr('data-filter');
$container.isotope({ filter: selector });
$container.isotope( 'on', 'layoutComplete',
function( isoInstance, laidOutItems ) { $.waypoints('refresh'); }
);
return false;
});
var $optionLinks = $filter_navigation.find('a');
$optionLinks.click(function(){
var $this = $(this);
// don't proceed if already selected
if ( $this.hasClass('active') ) {
return false;
}
var $optionSet = $this.parents('.filter_navigation');
$optionSet.find('.active').removeClass('active');
$this.addClass('active');
return false;
});
$filter_navigation.find('> li:first-child > a').trigger("click");
};
})(jQuery);
</script>
<?php
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment