How to assign serial no in WordPress Post summary Page? - php

I am trying to assign serial no in my WordPress Post Category summary Page. For this I have written following code. It is working fine but problem is in the case of paging.
When I am going to 2nd page or 3rd page it assigns the no from 1 again. Currently I am showing 5 posts/per page. Then in 2nd page it should show post no starting from 6 but it starts from 1 again. Same to 3rd and other pages. How can I show it in 1st page from 1-5 and in 2nd page 6-10 etc?
Below is my code:
<?php
// args
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$args = array(
'paged' => $paged,
'post_type' => 'post',
'cat' => 5,
);
$the_query = new WP_Query( $args );
$postNumber = 1;// to give serial no to post
?>
<?php if($the_query->have_posts() ): ?>
<?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php echo $postNumber++; ?> <?php the_title(); ?>
<?php the_excerpt(); ?>
<?php endwhile; ?>
<?php custom_numeric_posts_nav(); ?>
<?php endif; ?>
<?php wp_reset_query();

Taking help from this topic https://wordpress.stackexchange.com/questions/155903/show-number-of-posts-and-number-on-current-page I have been able to solve my problem. Below is my working code:
<?php
// args
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$ppp = get_query_var('posts_per_page');
$end = $ppp * $paged;
$args = array(
'paged' => $paged,
'post_type' => 'post',
'cat' => 5,
);
$the_query = new WP_Query( $args );
$postNumber = $end - $ppp + 1;// to give serial no to post
?>

Related

Not getting paging links when using WP_Query in Wordpress

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();
?>

Wordpress Pagination Only showing 2 pagess

I've set up a basic homepage to show 3 articles per page with pagination to navigate through these pages. At the moment it'll only show pagination for pages 1 & 2 and no more, even though I have 12 articles which result in 4 pages. I'm not quite sure where I'm going wrong here:
<?php
$paged = (get_query_var('paged'))? get_query_var('paged') : '1';
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'paged' => $paged,
'posts_per_page' => 2
);
$the_query = new WP_Query( $args );
while ($the_query -> have_posts()) : $the_query -> the_post();
include(locate_template('content-post.php' ));
endwhile;
?>
<?php the_posts_pagination( array('mid_size' => 3) ); ?>
the_posts_pagination use default WP query so it not work here. Can you please try below code:
<?php
if ( get_query_var('paged') ) { $paged = get_query_var('paged'); } else if ( get_query_var('page') ) {$paged = get_query_var('page'); } else {$paged = 1; }
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'paged' => $paged,
'posts_per_page' => 2
);
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query($args);
while ($wp_query -> have_posts()) : $wp_query -> the_post();
include(locate_template('content-post.php' ));
endwhile;
the_posts_pagination( array('mid_size' => 3) );
$wp_query = null;
$wp_query = $temp;
wp_reset_query();
?>
Code is tested in work perfect.
Your code looks alright to me. I was having the same issue, I resolved it by matching the posts_per_page with the Blog pages show at most field located in Settings->Reading in the admin dashboard
example:
"posts_per_page" => 6 then Blog pages show at most should be also 6
I hope this helps.
Use this plugin Click here
and use this shortcode for pagination <?php wp_pagenavi(); ?>
and I use exact following loop for my project and its working.
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args= array(
'cat' => '',
'orderby'=> 'date',
'order'=> 'DESC',
'paged' => $paged ,
'posts_per_page' => 3
);
query_posts($args);
if (have_posts()) :
while (have_posts()):
the_post();
endwhile;
endif;
?>
Please try it. Hope it will work for you also.
And If you are displaying your posts on home page, you need to replace
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
with
<?php
if ( get_query_var('paged') )
{
$paged = get_query_var('paged');
}
else if ( get_query_var('page') )
{
$paged = get_query_var('page');
}
else
{
$paged = 1;
}
The problem is that $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; retrieves the data of the main query object but not for the custom one.
I found a solution here https://wordpress.stackexchange.com/questions/120407/how-to-fix-pagination-for-custom-loops

Creating paging within a page.php wordpress

I am trying to create a paging within the page.php my thema and is not showing up ! How do?
I'm doing as follows:
Loop:
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$query_prof = array('post_type' => 'Professor', 'posts_per_page' => 10, 'paged'=>$paged);
$profLoop = new WP_Query($query_prof);
if( $profLoop->have_posts()) : while( $profLoop->have_posts()) : $profLoop->the_post();
get_template_part('partials/content','professores'); ?>
<?php endwhile;
?>
calling on page:
<?php wp_pagenavi( array( 'type' => 'Professor' ) ); ?>
Paging appears , no more list is the first direct

Wordpress Kriesi Pagination Issue

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 );

Having trouble displaying posts from only one category in Wordpress

I'm having difficulty pulling only the desired category into an archive page on Wordpress. My code is below, I thought by defining get-category-by-slug this would work, but instead its pulling all of the post from every category into the page.
<?php
$category = get_category_by_slug('weddings');
$args = array(
'post_type' => 'elp_projects',
'posts_per_page' => 12,
'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1)
);
query_posts($args);
$x = 0;
while (have_posts()) : the_post();
?>
Any ideas on how to fix this would be appreciated.
I've also tried these combinations with no luck.
<?php
$category = get_category_by_slug('weddings');
$args = array(
'post_type' => 'elp_projects',
'posts_per_page' => 12,
'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1)
);
$query = new WP_Query( 'category_name=weddings' );
$x = 0;
while (have_posts()) : the_post();
?>
and
<?php $query = new WP_Query( 'category_name=weddings' ); ?>
and
<?php
$args = array(
'post_type' => 'elp_projects',
'posts_per_page' => 12,
'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1)
);
$query = new WP_Query( 'category_name=weddings' );
$x = 0;
while (have_posts()) : the_post();
?>
Please do not use query_posts its not the best way to query data its a last resort if you will.
Instead use WP_Query to get posts from one category just do $query = new WP_Query( 'category_name=staff' ); refer to this page for more information on how to get posts from one category using WP_Query.
EDITED
Try this
$the_query = new WP_Query( array(
'post_type' => 'page',
'orderby' => 'date',
'category_name' => 'wedding', //name of category by slug
'order' => 'DESC',
'posts_per_page' => )); // how many posts to show
// Put into the loop
while ( $the_query->have_posts() ) :
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
endwhile;
// Restore original Post Data if needed
wp_reset_postdata();

Categories