I am in trouble with the category page in wordpress, I want to display the pagination and I used a method that works in other templates that I developed in the past.
I get the Category ID
$category = get_category( get_query_var( 'cat' ) );
$cat_id = $category->cat_ID;
Declare the pagination
$page = (get_query_var('paged')) ? get_query_var('paged') : 1;
$wpquery = new WP_Query(array(
'order' => 'DESC',
'cat' => $cat_id,
'posts_per_page' => 4,
'paged'=>$page
));
And after my Loop I show the pagination
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
) );
}
If you try to go here
http://192.185.20.70/~t1t/tobeus.it/category/press/
you can see that the first & second page works, the third shows a 404...
in the other categories like "events"
http://192.185.20.70/~t1t/tobeus.it/category/events/
only the first page works...
I can't solve this strange problem!
I also had a problem with pagination in category template.
Try to change 'format' parameter 'paged' of paginate_links function to any other.
Example:
Set other parameter name, for example 'newparam'
echo paginate_links( array(
...
'format' => '?newparam=%#%',
Then get it from your url
$page = (get_query_var('newparam')) ? get_query_var('newparam') : 1;
Also don't forget to declare 'newparam' in functions.php file 'add_query_vars_filter' function.
Other solutions didn't worked for me, but this one did. I cannot explain exactly why, but it might be that parameter 'paged' is used somewhere when the second page is loading or something..
In this example you will just use other variable name to pass page number, and when the other pagination page will load, you will assign that value to 'paged' again, so it will load correct data from database.
Related
This is how I override the WordPress search page to implement better results thanks to Relevanssi:
global $wp_query;
$search_txt = get_query_var('s');
$args = array(
's' => $search_txt,
'post_type' => 'formations',
'posts_per_page' => 9,
'paged' => $wp_query->query_vars['paged'], // conserver le numéro de page de la requête initiale
);
// filtrer suivant la bonne taxonomy
if (isset($_GET['taxonomy'])) {
switch ($_GET['taxonomy']) {
case 'formation-diplomantes-cpf':
$ta = ['formation-diplomante', 'formation-eligible-au-cpf'];
$op = 'AND';
break;
case 'toute-formation':
break;
default:
$ta = $_GET['taxonomy'];
$op = 'IN';
}
if (isset($ta)) {
$tq = [[
'taxonomy' => 'type_form',
'field' => 'slug',
'terms' => $ta,
'operator' => $op,
]];// Tax Query
$args['tax_query'] = $tq;
}
}
$fq = new WP_Query();
$fq->parse_query( $args );
relevanssi_do_query($fq);
$any_formation = false;
$fdia = [];// Formations DIOGEN IDs Array
$fia = [];// Formations IDs Array
$i=0;
while ($fq->have_posts()) : $fq->the_post();
if ( 'formations' == get_post_type() ) {
$i++;
$fdia[get_the_ID()] = get_field('id_diogen', get_the_ID());
$fia[] = get_the_ID();
$any_formation = true;
}
endwhile;
?>
The results are paginated:
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $fq->max_num_pages
) );
This works well in most cases:
https://cdma.happy-dev.fr/?taxonomy=toute-formation&s=design
https://cdma.happy-dev.fr/page/2/?taxonomy=toute-formation&s=design
https://cdma.happy-dev.fr/page/3/?taxonomy=toute-formation&s=design
It fails in others:
https://cdma.happy-dev.fr/?taxonomy=toute-formation&s=sophie
https://cdma.happy-dev.fr/page/2/?taxonomy=toute-formation&s=sophie
https://cdma.happy-dev.fr/page/3/?taxonomy=toute-formation&s=sophie
I ended up understanding that pagination of my formations was not working when I didn’t have enough actualites. That is, say I have a query that brings 10 formations, and 20 actualites: everything works as expected. The opposite fails however.
This is how I rewrite URLs regarding actualites:
function custom_rewrite_rules( $wp_rewrite ) {
$wp_rewrite->rules = array(
'actualite/page/?([0-9]{1,})/?$' => $wp_rewrite->index . '?pagename=actualite&paged=' . $wp_rewrite->preg_index( 1 ),
) + $wp_rewrite->rules;
}
add_action( 'generate_rewrite_rules', 'custom_rewrite_rules' );
I tried to had a prefix to the search URL thinking that could play a role, but to no avail.
I also tried adding a custom rewrite rule for the search page, but it is not having any impact. I still get 404s.
I spent hours digging every possible direction but to no avail.
Any suggestion is most welcome.
I don’t see why both pagination of formations and actualites are related.
The polylang plugin was causing the trouble. Deactivating it solved the problem.
I am getting pagination links like -> /page/3. But i would like to change this "page" to "news". Is it possible?
Thank you!
echo paginate_links( array(
'base' => str_replace( $big2, '%#%', esc_url( get_pagenum_link( $big2 ) ) ),
'format' => '?paged1=%#%',
'current' => max( 1, get_query_var('paged1') ),
'total' => $news->max_num_pages,
'mid_size' => 0,
'type' => 'plain',
'end_size'=>0,
'prev_text' => '<IEPREKŠĒJĀ LAPA',
'next_text' => 'NĀKOŠĀ LAPA>'
) );
function my_custom_pagination_base() {
global $wp_rewrite;
$wp_rewrite->pagination_base = 'p'; //where new-slug is the slug you want to use
$wp_rewrite->flush_rules();
}
add_action('init', 'my_custom_pagination_base', 1);
You can change pagination base using below code
<?php
add_action( 'init', 'my_custom_page_word' );
function my_custom_page_word() {
global $wp_rewrite; // Get the global wordpress rewrite-rules/settings
// Change the base pagination property which sets the wordpress pagination slug.
$wp_rewrite->pagination_base = "news"; //where new-slug is the slug you want to use ;)
}
?>
For reference you can refer below link
https://wordpress.stackexchange.com/questions/57070/change-the-page-slug-in-pagination
To remove page or whatever your base is set to for your pagination do the following.
First add the following to functions.php. This function will rewrite our base pagination prefix to and empty string.
function my_custom_pagination_base() {
global $wp_rewrite;
$wp_rewrite->pagination_base = '';
$wp_rewrite->flush_rules();
}
add_action('init', 'my_custom_pagination_base', 1);
Second we add the our paginate_links() function to our archives page. with the following arguments. Make sure base is not set, or it will mess up our rewrite function.
echo paginate_links(array(
'format' => '\\%#%/',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages
));
Set your current and total args to whatever you need them to be.
Your archive pagination url should look like www.site.com/some-category/2/ when you go to page 2
In my custom theme in WordPress I have created a front-page.php. There I have three WP_Query's. The first one just takes a custom post type.
$args = array (
'post_type' => array( 'frontpage_element' ),
'orderby' => 'rand',
'posts_per_page' => '1',
);
$query = new WP_Query( $args );
if ($query->have_posts()):
while ($query->have_posts() ) : $query->the_post();
// DISPALY A PICTURE
endwhile;
// Reset postdata
wp_reset_postdata();
endif;
The second is the default Loop.
if (have_posts()) :
while (have_posts()) : the_post(); ?>
the_title();
the_content();
endwhile;
// Reset postdata
wp_reset_postdata();
endif;
The third WP_Query shows the first 6 post entries of a category.
$options = get_option('custom_theme_options');
$categories = implode("," , $options['catprojectlayout']);
$atts = shortcode_atts( array(
'paging' => 'projectpage',
'post_type' => 'post',
'posts_per_page' => '6',
'post_status' => 'publish',
'cat' => $categories,
'cache_results' => false
), $atts );
$paging = $atts['paging'];
unset( $atts['paging'] );
if( isset($_GET[$paging]) )
$atts['paged'] = $_GET[$paging];
else
$atts['paged'] = 1;
$custom_query = new WP_Query( $atts );
$pagination_base = add_query_arg( $paging, '%#%' );
if ($custom_query->have_posts()) : while ($custom_query->have_posts()) : $custom_query->the_post();
the_title();
the_content();
endwhile; endif;
echo paginate_links( array(
'type' => '',
'base' => $pagination_base,
'format' => '?'. $paging .'=%#%',
'current' => max( 1, $custom_query->get('paged') ),
'total' => $custom_query->max_num_pages,
'prev_text' => '<div class="prevbtn"></div>',
'next_text' => '<div class="nextbtn"></div>',
'show_all' => true
));
The code works, but I have troubles with caching. When I add a new Post to the defined category, the second WP_Query was not updated (I think because of browser caching).
I think I have the code done like is defined in WP Codex.
It should be work like this: When I add a new post in the defined category, a User browse to the front-page, the site must be reloaded from server (because in the third WP_Query there is a new entry).
it is ulikely to be browser caching as browsers tend to store style sheets and images page content is normally re-interpreted each time the page is loaded incase it has changed.
It is more likely to be either cashing on the server or another 3rd party service such as Cloud flare which store the page appearance for a period of time to save regenerating the same wedge of dynamic page controls over and over.
WP_Engine hosting has it's own caching that can be cleared through the installs dash board, Cloud flare allows cache to be cleared per URL and plugins... well these are all different, check if there are any plugins that control caching and either turn them off whilst you are making a change or clear their cache.
I'm having an issue with the pagination in search archives. For some reason whenever I try going to the next page the pagination gives me a 404.
It's a default if (have_posts()) : while (have_posts()) : the_post(); loop.
I don't know how, but I think the reason is because I'm getting redirected to a new page that get's generated somehow. Say I search for stoff. Instead of /page/2/?s=stoff it replaces it with ?attachment_id=550&s=stoff.
Here's what I've tried to do (with no different results)
Change the search query (attachment_id is the same here as well)
Change pagination functions (pagenavi, get_next_posts_link, custom function)
Replacing loop content with just a title
removing header, sidebars, footer
resetting post data
Resetting permalinks (pretty)
You can test the pagination live here (dev location)
Any thoughts on this are greatly appreciated :)
EDIT -
Added Pagination code currently used in search.php
I don't think it should be a problem with the pagination, seeing that I've tried so many different ones, and this pagination still works in every other location at the site, but here it is:
// Numeric Page Navi (built into the theme by default)
function bones_page_navi() {
global $wp_query;
$bignum = 999999999;
if ( $wp_query->max_num_pages <= 1 )
return;
echo '<nav class="pagination">';
echo paginate_links( array(
'base' => str_replace( $bignum, '%#%', html_entity_decode( get_pagenum_link($bignum) ) ),
'format' => '',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages,
'prev_text' => '←',
'next_text' => '→',
'type' => 'list',
'end_size' => 3,
'mid_size' => 3
) );
echo '</nav>';
} /* end page navi */
The function get's called after the endwhile;
I have such a piece of code
<?php
global $wp_query;
$args = array_merge( $wp_query->query_vars, array( 'post_type' => array( 'post', 'project') ) );
$wp_query = new WP_Query( $args );
if ( have_posts() ) : while ( have_posts() ) : the_post();
?>
after the loop i have
<?php
$permalink_structure = get_option('permalink_structure');
$format = empty( $permalink_structure ) ? '?paged=%#%' : 'page/%#%/';
echo paginate_links( array(
'base' => get_pagenum_link(1) .'%_%',
'format' => $format,
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages,
'prev_text' => __('«'),
'next_text' => __('»'),
'show_all' => false,
'mid_size' => 2,
'end_size' => 1,
) );
?>
now the problem is that if I have 6 posts and 18 projects and 3 posts per page ... paginate_links will generate (6+18)/3 pages i.e. 8 pages ... so I click on 2 and go to page number 2 .. but when I click on 3 .. I get error 404.
As if paginate_links generates the required amound of page links but only the links to 6/3 pages word .. like 1 and 2.
Problem is for sure because of custom post type added but I can't understand where is that problem.
What may be the problem?
It looks like you have to alter the "main query" (you are using a "sub query" inside the "main query") to include your custom post type, so your pagination links will work.
You can try to alter the "main query" using the pre_get_posts hook
add_action( 'pre_get_posts', 'my_pre_get_posts' );
function my_pre_get_posts( $query ) {
if($query->is_main_query() && $query->is_home()){ // <-- EDIT this condition to your needs
$query->set( 'post_type', array( 'post','projects' ) );
}
}
where you place this code into the functions.php file in your current theme directory.
This assumes you are using the pagination on the frontpage, i.e.
http://example.com/page/5
We have the condition $query->is_home() to check if we are on the frontpage. If you are on a different page, you can alter this condition to your needs.
ps: I think your way is not working because you are doing it in the theme file and that is "too late" to alter the scope of the pagination links.