Adding exta numeric parameter after slash in single post - wordpress

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.

Related

In wordpress where is the code that differentiates between categories and standard pages?

This is a question for Wordpress experts:
Where, in the Wordpress machine, is the code that decides whether to route a query to the standard page or to a category? Does it try to match specific strings in the URL looking for categories and then, if it doesn't get a match, default to the code that looks for the page in the wp_posts table?
This is something I have been wondering about for a while but I have found it difficult trying to trace the path of the query through the system.
Thanks for any insights you can give!
There is a function for this you can use to hook in a page or a category.
function is_category( $category = '' ) {
}
function is_page( $page = '' ) {
}
$category / $page can be ID, name, slug or array

Is it possible to change pagination permalinks in WP?

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

Display wp author meta url without 'http'

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.

how to retrieve custom field at root directory for wordpress?

i have create a redir.php and placed in the home directory of wordpress to do the redirect. I want to pass in the post id and then retrieve a custom field value of the post and feed into header('location:'.$url);
www.mysite.com/redir.php?id=30
in redir.php will retrieve post id=30 custom field value and pass it into $url.
this is what I have, but it's not working. It gives me "Parse error: parse error in \redir.php on line 5".
it looks like wordpress environment is not being loaded.
<?php
require('./wp-blog-header.php');
$id = $_GET['id'];
$url= get_field('deal_http_link',post->$id);
header('Location:'.$url);
?>
thanks
Your script has multiple issues:
There is whitespace before the opening <?php tag, so the redirect wouldn't work because the headers will have already been sent. Instead, <?php should be the very first thing in the file.
post->$id is invalid syntax. You probably meant the $id variable which you defined in the preceding line.
To retrieve the value of a custom field, use get_post_meta(), not get_field().
Try something like this instead:
<?php
require('./wp-blog-header.php');
$id = $_GET['id'];
$url = get_post_meta($id, 'deal_http_link', true);
header('Location: ' . $url);
exit();

Add Meta Post function not working

I am using add post meta function to save some data and its not working
<?php
//include '../../../wp-blog-header.php';
$unique = "true";
$pageID = $_GET['postID'];
echo "pageID:";
echo $pageID;
echo "</br>";
$num_posts = $_GET['num_posts'];
echo "num_posts: ";
echo $num_posts;
echo "</br>";
$num_posts_meta_key = "num_posts";
add_post_meta($pageID, $num_posts_meta_key, $num_posts , $unique) or update_post_meta($pageID, "num_posts" , $num_posts);
?>
Can someone help me out?
In first page I am getting all values from textboxes or checkboxes in javascript and then i am passing it in URL to next page where add_post_meta function is there.
I tried using method POST ...but then it doesnt work for me. It just submit the page and come back w/o doing anything on 1st page. I tried with GET method..but nothing works.
Hence I decided to take all values like num of post, post id in javascript and then from there pass it with url by using window.location.
I am very new to wordpress plugin coding. I thought POST method in my plugin is conflicting with some other post method in post.php..not sure though..
I am writing plugin for admin panel.
not sure what your problem is.. are you sure you're passing the right postID parameter? does the post exist in the database?
You don't really need to do add_post_meta() or update_post_meta.
From the manual:
The first thing this function will do
is make sure that $meta_key already
exists on $post_id. If it does not,
add_post_meta($post_id, $meta_key,
$meta_value) is called instead and its
result is returned.
<?php
// This minimum code should work, though you should really check that a post
// with this id does exist.
update_post_meta($_GET['postID'], "num_posts" , $_GET['num_posts']);
?>

Resources