I have a WordPress theme that uses visual composer built in plugin. The blog and porfolio are inside a function that is then expressed as shortcode on a page. Whenever I try to add some pagination function it doesnt work. The pagination works on other "normal" pages like archives, or search. Any clues?
<?php function hsv_blog( $atts, $content = '', $id = '' ) {
extract( shortcode_atts( array(
'id' => '',
'class' => '',
'cats' => '',
'limit' => '',
'page_name' => '',
), $atts ) );
$args = array(
'post_type' => 'post',
'posts_per_page' => $limit,
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'ids',
'terms' => explode(',', $cats),
),
)
);
ob_start();
$q = new WP_Query( $args );
if ( $q->have_posts() ) while ( $q->have_posts() ) : $q->the_post();
$post_format = (get_post_format() == true) ? get_post_format():'standard';
switch ($post_format) {
case 'aside':
case 'quote':
$class = ' grey';
break;
default:
$class = '';
break;
}
global $hsv_opt;
// general settings
$type_website = $hsv_opt['general-type-website'];
$pg_class = ( $type_website == 2 ) ? ' home' : ''; ?>
<div id="post-<?php the_ID(); ?>" <?php post_class('element clearfix col1-3 blog auto bi'.$class.$pg_class); ?>>
<?php get_template_part('post-formats/content', $post_format ); ?> </div> <?php endwhile; ?> <?php wp_reset_query(); ?> <?php return ob_get_clean(); } add_shortcode( 'hsv_blog', 'hsv_blog' ); ?>
pagination in the functions file:
function wpbeginner_numeric_posts_nav() {
if( is_singular() )
return;
global $wp_query;
/** Stop execution if there's only 1 page */
if( $wp_query->max_num_pages <= 1 )
return;
$paged = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1;
$max = intval( $wp_query->max_num_pages );
/** Add current page to the array */
if ( $paged >= 1 )
$links[] = $paged;
/** Add the pages around the current page to the array */
if ( $paged >= 3 ) {
$links[] = $paged - 1;
$links[] = $paged - 2;
}
if ( ( $paged + 2 ) <= $max ) {
$links[] = $paged + 2;
$links[] = $paged + 1;
}
echo '<div class="navigation"><ul>' . "\n";
/** Previous Post Link */
if ( get_previous_posts_link() )
printf( '<li>%s</li>' . "\n", get_previous_posts_link() );
/** Link to first page, plus ellipses if necessary */
if ( ! in_array( 1, $links ) ) {
$class = 1 == $paged ? ' class="active"' : '';
printf( '<li%s>%s</li>' . "\n", $class, esc_url( get_pagenum_link( 1 ) ), '1' );
if ( ! in_array( 2, $links ) )
echo '<li>…</li>';
}
/** Link to current page, plus 2 pages in either direction if necessary */
sort( $links );
foreach ( (array) $links as $link ) {
$class = $paged == $link ? ' class="active"' : '';
printf( '<li%s>%s</li>' . "\n", $class, esc_url( get_pagenum_link( $link ) ), $link );
}
/** Link to last page, plus ellipses if necessary */
if ( ! in_array( $max, $links ) ) {
if ( ! in_array( $max - 1, $links ) )
echo '<li>…</li>' . "\n";
$class = $paged == $max ? ' class="active"' : '';
printf( '<li%s>%s</li>' . "\n", $class, esc_url( get_pagenum_link( $max ) ), $max );
}
/** Next Post Link */
if ( get_next_posts_link() )
printf( '<li>%s</li>' . "\n", get_next_posts_link() );
echo '</ul></div>' . "\n";}
I had the same problem, trying to create an element in Visual Composer that displays elements from my custom post.
The solution: inside your while loop add: $totalPages = $q->max_num_pages; Now you can pass $totalPages to your pagination function.
Below I've added my pagination function that worked.
Example of usage: my_pagination($totalPages);
function my_pagination($pages = '', $range = 2) {
$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'>";
if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>«</a>";
if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>‹</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)."'>›</a>";
if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>»</a>";
echo "</div>\n";
}
}
Related
I'm currently using the Custom Field Suite plugin for WordPress to get the relationship between post types. My code looks like this:
<div class="productList">
<?php
$values = CFS()->get( 'auctioned_items' );
//pagination
$nb_elem_per_page = 16;
$page = isset($_GET['nav'])?intval($_GET['nav']-1):0;
$current_page = $_GET['nav'];
$data = $values;
$count = 0;
if($i %$nb_elem_per_page != 0) {
$number_of_pages = intval(count($data)/$nb_elem_per_page)+2;
} else {
$number_of_pages = intval(count($data)/$nb_elem_per_page)+1;
}
foreach ( array_slice( $data, $page*$nb_elem_per_page, $nb_elem_per_page) as $post_id) {
$lot_number = CFS()->get( 'lot_number', $post_id );
$count++;
if (empty($current_page)) {
$newCount = ($count + ((1 + 1) - 1) * $nb_elem_per_page) - $nb_elem_per_page;
} else {
$newCount = ($count + (($current_page + 1) - 1) * $nb_elem_per_page) - $nb_elem_per_page;
}
//sort by $lot_number
?>
<p class="lot"><?php echo $lot_number; ?></p>
<h3><?php echo $the_post->post_title; ?></h3>
<?php } ?>
</div>
$values is an array that lists all ID of related post types. It looks like this
Array ( [0] => 111 [1] => 109 [2] => 110)
This results into listing of related posts based on it's position on array. However, I wanted to sort the values based on the custom field named $lot_number. $lot_number is just input numbers but sometimes there will be cases that a letter is involved (ex. 8, 9, 10A, 10B, 11)
Is there a way to do this? It gets confusing since there is a pagination involved.
If I understood well, you can try this:
<div class="productList">
<?php
$values = CFS()->get( 'auctioned_items' );
//pagination
$nb_elem_per_page = 16;
$page = isset( $_GET[ 'nav' ] ) ? intval( $_GET[ 'nav' ] - 1 ) : 0;
$current_page = $_GET[ 'nav' ];
$data = $values;
if ( $i % $nb_elem_per_page != 0 ) {
$number_of_pages = intval( count( $data ) / $nb_elem_per_page ) + 2;
} else {
$number_of_pages = intval( count( $data ) / $nb_elem_per_page ) + 1;
}
$sorted = [];
$posts = array_slice( $data, $page * $nb_elem_per_page, $nb_elem_per_page );
foreach ( $posts as $post_id ) {
$sorted[ $post_id ] = CFS()->get( 'lot_number', $post_id );
}
// you can use either asort(low to high) or arsort(high to low)
// there are some flags too, see here: http://php.net/manual/en/function.sort.php
asort( $sorted );
// and now...
foreach ( $sorted as $post_id => $lot_number ) {
?>
<p class="lot"><?php echo $lot_number; ?></p>
<h3><?php echo $the_post->post_title; ?></h3>
<?php } ?>
</div>
I've been trying to fix this issue since last week but I can't get it done. The problem itself sounds quite simple, my pagination on WordPress works just fine when it's not dealing with taxonomies, for example: www.example.com/post_type/2 but if I try to use a taxonomy, I can't go past page 1 (www.example.com/taxonomy/term/2).
Custom Post Type = noticias and Taxonomy = assunto, so I don't think the name is a conflicting factor here.
$custom_query_args['paged'] = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$args = array( 'post_type' => 'noticias', 'paged' => $custom_query_args['paged'], 'posts_per_page' => 5);
$loop = new WP_Query( $args );
$temp_query = $wp_query;
$wp_query = NULL;
$wp_query = $loop;
while ( $loop->have_posts() ) : $loop->the_post();
<!-- Content here -->
endwhile;
wp_reset_postdata();
if (function_exists("pagination")) {
pagination($wp_query->max_num_pages);
}
$wp_query = $temp_query;
The function that I'm using atm:
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>Página ".$paged." de ".$pages."</span>";
if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>« Primeira</a>";
if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>‹ Anterior</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>":"".$i."";
}
}
if ($paged < $pages && $showitems < $pages) echo "Próxima ›";
if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>Última »</a>";
echo "</div>\n";
}
}
Ok, so I got it done. I don't know if this is the best way to solve the problem but it was the only one that I found. This answer is a "fork" of jcarroll780's answer on the same problem with some modifications.
First I added these functions into my functions.php:
$option_posts_per_page = get_option( 'posts_per_page' );
add_action( 'init', 'my_modify_posts_per_page', 0);
function my_modify_posts_per_page() {
add_filter( 'option_posts_per_page', 'my_option_posts_per_page' );
}
function my_option_posts_per_page( $value ) {
global $option_posts_per_page;
if ( is_tax( 'assunto') ) {
return 5;
} else {
return $option_posts_per_page;
}
}
Then I used the following loop to display the correct pagination (taxonomy-assunto.php):
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
query_posts(array_merge( $wp_query->query, array('post_type'=>'noticias', 'posts_per_page' => 5, 'paged'=>$paged ) ) );
if (have_posts()) : while ( have_posts() ) : the_post();
// Content Here
endwhile; endif;
if ( function_exists( 'wp_pagenavi' ) ) {
wp_pagenavi();
}
It worked pretty well for me, I hope this answers your questions aswell.
I'm having difficulties in adding pagination for custom wordpress query showing latest author's posts from category 'blog'. I found this code somewhere on stackoverflow. Could someone help me/give some tips how to add pagination to this query? I've spent on it couple hours and couldn't came up with something working.
Best for me would be numeric pagination:
1 | 2 | 3 | 4 | so on...
functions.php
function the_latest_author_posts($post) {
$relatedargs = array(
'author' => $post->post_author,
'post__not_in' => array( $post->ID),
'posts_per_page' => 8,
'category_name' => 'blog',
);
$relatedquery = new WP_Query( $relatedargs );
while($relatedquery->have_posts()){
$relatedquery->the_post();
$ID = get_the_ID();
?>
<div class="post-blog">
<?php
if(has_post_thumbnail()) {
$relatedthumbnail = wp_get_attachment_image_src( get_post_thumbnail_id($ID), 'medium', false);
$relatedthumbnail_large = wp_get_attachment_image_src( get_post_thumbnail_id($ID), 'full', false);
?>
<?php } ?>
<h1><span><?php the_title(); ?></span></h1>
<h6><i class="fa fa-clock-o" aria-hidden="true"></i> <?php echo get_the_time('j') . '/' . get_the_time('m') . '/' . get_the_time('Y') . ' '; ?></h6>
<?php
$content = get_the_content();
echo wp_trim_words( $content , '25' ); ?>
</div>
<?php wp_reset_postdata();}}
I call this function in my template in this way:
<?php
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
the_latest_author_posts($post);
}
}
?>
First add this function in your functions
function numeric_pagination($pages = '', $range = 2){
$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'>";
if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>«</a>";
if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>‹</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)."'>›</a>";
if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>»</a>";
echo "</div>\n";
} }
and use it after your loop
numeric_pagination();
WordPress pagination function in shortcode always displayed on the top. Please have a look on below code
/*-------------------------------------------------------------------------*/
/* Custom Pagination */
/*-------------------------------------------------------------------------*/
function suareztheme_pagination($pages = '', $range = 2){
$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 ){
$pagination_html .= '<div class="pagination">';
if( $paged > 2 && $paged > $range + 1 && $showitems < $pages ){
$pagination_html .= '«';
}
if( $paged > 1 && $showitems < $pages ){
$pagination_html .= '‹';
}
for ( $i = 1; $i <= $pages; $i++ ){
if ( 1 != $pages && ( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems )){
if ( $paged == $i ){
$pagination_html .= '<span class="current">' . $i . '</span>';
} else{
$pagination_html .= '' . $i . '';
}
}
}
if ( $paged < $pages && $showitems < $pages ){
$pagination_html .= '›';
}
if ( $paged < $pages - 1 && $paged + $range - 1 < $pages && $showitems < $pages ){
$pagination_html .= '»';
}
$pagination_html .= '</div>';
return $pagination_html;
}
}
Then I called it in the shortcode function.
/*-------------------------------------------------------------------------*/
/* Grid Shortcode */
/*-------------------------------------------------------------------------*/
function RecentBlog($atts, $content = null) {
extract(shortcode_atts(array(
"comments" => 'true',
"date" => 'true',
"columns" => '4',
"limit" => '-1',
"title" => 'true',
"description" => 'true',
"cat_slug" => '',
"post_type" => '',
"excerpt_length" => '15',
"readmore_text" => '',
"pagination" => 'false'
), $atts));
global $post;
$postformat = get_post_format();
....
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} elseif ( get_query_var('page') ) {
$paged = get_query_var('page');
} else {
$paged = 1;
}
......
if (have_posts()) : while (have_posts()) : the_post();
$postformat = get_post_format();
if( $postformat == "" ) $postformat="standard";
$protected = "";
$portfoliogrid .= '<div class="' . $column_no . ' post grid-post post-' . $postformat . '">';
.....////
$portfoliogrid .= '<div class="summary-info">';
.......
$portfoliogrid .='</div>';
// If either of title and description needs to be displayed.
if ( $title == "true" || $description == "true" ) {
$portfoliogrid .='<div class="work-details">';
......
$portfoliogrid .='</div>';
}
$portfoliogrid .='</div>';
endwhile; endif;
if ( $pagination == "true" ){
if ( isset( $additional_loop ) ){
echo suareztheme_pagination( $additional_loop->max_num_pages );
} else {
echo suareztheme_pagination();
}
if ( function_exists("suareztheme_pagination") ) {
} else {
next_posts_link('«« Older Posts');
previous_posts_link('Newer Posts »»');
}
}
wp_reset_query();
return $portfoliogrid;
}
add_shortcode( "recentblog", "RecentBlog" );
The above code is working fine , but it has one problem, when it display in specific page, it always displayed on the top regardless it's place. So appreciate your support to help me, thanks in advance.
In the shortcode callback, you need to concatenate and return the html, e.g.:
$portfoliogrid .= suareztheme_pagination();
Note that you'll have to use get_next_posts_link() and get_previous_posts_link() instead of next_posts_link() and previous_posts_link().
Can't figure out why this stopped working all of a sudden. The website is: http://www.revival.tv/sermons/topical-messages/
once it goes to page two it just uses the archive template :( not sure why it is doing this, it worked fine for the last year. Wondering if a Wordpress update did it, we just noticed it.
I am using the following for the pagination:
function pagination($pages = '', $range = 2) {
$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($pages != 1) {
echo "<div class='pagination group'>";
if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>«</a>";
if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>‹</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-page'>".$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)."'>›</a>";
if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>»</a>";
echo "</div>\n";
}
}
and then this rewrite for the sermons:
// Rewrite the Sermon URL
function sermon_rewrite() {
$cpt = 'sermon';
$tax = 'series';
add_rewrite_rule(
'^'.$cpt.'s/([^/]+)/page/([0-9]+)/?$',
'index.php?post_type='.$cpt.'&'.$tax.'=$matches[1]&paged=$matches[2]',
'top'
);
add_rewrite_rule(
'^'.$cpt.'s/([^/]+)/([^/]+)/?',
'index.php?post_type='.$cpt.'&'.$tax.'=$matches[1]&'.$cpt.'=$matches[2]',
'top'
);
add_rewrite_rule(
'^'.$cpt.'s/([^/]+)/([^/]+)/([^/]+)/?',
'index.php?post_type='.$cpt.'&'.$tax.'=$matches[1]&'.$tax.'=$matches[2]&'.$cpt.'=$matches[3]',
'top'
);
}
add_action('init','sermon_rewrite');
// Update Wordpress with the rewrite
function sermon_link( $link, $post = 0 ) {
$cpt = 'sermon';
$tax = 'series';
$parent = null;
$terms = get_the_terms($post->ID, $tax);
if( !$terms ) {
$child = 'other';
} else {
$child_obj = array_pop($terms);
$parent_obj = get_term_by('id', $child_obj->parent, $tax);
$child = $child_obj->slug;
if ( $parent_obj ) {
$parent = $parent_obj->slug;
}
}
if ( $post->post_type == $cpt && $terms ) {
return home_url(user_trailingslashit($cpt.'s/'.$parent.'/'.$child.'/'.$post->post_name));
} else if ( $post->post_type == $cpt && !$terms ) {
return home_url(user_trailingslashit($cpt.'s/'.$child.'/'.$post->post_name));
} else {
return $link;
}
}
add_filter('post_type_link', 'sermon_link', 1, 3);
I think I have found the problem (I'm just trial and erroring here using your provided URL). The following URL successfully loads page 2.
http://www.revival.tv/sermons/topical-messages/page/2/?post_type=sermons
Which leads me to think that simply adding the s after index.php?post_type='.$cpt should do the trick. See sample below:
add_rewrite_rule(
'^'.$cpt.'s/([^/]+)/page/([0-9]+)/?$',
'index.php?post_type='.$cpt.'s&'.$tax.'=$matches[1]&paged=$matches[2]',
'top'
);