PAGINATION SETUP FOR WP_QUERY
How should I setup pagination in this WP_QUERY for my website.I want only 6 posts in a page.
In my wordpress site I set front-page display as a latest posts instead of particular page.
<div class="row child-section">
<?php
/**************************************
* Generating multiple posts
*************************************/
$args = array(
'type' => 'post',
'posts_per_page' => 6,
'offset' => 1
);
$query = new WP_Query($args);
if($query->have_posts()):
while($query->have_posts()):
$query->the_post();
?>
<div class="col-xs-6 col-sm-3 other-post clearfix">
<div class="featured-img">
<?php the_post_thumbnail(); ?>
</div>
<h1 class="post-heading">
<?php
$title = wp_trim_words(get_the_title(),3," .. ");
echo $title;
?>
</h1>
<p class="small-desc">
<?php echo get_the_excerpt(); ?>
<p>
READ MORE
</div>
<?php
endwhile; wp_reset_postdata(); endif;
?>
</div>
<?php
global $wp_query;
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages
) );
?>
this particular code echo out the paginate_links there are more arguments available for this you can look into paginate_links
/********function.php*******/
function wp_custom_pagination() {
global $wp_query;
$big = 999999999; // need an unlikely integer
$pages = paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages,
'prev_next' => True,
'prev_text' => __('« Previous'),
'next_text' => __('Next »'),
'type' => 'array'
) );
if( is_array( $pages ) ) {
$paged = ( get_query_var('paged') == 0 ) ? 1 : get_query_var('paged');
echo '<div id="pagination"><ul class="page-numbers">';
echo '<li id="page_num"><span>'. $paged . ' of ' . $wp_query->max_num_pages .'</span></li>';
foreach ( $pages as $page ) {
echo "<li id='page_num'>$page</li>";
}
echo '</ul></div>';
}
}
/*************************************/
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts("posts_per_page=6&order=DESC&paged=$paged");
if(have_posts())
{
while(have_posts())
{
the_post();
?>
<div class="col-xs-6 col-sm-3 other-post clearfix">
<div class="featured-img">
<?php the_post_thumbnail(); ?>
</div>
<h1 class="post-heading">
<?php
$title = wp_trim_words(get_the_title(),3," .. ");
echo $title;
?>
</h1>
<p class="small-desc">
<?php echo get_the_excerpt(); ?>
<p>
READ MORE
</div>
<?php } ?>
<div class="pagination"><?php wp_custom_pagination(); ?> </div>
<?php } wp_reset_query();
?>
Use php pagination
global $wpdb;
$db_table_name=(isset($_GET['browse'])!='' && isset($_GET['page'])=='wp_db_admin')?$_GET['browse']:'';
$quer = "select * from ".$_GET["browse"];
$res= $wpdb->get_results($quer);
$results_per_page = 25;
$count_result = "select count(*) from ". $_GET["browse"];
$result_number = $wpdb->get_var($count_result);
$number_of_pages = ceil ( $result_number/$results_per_page);
if(!isset($_GET['pagina'])){
$pagina=1;
}else{
$pagina= $_GET['pagina'];
}
$this_page_result_first = ($pagina-1)*$results_per_page;
$pageQuery = "SELECT * FROM ".$db_table_name." LIMIT ".$this_page_result_first.",". $results_per_page;
$resPageQuery = $wpdb->get_results($pageQuery);
Related
I'm new to Wp and I'm trying to developing my first theme. After I add the paginations to my archive page the category filter doesn't work anymore (probably because I override the WP Query).
What shoud I put in the query in order to see only the posts of the category that is clicked? (if I don't put anything wp do the magic) but without the query the pagination doesn't work.
This is the archive.php
<?php
get_header(); ?>
<?php
if ( have_posts() ) : ?>
<header class="page-header no-image">
<?php
the_archive_title( '<h1 class="page-title u-text-center">', '</h1>' );
?>
</header><!-- .page-header -->
<?php get_template_part( 'template-parts/filter-category' ); ?>
<?php
/*QUERY:
This is the problem if I move the query from there the category works but
If I leave the query the pagination doesn't have the post_per_page argument to take and doesn't work...
*/
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'post_type'=>'post',
'post_status'=>'publish',
'posts_per_page' => 6,
'paged' => $paged
);
$wp_query = new WP_Query( $args );
/*END QUERY*/
?>
<div id="blogpost-list" class="container">
<div class="row blogpost-wrapper">
<?php
/* Start the Loop */
$i = 1;
while ( $wp_query->have_posts() ) : $wp_query->the_post();?>
<?php if ($i % 3 == 0): ?>
<div class="blogpost-item-grid">
<!-- some stuff to display the post.. doesn't really matter-->
<?php get_template_part( 'template-parts/content-post-preview-large' ); ?>
</div>
<?php else: ?>
<div class="blogpost-item-grid">
<!-- some stuff to display the post doesn't really matter -->
<?php get_template_part( 'template-parts/content-post-preview' ); ?>
</div>
<?php endif; ?>
<?php $i++; ?>
<?php endwhile;?>
</div>
</div>
<?php get_template_part( 'template-parts/pagination' ); ?>
<?php
else :
get_template_part( 'template-parts/content', 'none' );
endif; ?>
<?php
get_footer();
Here the code that I use for the pagination:
<?php //Require a wp->query ?>
<div class="container-fluid">
<div class="pagination-wrapper">
<?php
$pag_args = array(
'base' => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
'total' => $wp_query->max_num_pages,
'current' => max( 1, get_query_var( 'paged' ) ),
'format' => '?paged=%#%',
'show_all' => false,
'type' => 'plain',
'end_size' => 2,
'mid_size' => 1,
'prev_next' => true,
'prev_text' => sprintf( '<img class="icn icn-small" src="' . get_template_directory_uri() .'/assets/images/icn-chevron-left.svg"/>'),
'next_text' => sprintf( '<img class="icn icn-small" src="' . get_template_directory_uri() .'/assets/images/icn-chevron-right.svg"/>'),
'add_args' => false,
'add_fragment' => '',
);
echo paginate_links($pag_args);
?>
</div>
</div>
SOLVED:
Add this to get the category
get_header();
$catID = get_queried_object_id();
Pass that to the query:
$args = array(
[...]
'cat' => $catID
);
$wp_query = new WP_Query( $args );
The problem is that you are not using your $wp_query variable after its instanciation.
Your while loop should look like this:
while ( $wp_query->have_posts() ) : $wp_query->the_post();?>
/*Your inner code here*/
<?php endwhile;?>
You can create the category.php file if it' not exists. Then Inside your file
<?php
get_header();
$catID = get_queried_object_id();
?>
<?php
if ( have_posts() ) : ?>
<header class="page-header no-image">
<?php
the_archive_title( '<h1 class="page-title u-text-center">', '</h1>' );
?>
</header><!-- .page-header -->
<?php get_template_part( 'template-parts/filter-category' ); ?>
<?php
/*QUERY:
This is the problem if I move the query from there the category works but
If I leave the query the pagination doesn't have the post_per_page argument to take and doesn't work...
*/
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'post_type'=>'post',
'post_status'=>'publish',
'posts_per_page' => 6,
'cat' => $catID,
'paged' => $paged
);
$wp_query = new WP_Query( $args );
/*END QUERY*/
?>
<div id="blogpost-list" class="container">
<div class="row blogpost-wrapper">
<?php
/* Start the Loop */
$i = 1;
while ( $wp_query->have_posts() ) : $wp_query->the_post();?>
<?php if ($i % 3 == 0): ?>
<div class="blogpost-item-grid">
<!-- some stuff to display the post.. doesn't really matter-->
<?php get_template_part( 'template-parts/content-post-preview-large' ); ?>
</div>
<?php else: ?>
<div class="blogpost-item-grid">
<!-- some stuff to display the post doesn't really matter -->
<?php get_template_part( 'template-parts/content-post-preview' ); ?>
</div>
<?php endif; ?>
<?php $i++; ?>
<?php endwhile;?>
</div>
</div>
<?php get_template_part( 'template-parts/pagination' ); ?>
<?php
else :
get_template_part( 'template-parts/content', 'none' );
endif; ?>
<?php
get_footer();
I'm trying to make a category template in which, first post is displayed with full text + big thumbnail (as featured) and then posts with titles only + a smaller thumbnail.
I've managed (with help) to do this with two queries, but first post remains the same on each page, won't change, as also, the loop won't adopt the offset of first page into other pages too.
Code is here:
<?php $cat_link = get_category_link( $cat_id );
$cat_name = get_cat_name($cat_id);
$cat_id = 22; ?>
<?php $latest_cat_post = new WP_Query( array('posts_per_page' => 1, 'category__in' => array($cat_id)));
if( $latest_cat_post->have_posts() ) : while( $latest_cat_post->have_posts() ) : $latest_cat_post->the_post(); ?>
<div class="catrecent">
<div class="recenttitle">
<h2 class="catidtxt"> <?php echo ($cat_name); ?></h2>
<h2 class="recentposttitle"><?php the_title(); ?></h3>
</div>
<?php if( has_post_thumbnail() ) { ?>
<div class="recentpostwrap">
<?php the_post_thumbnail( 'thumbcatbig' ); ?>
<?php the_content_limit(588,""); ?>
<div class="readrecent">
<?php _e("more", "mm"); ?>
</div>
</div>
<?php } else { ?>
<div class="holder no-thumb-big">
<?php the_content_limit(798,""); ?>
<div class="more">
<?php _e("more", "mm"); ?>
</div>
</div>
<?php } ?>
<?php endwhile; else: ?><?php endif; ?><?php wp_reset_query(); ?>
</div>
<ul class="catlist-recent">
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$ppp = 7;
$offset = 1;
$cat_id = '22,27,29';
//Manually determine page query offset (offset + current page (minus one) x posts per page)
$page_offset = $offset + ( ($paged - 1) * $ppp );
$query = new wp_query(array(
'offset' => $page_offset,
'posts_per_page' => $ppp,
'cat' => $cat_id,
'paged' => $paged
));
if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post(); ?>
<li class="catlist-recentpost">
<?php the_post_thumbnail( 'thumbcatsmall' ); ?>
<?php the_title(); ?>
<div class="catlist-recentposttxt"> <?php the_content_limit(300, ''); ?></div>
</li>
<?php endwhile; else: ?><?php endif; ?>
</ul>
<div class="cat-pagenate">
<?php
// pagination
$big = 999999999; // need an unlikely integer
echo paginate_links(array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'total' => ceil(($query->found_posts - $offset) / $ppp),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
));
wp_reset_query();
?>
</div>
Any help appreciated!
Try
$query = new wp_query(array(
'offset' => $page_offset,
'number' => $ppp,
'cat' => $cat_id,
'page' => $paged
));
Note the change from "paged" to "page"
I'm using the code below to display in a custom page, a list with posts from a certain category. I would like to add pagenation at the bottom, but until now, I fail.
Any way to add it somehow?
<?php $cat_id = 22;
$cat_link = get_category_link( $cat_id );
$cat_name = get_cat_name($cat_id); ?>
<div class="catrecent">
<?php $query = new wp_query();
$query->query('showposts=1&cat=' . $cat_id);
if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post(); ?>
<div class="recenttitle">
<h2 class="catidtxt"> <?php echo ($cat_name); ?></h2>
<h2 class="recentposttitle"><?php the_title(); ?></h3>
</div>
<div class="recentpostwrap">
<?php the_post_thumbnail( 'catlarge' ); ?>
<?php the_content_limit(600,""); ?>
</div>
<?php endwhile; else: ?><?php endif; ?><?php wp_reset_query(); ?>
</div>
<ul class="posts_list">
<?php $query = new wp_query();
$query->query('offset=1&showposts=4&cat=22&paged=' . $cat_id);
if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post(); ?>
<li>
<?php the_post_thumbnail( 'catmed' ); ?>
<?php the_title(); ?>
</li>
<?php endwhile; else: ?><?php endif; ?><?php wp_reset_query(); ?>
</ul>
Useful links: http://codex.wordpress.org/Function_Reference/paginate_links, http://codex.wordpress.org/Making_Custom_Queries_using_Offset_and_Pagination
Try something like this
<ul class="posts_list">
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$ppp = 4;
$offset = 1;
//Manually determine page query offset (offset + current page (minus one) x posts per page)
$page_offset = $offset + ( ($paged - 1) * $ppp );
$query = new wp_query(array(
'offset' => $page_offset,
'posts_per_page' => $ppp,
'cat' => $cat_id,
'paged' => $paged
));
if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post(); ?>
<li>
<?php the_post_thumbnail( 'catmed' ); ?>
<?php the_title(); ?>
</li>
<?php endwhile; else: ?><?php endif; ?>
</ul>
<?php
// pagination
$big = 999999999; // need an unlikely integer
echo paginate_links(array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'total' => ceil(($query->found_posts - $offset) / $ppp),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
));
wp_reset_query();
?>
I am trying to do some custom page and I am not using the wp_pagenavi plugin but a custom function with my custom pages, right now it only works on index.php pages but it was working fine some days ago before I've added more query elements.
//custom pagepavi function
function my_pagenavi( $the_query = false ){
global $wp_query;
$query = ($the_query) ? $the_query : $wp_query;
$max = $query->max_num_pages;
$current_page = max(1, get_query_var('paged'));
$big = 999999999;
if ( $max > 1 ) {
echo "<div class='pagination' style='height:auto'>";
echo paginate_links(array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => $current_page,
'show_all' => false,
'total' => $max,
'type' => 'list',
'prev_text' => __('PREV','dnp_theme'),
'next_text' => __('NEXT','dnp_theme'),
));
echo "</div>";
}
}
Now here is the loop from my template page.
//some query stuff
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$query = 'offset=0&paged='.$paged;
$blogs = new WP_Query($query);
if ( $blogs->have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( $blogs->have_posts() ) : $blogs->the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php my_pagenavi( array('query' => $blogs) ); ?>
<?php endif; ?>
Why is not loading anything? What is going on??
The query is not correct that is why it's not working, at least it was not, not I have it working fine :)
instead of
$blogs = new WP_Query($query);
and all that should be
$blogs = query_posts( 'post_type=post&posts_per_page=4&paged='.get_query_var('paged') );
the loop is just like any other and works perfect
if ( have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php my_pagenavi(); ?>
<?php endif; ?>
after endwhile write this code
global $wp_query;
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages
) );
Long story short. I'm trying to expand the functionality of a portfolio template. It currently can only handle one page. I can duplicate the template, relying on custom fields to pull in specific portfilio items (the theme uses custom post types).
So, here's the bit from my portfolio-template.php:
$args = array(
'post_type' => 'portfolio',
'orderby' => 'menu_order',
'order' => 'ASC',
'meta_value' => 'ao',
'posts_per_page' => -1
);
So, I want to create a new page, select the portfolio template, then define a custom field on that page that will pass on the meta_value to the portfolio-template.php
So it would look like this instead:
$args = array(
'post_type' => 'portfolio',
'orderby' => 'menu_order',
'order' => 'ASC',
'meta_value' => 'PULL_VALUE_FROM_CUSTOM_FIELD',
'posts_per_page' => -1
);
With "PULL_VALUE_FROM_CUSTOM_FIELD" being the bit that I can't figure out. Sorry if my vernacular is code_crude, I'm new at this!
EDIT: Adding in the full code of the template
<?php
$args = array(
'post_type' => 'portfolio',
'orderby' => 'menu_order',
'order' => 'ASC',
'meta_value' => 'ao',
'posts_per_page' => -1
);
$portfolio_query = new WP_Query($args);
if( $portfolio_query->have_posts() ) :
echo '<div id="primary" class="hfeed">';
while( $portfolio_query->have_posts() ) : $portfolio_query->the_post();
// styles
$style = 'zilla-' . get_post_meta($post->ID, '_zilla_portfolio_style', true);
$media_pos = 'zilla-' . get_post_meta($post->ID, '_zilla_portfolio_media_position', true);
$width = ( $media_pos == 'zilla-media-center' ) ? 940 : 600;
// project url
$portfolio_url = get_post_meta($post->ID, '_zilla_portfolio_project_url', true);
if( !empty($portfolio_url) )
$portfolio_button_copy = get_post_meta($post->ID, '_zilla_portfolio_project_url_copy', true);
// determine which media to display
$portfolio_display_gallery = get_post_meta($post->ID, '_zilla_portfolio_display_gallery', true);
$portfolio_display_video = get_post_meta($post->ID, '_zilla_portfolio_display_video', true);
$portfolio_display_audio = get_post_meta($post->ID, '_zilla_portfolio_display_audio', true);
// grab everything else
$custom_bg = get_post_meta($post->ID, '_zilla_portfolio_display_background', true);
$portfolio_caption = get_post_meta($post->ID, '_zilla_portfolio_caption', true);
?>
<?php zilla_page_before(); ?>
<!--BEGIN .hentry-->
<div <?php post_class( $style . ' ' . $media_pos ) ?> id="post-<?php the_ID(); ?>">
<?php zilla_page_start(); ?>
<div class="hentry-inner">
<!--BEGIN .entry-media -->
<div class="entry-media">
<?php
if( $portfolio_display_gallery == 'on' ) {
$gallery_layout = get_post_meta( $post->ID, '_zilla_gallery_layout', true);
$slideshow = ( $gallery_layout == 'slideshow' ) ? true : false;
$size = ( $media_pos == 'zilla-media-center' ) ? 'portfolio-full' : 'portfolio-index';
zilla_gallery( $post->ID, $size, $slideshow, $slideshow );
}
if( $portfolio_display_video == 'on' ) {
$embed = get_post_meta($post->ID, '_zilla_video_embed_code', true);
if( !empty( $embed ) ) {
echo stripslashes(htmlspecialchars_decode($embed));
} else {
zilla_video( $post->ID, $width );
}
}
if( $portfolio_display_audio == 'on' ) {
zilla_audio( $post->ID, $width );
}
?>
<!--END .entry-media -->
</div>
<!--BEGIN .entry-content -->
<div class="entry-content">
<h2 class="entry-title"><?php the_title(); ?></h2>
<?php if( !empty($portfolio_caption) )
echo "<p class='zilla-caption'>" . stripslashes(htmlspecialchars_decode($portfolio_caption)) . "</p>"; ?>
<?php if( !empty($portfolio_url) && $media_pos == 'zilla-media-center' ) { ?>
<?php echo $portfolio_button_copy; ?>
<?php } ?>
<?php the_content(); ?>
<?php wp_link_pages(array('before' => '<p><strong>'.__('Pages:', 'zilla').'</strong> ', 'after' => '</p>', 'next_or_number' => 'number')); ?>
<?php if( !empty($portfolio_url) && ( $media_pos == 'zilla-media-left' || $media_pos == 'zilla-media-right' ) ) { ?>
<?php echo $portfolio_button_copy; ?>
<?php } ?>
<!--END .entry-content -->
</div>
</div>
<?php zilla_page_end(); ?>
<!--END .hentry-->
</div>
<?php zilla_page_after(); ?>
<?php endwhile; ?>
<!--END #primary .hfeed-->
</div>
<?php else: ?>
<div id="content">
<!--BEGIN #post-0-->
<div id="post-0" <?php post_class(); ?>>
<h2 class="entry-title"><?php _e('Error: No Portfolios Found', 'zilla') ?></h2>
<!--BEGIN .entry-content-->
<div class="entry-content">
<p><?php _e("Sorry, but no portfolios have been created.", "zilla") ?></p>
<!--END .entry-content-->
</div>
<!--END #post-0-->
</div>
</div>
<?php endif; ?>
Before the Loop, you can access the global variable $post, which in case of single.php and page.php will represent that very post type (post/page/cpt) object that will be displayed by the template file.
If not mistaken, when used in category.php, for example, it will grab the first one.
So, in the case in question:
<?php
global $post;
$the_meta = get_post_meta( 'custom_field_name', $post->ID );
if( !$the_meta )
$the_meta = 'your_backup_solution';
$args = array(
'post_type' => 'portfolio',
'orderby' => 'menu_order',
'order' => 'ASC',
'meta_value' => $the_meta,
'posts_per_page' => -1
);
$portfolio_query = new WP_Query($args);
// et cetera