Last active
May 11, 2017 07:40
-
-
Save asadaly111/a040b04ee9e43414299b19e7290c25af to your computer and use it in GitHub Desktop.
Wordpess Pagination without plugin custom pagination by asadaly111
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//add in function in function.php | |
function pagination($pages = '', $range = 4) | |
{ | |
$showitems = ($range * 2)+1; | |
global $paged; | |
if(empty($paged)) $paged = 1; | |
if($pages == '') | |
{ | |
global $wp_query; | |
$pages = $wp_query->max_num_pages; | |
if(!$pages) | |
{ | |
$pages = 1; | |
} | |
} | |
if(1 != $pages) | |
{ | |
// echo "<div class=\"pagination\"> | |
// <span>Page ".$paged." of ".$pages."</span>"; | |
echo "<div class=\"pagination\">"; | |
previous_posts_link('Prev'); | |
if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>« First</a>"; | |
if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>‹ Previous</a>"; | |
for ($i=1; $i <= $pages; $i++) | |
{ | |
if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems )) | |
{ | |
echo ($paged == $i)? "<span class=\"current\">".$i."</span>":"<a href='".get_pagenum_link($i)."' class=\"inactive\">".$i."</a>"; | |
} | |
} | |
if ($paged < $pages && $showitems < $pages) echo "<a href=\"".get_pagenum_link($paged + 1)."\">Next ›</a>"; | |
if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>Last »</a>"; | |
next_posts_link('Next'); | |
echo "</div>\n"; | |
} | |
} | |
//add in css | |
.pagination { | |
clear:both; | |
padding:20px 0; | |
position:relative; | |
font-size:11px; | |
line-height:13px; | |
} | |
.pagination span, .pagination a { | |
display:block; | |
float:left; | |
margin: 2px 2px 2px 0; | |
padding:6px 9px 5px 9px; | |
text-decoration:none; | |
width:auto; | |
color:#fff; | |
background: #555; | |
} | |
.pagination a:hover{ | |
color:#fff; | |
background: #3279BB; | |
} | |
.pagination .current{ | |
padding:6px 9px 5px 9px; | |
background: #3279BB; | |
color:#fff; | |
} | |
<!-- Add this code where you want to show should be similar like this--> | |
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1; | |
$q = new WP_Query( | |
array( | |
'post_type' => array('post'), | |
'post_status' => array('publish'), | |
'orderby' => 'date', | |
'order' => 'DSC', | |
'posts_per_page' => 1, | |
'paged' => $paged | |
) | |
); | |
?> | |
<?php while ($q->have_posts()) : $q->the_post(); ?> | |
<div class="col-md-4 col-sm-6"> | |
<article class="bpost"> | |
<?php | |
if(has_post_thumbnail()){ | |
$thumbnail = wp_get_attachment_image_src(get_post_thumbnail_id(),'full', true); | |
?> | |
<img src="<?php echo $thumbnail[0] ?>" width="306" height="200" alt="mproduct"> | |
<?php } ?> | |
<span><?php echo get_the_date(); ?></span> | |
<h1><?php the_title();?></h1> | |
<p><?php echo substr(strip_tags(get_the_content()),0,250); ?>...</p> | |
<a href="<?php echo get_permalink($post->ID);?>"><i class="fa fa-angle-right" aria-hidden="true"></i></a> | |
</article> | |
</div> | |
<?php endwhile; ?> | |
<?php if (function_exists("pagination")) { | |
pagination($q->max_num_pages); | |
} ?> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment