change pagination link in WordPress - wordpress

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

Related

Exclude children of custom post type from search

I have a custom post type of 'location'. I then have children pages for each of the pages for that cpt. so it looks something like this, "www.example.com/location/location-name/child-page/", each child page is using a post template of "location-product-services.php". So what I am trying to do is exclude from the search results the children of this cpt.
I am trying to do it by checking the meta data to see if it is using that template. I just cant seem to get it working. Any help would be great.
This is what I currently have -
// Exclude local product and services pages from search result.
function location_subpages_exclude_search( $query ) {
if ( is_search() && !is_admin()) {
$query->set('meta_query',
array(
'key' => '_wp_page_template',
'value' => 'location-product-services.php',
'compare' => '!='
)
);
}
}
add_action('pre_get_posts', 'location_subpages_exclude_search');
Thanks in advance.
First, I pretty much exclusively use the Relevanssi plugin any time I want to modify search. But to search programmatically, I think this is what you're after:
$taxonomy = 'location';
$terms = get_terms($taxonomy, array( 'parent' => 0, 'search' => $query ) );
if ( $terms && !is_wp_error( $terms ) ) :
?>
<ul>
<?php foreach ( $terms as $term ) { ?>
<li><?php echo $term->name; ?></li>
<?php } ?>
</ul>
<?php endif;?>
Use the function get_terms to search your CPT, the 'search' is your $query (you might consider wrapping the search string with the SQL wildcard '%') and 'parent'=>0 returns only the top level.
I figured it out.
First I got all parent pages of my post type, used get_pages() grab them all.
Looped through each of the parent pages and ran another get_pages() for children of that parent.
function SearchFilter($query) {
if ($query->is_search) {
$args = array(
'hierarchical' => 0,
'post_type' => 'location',
'parent' => 0, //returns all top level pages
'post_per_page' => -1
);
$parents = get_pages($args);
$excludes = array();
foreach($parents as $parent) :
$args = array(
'post_type' => 'location',
'child_of' => $parent->ID,
'post_per_page' => -1
);
$children = get_pages($args);
foreach($children as $child):
array_push($excludes, $child->ID);
endforeach;
endforeach;
$query->set('post__not_in', $excludes);
}
return $query;
}
add_filter('pre_get_posts','SearchFilter');

wp_list_comments function not working in wordpress 4.3.16 version

I am adding load more comments button in comments section. i want load function wp_list_comments using Ajax, function are loading but wp_list_comment not display in wordpress 4.3.16 version. How to solve this problem???
My code are is:
// maybe it isn't the best way to declare global $post variable, but it is simple and works perfectly!
add_action('wp_ajax_cloadmore', 'misha_comments_loadmore_handler');
add_action('wp_ajax_nopriv_cloadmore', 'misha_comments_loadmore_handler');
function misha_comments_loadmore_handler(){
global $post;
$post = get_post( $_POST['post_id'] );
setup_postdata( $post );
// actually we must copy the params from wp_list_comments() used in our theme
wp_list_comments( array(
'page' => $_POST['cpage'], // current comment page
'per_page' => get_option('comments_per_page'),
'style' => '<div>', // comments won't wrapped in this tag and it is awesome!
'short_ping' => true,
) );
die; // don't forget this thing if you don't want "0" to be displayed
}
Try this code.
add_action('wp_ajax_cloadmore', 'misha_comments_loadmore_handler');
add_action('wp_ajax_nopriv_cloadmore', 'misha_comments_loadmore_handler');
function misha_comments_loadmore_handler(){
$comments = get_comments(array(
'post_id' => $_POST['post_id'],
'status' => 'approve'
));
wp_list_comments(array(
'page' => $_POST['cpage'], // current comment page
'per_page' => get_option('comments_per_page'),
'style' => '<div>', // comments won't wrapped in this tag and it is awesome!
'short_ping' => true,
), $comments);
die; // don't forget this thing if you don't want "0" to be displayed
}

Trouble with pagination wordpress category page

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.

Main loop with custom post type added - paginate_links doesn't work well

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.

How to implement Wordpress pagination?

I'm trying to implement pagination for my posts. Though I'm a bit stuck on the php function and also how to call it.
The simple method is this I guess:
<?php posts_nav_link(); ?>
But what if I want custom pagination?
Here is my current code:
<?php
global $wp_query;
$total = $wp_query->max_num_pages;
if ( $total > 1 ) {
if ( !$current_page = get_query_var('paged') )
$current_page = 1;
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'format' => '?paged=%#%',
'current' => $current_page,
'total' => $total,
'mid_size' => 4,
'type' => 'list'
));
}
?>
Is this correct and how do I call it? In index.php? Where in the loop? Thanks.
Edit for clarification: How do I implement this code? At the moment I have put it in my functions.php. So how (and where in the loop) do I 'reference' this function so the pagination is displayed.
There are two ways you can implement this code. It looks like right now you are sort of in between the two.
The first way would be to add your pagination code directly into the template that it will be used in somewhere inside the loop (most likely somewhere right before the closing <?php endwhile; ?> tag). If you are using a single.php template, you would put it in there, if not, put it in index.php. The placing of it inside of the loop depends on where you want the pagination to appear on your page.
The second way is to add the pagination code to the functions.php file (which you have done). However, you will need to revise your code a bit for this. You need to wrap the code within a function, and name that function something. I've used your_custom_pagination for an example. Your functions.php file is most likely already wrapped in php tags, so I have removed those.
function your_custom_pagination() {
global $wp_query;
$total = $wp_query->max_num_pages;
if ( $total > 1 ) {
if ( !$current_page = get_query_var('paged') ) {
$current_page = 1;
}
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'format' => '?paged=%#%',
'current' => $current_page,
'total' => $total,
'mid_size' => 4,
'type' => 'list'
));
}
}
Then you'll need to go into the template that you are using and place this code <?php your_custom_pagination(); ?> into the same spot I illustrated above to call the pagination function.
I haven't actually tested your code, so assuming it's valid everything should work.

Resources