I am not getting paging links when using WP_Query in WordPress. I am trying to fetch all the products on the page and show them in page by page manner. For this I want to show the paging links on the bottom. But paging links are not showing.
Below is the link - here
And the code for this is:
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'post_type' => 'product',
'posts_per_page' => 3,
'paged' => $paged
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
echo '<br />' . woocommerce_get_product_thumbnail().' '.get_the_title().'';
endwhile;
?>
<?php next_posts_link(); ?>
<?php previous_posts_link(); ?>
<? wp_reset_query(); ?>
next_posts_link(); and previous_posts_link(); and not showing links.
Please help.
Use this code instead of of above code.
<?php
$temp = $wp_query;
$wp_query= null;
$postsPerPage = 3;
$argsev = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => $postsPerPage,
'paged' => $paged
);
$wp_query = new WP_Query($argsev);
while ( $wp_query->have_posts() ) : $wp_query->the_post();
global $product;
echo '<br />' . woocommerce_get_product_thumbnail().' '.get_the_title().'';
endwhile; ?>
<?php next_posts_link(); ?>
<?php previous_posts_link(); ?>
<?php
$wp_query = null; $wp_query = $temp;
wp_reset_query();
?>
Related
I have to customize a function.
That fonction display custom posts on a (custom) category page.
The problem of this function concerns the absence of text when there is no results.
For instance, on any category page, if there is no available post to show in the grid, I would like to display a text such as "There is no post in this category. Please try another category."
Here is the code:
global $paged, $wp_query, $wp;
$args = wp_parse_args($wp->matched_query);
if ( !empty ( $args['paged'] ) && 0 == $paged ) {
$wp_query->set('paged', $args['paged']);
$paged = $args['paged'];
}
$cat_id = get_queried_object_id();
$temp = $wp_query;
$featuredPosts = array();
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => $classieraFeaturedAdsCounter,
'paged' => $paged,
'cat' => $cat_id,
'meta_query' => array(
array(
'key' => 'featured_post',
'value' => '1',
'compare' => '=='
)
),
);
$wp_query= null;
$wp_query = new WP_Query($args);
while ($wp_query->have_posts()) : $wp_query->the_post();
$featuredPosts[] = $post->ID;
get_template_part( 'templates/classiera-loops/loop-ivy');
endwhile;
wp_reset_postdata();
wp_reset_query();
Does anyone has an idea to include this option?
Thanks.
SOLUTION:
wp_query = new WP_Query($args);
if ( have_posts() ) :
while ($wp_query->have_posts()) : $wp_query->the_post();
$featuredPosts[] = $post->ID;
get_template_part( 'templates/classiera-loops/loop-ivy');
endwhile;
else :
echo wpautop('No result');
endif;
I'm not a WP developer, but it seems to be
if ($wp_query->have_posts()) {
while ($wp_query->have_posts()) : $wp_query->the_post();
} else {
echo 'No post here';
}
Reference link : https://codex.wordpress.org/Function_Reference/have_posts
I'm trying to exclude posts by the author with the ID "1" form WP_Query. This still shows all posts by all users.
Any thoughts?
<?php
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query( array( 'author' => -1 ) );
$wp_query->query('posts_per_page=35'.'&paged='.$paged);
while ($wp_query->have_posts()) : $wp_query->the_post();
?>
<div class="grid__third">
...
</div>
<?php endwhile; // end of loop
?>
Thanks a lot!
try this
global $post;
$args=array('author' => '-1', //excludes users with id 1.
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 12,
'ignore_sticky_posts'=> 1,);
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
<li>
<?php the_title(); ?>
</li>
<?php endforeach;
wp_reset_postdata();?>
or
// The Query
$args=array('author' => '-1', //excludes users with id 1.
'post_type' => 'post','post_status' => 'publish','posts_per_page' => 12,'ignore_sticky_posts'=> 1,);
$the_query = new WP_Query( $args ); // The Loop
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
/* Restore original Post Data */
wp_reset_postdata();}else {
// no posts found
}
If your query is not working with
$wp_query = new WP_Query( array( 'author' => -1 ) );
then, You can try some other logic. Like inside the loop get the ID of the author by using POST ID:
$post_author_id = get_post_field( 'post_author', $post_id ); // You will get your author id here
Then apply condition like if($post_author_id != 1){ // YOUR POST } This will display post of all author except author with ID = 1.
Full code like:
while ($wp_query->have_posts()) : $wp_query->the_post();
$post_author_id = get_post_field( 'post_author', HERE YOUR POST ID );
if($post_author_id != 1){
?>
<div class="grid__third">
...
</div>
<?php
} endwhile; // end of loop
Hope this will helpful for you. Thanks.
So, I have the following to display post loops (wordpress):
METHOD A (works fine)
<?php
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} elseif ( get_query_var('page') ) {
$paged = get_query_var('page');
} else {
$paged = 1;
}
$args = array(
'post_type' => 'post',
'paged'=>$paged,
'posts_per_page' => 7,
'orderby' => 'date',
'order' => 'DESC'
);
$loop = new WP_Query( $args );
$id = get_the_ID();
global $paged;
while ( $loop->have_posts() ) : $loop->the_post();
?>
Posts go here
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<nav id="rh_nav_below">
<ul>
<li class="rh_nav_previous"><?php previous_posts_link( '« PREV', $loop->max_num_pages) ?></li>
<li class="rh_nav_next"><?php next_posts_link( 'NEXT »', $loop->max_num_pages) ?></li>
</ul>
</nav>
Now in the author page, following is used to display a post (a single posts):
METHOD B (works fine)
<?php rewind_posts(); while (have_posts()) : the_post(); ?>
<?php the_title(); ?>
<?php endwhile; ?>
I have been trying to change the author post (method b) to method A format, so I can control the number of posts, orderby and etc.
Here is what I have tried:
<?php
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} elseif ( get_query_var('page') ) {
$paged = get_query_var('page');
} else {
$paged = 1;
}
$rhp_author_profile_id = get_the_author_id();
$args = array(
'post_type' => 'post',
'paged'=>$paged,
'posts_per_page' => 7,
'orderby' => 'date',
'order' => 'DESC',
'author ' => $rhp_author_profile_id
);
$loop = new WP_Query( $args );
$id = get_the_ID();
global $paged;
while ( $loop->have_posts() ) : $loop->the_post();
?>
Posts show here.
However, I am only getting the admin's posts on every other authors.
What am I doing wrong?
Thanks
Try This
<?php
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} elseif ( get_query_var('page') ) {
$paged = get_query_var('page');
} else {
$paged = 1;
}
global $current_user;
get_currentuserinfo();
$args = array(
'post_type' => 'post',
'paged'=>$paged,
'posts_per_page' => 7,
'orderby' => 'date',
'order' => 'DESC',
'author ' => $current_user->ID
);
$loop = new WP_Query( $args );
$id = get_the_ID();
global $paged;
while ( $loop->have_posts() ) : $loop->the_post();
?>
I have custom query on my wordpress page. Query looks like this:
$args = array(
'post_type' => array( 'tworca' ),
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page'=>12,
'post_parent' => 0
);
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args['paged'] = $paged;
// The Query
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
... this is content of my query.....
}
?>
<nav>
<?php previous_posts_link('« Newer') ?>
<?php next_posts_link('Older »') ?>
<?php wp_pagenavi(); ?>
</nav>
<?php
} else {
echo 'no results';
}
/* Restore original Post Data */
wp_reset_postdata();
?>
But my pagination doesn't show. Any idea why? I have read many posts with similar problem, but solution always was to add 'paged' parameter to wp query. And I have this parameter in my query and it doesn't help.
Thanks in advance for your help!
Try This..
`$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'posts_per_page' => 12,
'paged' => $paged,
'orderby' => 'title',
'order' => 'ASC',
'post_parent' => 0,
'post_status' => 'publish',
);
query_posts( $args );
if ( have_posts() ) {
$i = 0;
while ( have_posts() ) {
the_post();
Your data here you want display like title
the_title();
}
wp_pagenavi();
}
wp_reset_query();
?>`
Please try by adding your query object to the wp_pagenavi(), which would be like,
wp_pagenavi( array( 'query' => $the_query ) );
Reference: http://scribu.net/wordpress/wp-pagenavi/wpn-2-74.html
Change wp_pagenavi() to wp_pagenavi( array( 'query' => $the_query) );
I'm having trouble figuring out this pagination issue on my site. The problem is that page 2 shows the same content as page 1. It's not supposed to do that.
<?php
$args = array( 'post_type' => 'baseball-news', 'posts_per_page' => 5 );
$baseball_loop = new WP_Query( $args );
while ( $baseball_loop->have_posts() ) : $baseball_loop->the_post();
?>
<?php
if ( get_post_type() == 'baseball-news' ) : ?>
<?php include( TEMPLATEPATH . '/includes/show-baseball-posts.php' ); ?>
<?php endif; ?>
<?php endwhile; ?>
<?php kriesi_pagination($baseball_loop->max_num_pages); ?>
<?php wp_reset_query(); ?>
This is the site for Kriesi pagination.
Site.
You are not using pagination parameter paged in your query. You use it like this:
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; ?>
$args = array( 'post_type' => 'baseball-news', 'posts_per_page' => 5 , 'paged' => $paged );