I'm using WordPress twenty twelve theme as parent theme. I wanted to add numbered pagination to it so I found this code and I added it to my function.php file:
<?php
if ( ! function_exists( 'twentytwelve_content_nav' ) ) :
function twentytwelve_content_nav() {
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
) );
}
endif;
?>
which really worked fine the problem is I failed to add css styling as It is not warped in any ID or class which limits my ability to style it the way I planed. Is there any way to add class or Id to this code so I can style it?
I have checked the Wordpress codec, and there is no way to add a class or id in the paginate_links function.
Why not just wrap the echo statement in a container div?
?><div class="paginated-links"><?php
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
) );
?></div><?php
EDIT: To use this, replace the following code, with the code above:
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
) );
<?php $category = get_category( get_query_var( 'cat' ) );
$cat_id = $category->cat_ID;
?>
<?php
$page = (get_query_var('paged')) ? get_query_var('paged') : 1;
$wpquery = new WP_Query(array(
'order' => 'DESC',
'cat' => $cat_id,
'posts_per_page' => 10,
'paged'=>$page
));
while ($wpquery->have_posts()) {
$wpquery->the_post();
?>
<li class="contentlist">
<h3><?php the_title(); ?></h3>
<p><?php the_excerpt(); ?></p>
</li>
<?php
}
?>
<?php
global $wpquery;
if( $wpquery->max_num_pages >1){
$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' => $wpquery->max_num_pages
) );
}
?>
functions.php
// Pagination for each category
function custom_ppp( $query ) {
if ( !is_admin() && $query->is_category() && $query->is_main_query() ) {
$query->set( 'posts_per_page', '10' );
}
}
add_action( 'pre_get_posts', 'custom_ppp' );
?>
Related
I have set up CPT with a custom taxonomy, yet I cannot get pagination working. First page renders fine but when I click through to "/page/2/", I get a 404 error.
I have a CPT named "species_guide" and a custom taxonomy named "species".
I attempted the solution outlined here: https://wpza.net/how-to-paginate-a-custom-post-type-in-wordpress/ and https://wpza.net/how-to-a-paginate-custom-taxonomy-archive-in-wordpress/ but to no avail.
Below is my code, any input would be appreciated:
in taxonomy-species.php I have the following:
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$term_slug = get_query_var('species');
$args = array(
'post_type' => 'species_guide',
'tax_query' => array(
array(
'taxonomy' => 'species',//custom taxonomy name
'field' => 'slug',
'terms' => $term_slug
)
),
'posts_per_page' => 10,
'paged' => $paged
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
get_template_part( 'templates/post-archive' );
endwhile;
?>
<div class="pagination">
<?php
$big = 999999999;
echo paginate_links( array(
'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $loop->max_num_pages,
'prev_text' => '«',
'next_text' => '»'
) );
wp_reset_postdata();
?>
and then in functions.php I have the following
function custom_tax_query_change( $query ) {
if ( ! is_admin() && $query->is_tax( 'species' ) ) {
$query->set( 'posts_per_page', 10 );
}
}
add_action( 'pre_get_posts', 'custom_tax_query_change' );
Should I be using the different species slugs in the is_tax() function? Not sure why I can't get this working
Your taxonomy template name should be taxonomy-species.php to match the taxonomy name, see WordPress documentation
You don't need to create a new instance of WP_Query() for the loop, simply copy existing taxonomy.php or archive.php file and name the file as described above. Make HTML/PHP changes as needed.
Here is the updated snippet for pagination:
<?php
// for total pages in the loop.
global $wp_query;
$big = 999999999;
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,
'prev_text' => '«',
'next_text' => '»'
)
);
?>
i have a problem with my pagination, it doesn't seem to work when i click on page 3 or 4, when it clearly says i have 4 pages when i do $the_query->max_num_pages. I dont understand what the problem is. This is my code:
$big = 999999999; // need an unlikely integer
$translated = __( 'Page', 'mytextdomain' ); // Supply translatable string
var_dump($the_query->max_num_pages);
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => floor($the_query->max_num_pages),
'before_page_number' => '<span class="screen-reader-text">'.$translated.' </span>'
) );
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
// The Query
$the_query = new WP_Query([
$atts,
'posts_per_page' => 6,
'post__not_in'=> get_option('sticky_post'),
'ignore_sticky_posts' => 1,
'paged' => $paged,
]);
i am completely customizing the downloads page, i use the following code on downloads page, it shows the page number correctly, but when i go to other pages, it gives the same results as the first page
path : /themes/theme-name-child/woocommerce/myaccount/downloads.php
<?php
$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
$args = array(
'posts_per_page' => 6,
'paged' => $paged,
);
$the_query = new WP_Query( $args );
$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' => $the_query->max_num_pages
) );
?>
i searched a lot, but i didn't find anything, i need your help
Is this possible?
I am trying to get pagination working on my pages and single pages. It seems like it's working, as it recognizes how many pages there are and shows the URL. The problem: the URL is wrong!
Functions.php:
function pagination_bar( $query_wp )
{
$pages = $query_wp->max_num_pages;
$big = 999999999; // need an unlikely integer
if ($pages > 1)
{
$page_current = max(1, get_query_var('paged'));
echo paginate_links(array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => $page_current,
'total' => $pages,
));
}
}
The query:
$the_query = new WP_Query( array( 'posts_per_page' => 25,
'paged' => $paged
) );
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
The HTML:
<nav class="pagination">
<?php pagination_bar( $the_query ); ?>
What URL I am getting when pagination is activated and clicked upon the next page:
https://example.com/my_postorpagename/page/3/
What I want to get:
https://example.com/page/3/
Question: (how) can I do this?
Edit:
I also tried to edit it like
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( "https://mywebsitename.com%_%" ) ) ),
But then it gives me only my URL, without the /page/x...
Alright. I got it to work by changing the base like this:
From:
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
To:
'base' => str_replace( $big, '%#%', "http://example.com/page/$big" ),
I have a custom post type name portfolio.
I have applied pagination successfully but content on each page is the same.
Below is my code.
I have tried too many solutions but unable to find appropriate answer.
please help
<?php
$exec_query = new WP_Query( array (
'posts_per_page'=>10,
'post_type' => 'portfolio',
'job_role' => 'executive',
'post-thumbnails' => 'thumbnail',
'paged' => get_query_var('paged') ? get_query_var('paged') : 1) );
// The Loop
if ( $exec_query->have_posts() ) {
while ( $exec_query->have_posts() ): $exec_query->the_post(); ?>
<div>
// The content
</div>
<?php
endwhile;
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $exec_query->max_num_pages
) );
wp_reset_postdata();
}
?>
Try this code,
$the_query = new WP_Query( array('posts_per_page'=>10,
'post_type'=>'phcl',
'paged' => get_query_var('paged') ? get_query_var('paged') : 1)
);
?>
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
<div class="col-xs-12 file">
<a href="<?php echo $file; ?>" class="file-title" target="_blank">
<i class="fa fa-angle-right" aria-hidden="true"></i> <?php echo get_the_title(); ?>
</a>
<div class="file-description"><?php the_content(); ?></div>
</div>
<?php
endwhile;
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $the_query->max_num_pages
) );
wp_reset_postdata();
Try this
<?php
/*
Template name: Test portfolio
*/
$exec_query = new WP_Query( array (
'posts_per_page'=>10,
'post_type' => 'portfolio',
'job_role' => 'executive',
'post-thumbnails' => 'thumbnail',
'paged' => get_query_var('paged') ? get_query_var('paged') : 1) );
if ( $exec_query->have_posts() ) {
while ( $exec_query->have_posts() ): $exec_query->the_post(); ?>
<p><?php echo get_the_title(); ?></p>
<div><?php the_content(); ?></div>
</div>
<?php
endwhile;
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $exec_query->max_num_pages
) );
wp_reset_postdata();
}
?>