Reply To: Photo Gallery Shortcode display Category
July 24, 2017 at 4:10 pm
#5143
Grace Themes
Member
Hi Pradeep,
I have completed the changes in custom-functions.php file of your website to display categorized photo gallery.
I have added below code in inc/custom-functions.php file
For customized photo gallery use shortcode:
[photogallery_category filter=’true’ category_id=’5,6′]
You just need to change the category_id
/[photogallery_category]
function photogallery_shortcode_category_func( $atts ) {
extract( shortcode_atts( array(
'show' => -1,
'category_id' => '',
'filter' => 'true'
), $atts ) );
$pfStr = '';
$categoryArray = explode(',', $category_id);
$pfStr .= '<div class="photobooth">';
if( $filter == 'true' ){
$pfStr .= '<div class="filter-gallery"><ul class="portfoliofilter clearfix"><li><a class="selected" data-filter="*" href="#">'.of_get_option('galleryshowallbtn').'</a><span></span></li>';
$args = array( 'include' => $categoryArray );
$categories = get_terms( 'gallerycategory', $args );
foreach ($categories as $category) {
$pfStr .= '<li><a data-filter=".'.$category->slug.'" href="#">'.$category->name.'</a></li>';
}
$pfStr .= '</ul></div><div class="clear"></div>';
}
$pfStr .= '<div class="row fivecol portfoliowrap"><div class="portfolio">';
$j=0;
$args = array(
'post_type' => 'photogallery',
'posts_per_page' => $show,
'tax_query' => array(
array(
'taxonomy' => 'gallerycategory',
'field' => 'ID',
'terms' => $categoryArray,
'operator' => 'IN'
),
)
);
query_posts( $args );
if ( have_posts() ) : while ( have_posts() ) : the_post();
$j++;
$videoUrl = get_post_meta( get_the_ID(), 'video_file_url', true);
$imgSrc = wp_get_attachment_image_src( get_post_thumbnail_id(), 'full');
$terms = wp_get_post_terms( get_the_ID(), 'gallerycategory', array("fields" => "all"));
$slugAr = array();
foreach( $terms as $tv ){
$slugAr[] = $tv->slug;
}
if ( $imgSrc[0]!='' ) {
$imgUrl = $imgSrc[0];
}else{
$imgUrl = get_template_directory_uri().'/images/img_404.png';
}
$pfStr .= '<div class="entry '.implode(' ', $slugAr).'"><div class="holderwrap">
<a href="'.( ($videoUrl) ? $videoUrl : $imgSrc[0] ).'" data-rel="prettyPhoto[bkpGallery]"><img src="'.$imgSrc[0].'"/></a>
</div></div>';
unset( $slugAr );
endwhile; else:
$pfStr .= '<p>Sorry, photo gallery is empty.</p>';
endif;
wp_reset_query();
$pfStr .= '</div></div>';
$pfStr .= '</div>';
return $pfStr;
}
add_shortcode( 'photogallery_category', 'photogallery_shortcode_category_func' );
Regards
Grace Themes