The default link for pagination looks like "site.com/articles/page/2", but I need to do the page number as parameter(example: "site.com/articles?page=2"). I've tried to use add_filter function:
add_filter('get_pagenum_link', 'edit_paginate_url');
function edit_paginate_url($url){
$pagenum = preg_match("/\/\d\//", $url, $pagenum);
$pagenum = preg_replace("/\//", "", $pagenum);
$url = preg_replace("/\/page\/\d/", "?page=", $url);
return $url . $pagenum;
}
But it didn't work. It returns url like: "site.com/articles0/page/2/" (this filter only adds zero symbol before pagination part of url).
Follow the below url in which you have to set base and format as per your requirements.
https://codex.wordpress.org/Function_Reference/paginate_links
Related
I want to add several parameters with slash not by question mark in Wordpress Post URL. Thanks in advance.
You can achieve by WordPress Standard. See below quick step and follow WordPress docx for specific functions/filters, if you get stuck.
You need to use add_rewrite_rule as per WordPress Standard to pass query string as a slash based URL
add_rewrite_rule(
'^pageslug/([^/]+)([/]?)(.*)',
//!IMPORTANT! THIS MUST BE IN SINGLE QUOTES!:
'index.php?pagename=pageslug&page_id=$matches[1]',
'top'
);
});
This filter is used to target the query string variable.
add_filter('query_vars', function( $vars ){
$vars[] = 'pagename';
$vars[] = 'page_id';
return $vars;
});
Update permalink settings
This will be used to get the value in a specific page
get_query_var( 'page_id' )
I have single post URL for example
http://www.example.com/single-post
I want to add numeric parameter after post slug like this
http://www.example.com/single-post/1
http://www.example.com/single-post/2
http://www.example.com/single-post/
I have tried many thing. None of its working..
Thanks
Please checkadd_query_arg() and link
I don't know what you are trying to achieve, but you should consider to add query arg at the end of the url instead of what you asked for.
Query args looks like this: http://www.example.com/single-post?post_number=1.
<?php
/* We Add query args at the end of the url
* If you click Single post 1 your url should be like:
* http://www.example.com/single-post?post_number=1
*/
$singlePost_1 = esc_url( add_query_arg( 'post_number', '1' ) );
$singlePost_2 = esc_url( add_query_arg( 'post_number', '2' ) ); ?>
Single post
Single post 1
Single post 2
How to use query args
You can get query args with $_GET['var_name'], but you should keep in mind using esc_url() when you get the final result to sanitize the url.
$postNumber = $_GET['post_number'] != null ? esc_url( $_GET['post_number'] ) : '';
When url has ?post_number=2 at the end $postNumber will be 2
Read more here about add_query_arg() esc_url
Hope this helps.
I am in a mesh and need some help.
There are three actor evolved.
Live Site / Production Site (wordpress)
Local Site / Stage / Testing Site (wordpress)
Third party analytic
Background:
I know its very bad but the fact is that the production sites and local sites content are not synced. i.e post id 1 in production can be as post id 24 in local.
Problem Definition:
I was assigned to use the third party's API to grab the list of top post with maximum hits and show it in our website.
$result = file_get_contents($url);
$result_json = json_decode($result, true);
The above lines did that for me easily.
Error:
Since the 3rd party did not had post excerpt, they send URL, image_link, hit_count, post title and author information as JSON.
So on my site I was able to show all these fields very well. But the problem started when my manager added post excerpt to the list.
I tried converting URL to postid and got the post excerpt using:
get_the_excerpt(url_to_postid($top_posts['link']))
It worked for live site. but in the stage and local its not working.
Well post URL has its domain name. Even when I replaced the domain name with my local or stage domain. its not showing any thing. and for some post when it shows the excerpt its not from the same article.
Guys need some idea.
Is there some way that I can get slug from URL?
I tried using explode() function. But sometime the slug is not the last item in array.
Thank you for reading it all. I appreciate you help.
For now I came with a solution. Primarily Using the URL to get post ID, if not found using post title to get the post id.
public static function get_excerpt ( $id = null ) {
$post = get_post($id);
if ( empty($post) ) {
return '';
}
if ( strlen($post->post_excerpt) ) {
// Use the excerpt
$excerpt = $post->post_excerpt;
$excerpt = apply_filters('the_excerpt', $excerpt);
} else {
// Make excerpt
$content = $post->post_content;
$content = strip_shortcodes($content);
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
$excerpt_length = apply_filters('excerpt_length', 55);
$excerpt_more = apply_filters('excerpt_more', ' ' . '[…]');
$excerpt = wp_trim_words($content, $excerpt_length, $excerpt_more);
$excerpt = wpautop($excerpt);
}
return apply_filters('wp_trim_excerpt', $excerpt);
}
Called this function to get the excerpt.
$postid = url_to_postid($top_posts['link']);
$postid = $postid!=0 ? $postid : get_page_by_title($post_title, OBJECT, 'post')->ID;
$excerpt = self::get_excerpt($postid);
This worked in my case as I didn't have posts with same title.
I am still waiting for some better solution to my problem.
I'm using a setting from my plugin to generate the rewrite rule:
$bioPage = $wpdb->get_var("SELECT settingValue FROM $table_name WHERE settingType='bioPage'");
$post = get_post($bioPage);
$slug = $post->post_name;
add_rewrite_tag('%player%','([^&]+)');
add_rewrite_rule('^'.$slug.'/([^/]*)$','/'.$slug.'/?player=$matches[1]','top');
I am getting the correct slug.
I have a url that looks like this:
mysite.com/info/?player=joe.smith.01
I want it to look like this:
mysiste.com/info/joe.smith.01
I'm using the add_rewrite_tag so WP will grab the variable for the url. When I visit a page url structured like what I'm trying to achieve I get a page not found.
I would not recommend modifying the htaccess file if you don't really have to. WP's Rewrite APi should do just fine.
That said, I'm pretty sure you have to go via 'index.php' in the rewrite argument of add_rewrite_rule().
I don't have any way of testing this right now, but something along the lines of the following might work:
$bioPage = $wpdb->get_var("SELECT settingValue FROM $table_name WHERE settingType='bioPage'");
$post = get_post($bioPage);
$slug = $post->post_name;
$page_id = $post->ID; //Get the post ID to include in the page_id query var
add_rewrite_tag('%player%','([^&]+)');
add_rewrite_rule('^'.$slug.'/([^/]*)$','index.php?page_id='.$page_id'.&player=$matches[1]','top');
I'm using the WP function: get_the_author_meta('user_url');
When I echo that to the browser it automatically prepends 'http://' to the URL. How can I avoid this, so that my URLs show exactly as they are entered on the user settings page.
Thanks is advance.
$author_url = get_the_author_meta('user_url'); // e.g. http://www.example.com
$to_remove = array( 'http://', 'https://' );
foreach ( $to_remove as $item ) {
$author_url = str_replace($item, '', $author_url); // to: www.example.com
}
echo $author_url; // now it will not have the http:// part you wish to avoid.
str_replace is probably the most efficient way of doing this.
$author_url = str_replace(array( 'http://', 'https://' ), '', get_the_author_meta('user_url'));
Other URL modification techniques.
For more complicated string replacements which match you could look at using preg_replace .
There is a function called http-build-url() which is included in the php_http.dll extension which might be more suitable for some use cases.