Skip to content

Instantly share code, notes, and snippets.

@hasanm95
Created October 19, 2016 19:36
Show Gist options
  • Save hasanm95/1b8f8a21e76d6ca828858de974b20351 to your computer and use it in GitHub Desktop.
Save hasanm95/1b8f8a21e76d6ca828858de974b20351 to your computer and use it in GitHub Desktop.
Dynamic wordpress portfolio section with navigation
//1st register your custom post and taxonomy
//This code for fron-end
<section id="portfolio">
<div class="container">
<div class="row">
<ul class="portfolio-filter text-center">
<li><a class="btn btn-default active" href="#" data-filter="*">All</a></li>
<?php
$terms = get_terms('taxonomy_name');
if(!empty($terms) && !is_wp_error($terms)){
foreach($terms as $term){
echo ' <li><a class="btn btn-default" href="#" data-filter=".'.$term->slug.'">'.$term->name.'</a></li>';
}
}
?>
</ul><!--/#portfolio-filter-->
<div class="portfolio-items">
<?php
global $paged;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$posts_per_page = 4;
$settings = array(
'showposts' => $posts_per_page,
'post_type' => 'your_custom_post',
'orderby' => 'menu_order',
'order' => 'ASC',
'paged' => $paged
);
$post_query = new WP_Query($settings);
$total_found_posts = $post_query->found_posts;
$total_page = ceil($total_found_posts / $posts_per_page);
while($post_query->have_posts()) : $post_query->the_post();
$large_thumb = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'large', true );
?>
<?php
$terms = get_the_terms($post->ID, 'portfolio_cat');
if(!empty($terms) && !is_wp_error($terms)){
$portfolio_cat_slug = array();
foreach($terms as $term){
$portfolio_cat_slug[] = $term->slug;
}
$portfolio_cat_array = join(" ", $portfolio_cat_slug);
}
?>
<div class="col-xs-6 col-sm-4 col-md-3 portfolio-item <?php echo $portfolio_cat_array; ?>">
<div class="portfolio-wrapper">
<div class="portfolio-single">
<div class="portfolio-thumb">
<?php the_post_thumbnail('portfolio-thumb', array('class' => 'img-responsive'));?>
</div>
<div class="portfolio-view">
<ul class="nav nav-pills">
<li><a href="<?php the_permalink(); ?>"><i class="fa fa-link"></i></a></li>
<li><a href="<?php echo $large_thumb[0];?>" data-lightbox="example-set"><i class="fa fa-eye"></i></a></li>
</ul>
</div>
</div>
<div class="portfolio-info ">
<h2><?php the_title(); ?></h2>
</div>
</div>
</div>
<?php endwhile; ?>
</div>
<div class="portfolio-pagination">
<?php echo nyPaginate ($end_size = 1, $mid_size = 8, $pagi_type = 'list' ); ?>
<style>
.next.page-numbers {
background: rgba(0, 0, 0, 0) url("http://localhost/triangle/wp-content/themes/rrf-triangle/images/portfolio/right.png") no-repeat scroll 0 4px;
}
.prev.page-numbers{
background: rgba(0, 0, 0, 0) url("http://localhost/triangle/wp-content/themes/rrf-triangle/images/portfolio/left.png") no-repeat scroll 0 4px;
}
</style>
</div>
</div>
</div>
</section>
//input this code in functions.php
//This code for navigation
function nyPaginate($end_size = 1, $mid_size = 5, $pagi_type = 'list') {
global $post_query;
$big = 999999999; // need an unlikely integer
$args = array(
'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
'format' => '?page=%#%',
'total' => $post_query->max_num_pages,
'current' => max( 1, get_query_var('paged') ),
'show_all' => false,
'end_size' => $end_size,
'mid_size' => $mid_size,
'prev_text' => 'prev',
'next_text' => 'next',
'type' => $pagi_type, // plain, list, array
'add_args' => false,
'add_fragment' => ''
);
return paginate_links( $args );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment