Currently, I have my permalink structure set up such as https://example.com/blog/%postname%/ which makes it so that whenever a blog post is loaded, the URL shows /blog/. I would also like to add the subdirectory /try/ for specific pages. I have tried to use a plugin to add categories to pages so I can create a category and add it that way, but it seems that this is not possible because the "Custom Structure" is already set as noted above.
Does anyone know of a way to add a subdirectory /try/ to specific pages for a WordPress website so the URL for certain pages would be such as https://example.com/try/page-title
Thanks.
First we have to add rewrite_rule
add_rewrite_rule(
'^try/([^/]+)/?',
'index.php?name=$matches[1]',
'top'
);
Then we need to use the post_link filter to change the link format for certain posts
add_filter( 'post_link', 'custom_post_permalink', 10, 4 );
function custom_post_permalink( $link, $post = 0 ) {
$post_cur_id = $post->ID;
$post_cur_slug = $post->post_name;
//array of post_ids with try in permalink
$try_array = array(1,7,22,78);
if(in_array($post_cur_id, $try_array)) {
$slug = '/try/' . $post_cur_slug . '/';
$link = home_url($slug, 'https');
}
return $link;
}
The array $try_array should contain post_id's in which we change the link format
Related
I'm trying to set custom pages instead of the default category/archive pages of Wordpress.
I've copied the following script into the theme function.php, I mention that the site uses The7 Theme.
function loadPageFirst() {
// get the actual category
$actualCategory = get_category( get_query_var('cat') );
// get the page with the same slug
$matchingPage = get_page_by_path( $actualCategory->slug );
// If no match, load the normal listing template and exit (edit if you are using a custom listing template, eg. category.php)
if (!$matchingPage) {
include( get_template_directory() . '/archive.php');
die();
}
// Make a new query with the page's ID and load the page template
query_posts( 'page_id=' . $matchingPage->ID );
include( get_template_directory() . '/page.php');
die();
}
add_filter( 'category_template', 'loadPageFirst' );
I took it from here, it's bencergazda's solution.
Now, after the script, if I create a page, which has the same url as a category page it automatically replaces it.
The problem is that the script is limited to the main (parent) category.
I want to create a child page (let's say example.com/cars/european/german) that automatically replaces the same child category page.
My question is how to modify the script to include category children.
Run a try with this:
function loadPageFirst() {
// get the actual category
$actualCategory = get_category( get_query_var('cat') );
// get the page with the same slug
$matchingPage = get_page_by_path( $actualCategory->slug );
// If no match, load the normal listing template and exit (edit if you are using a custom listing template, eg. category.php)
if (!$matchingPage) {
include( get_template_directory() . '/archive.php');
die();
}
// Make a new query with the page's ID and load the page template
global $post; $post->ID = $matchingPage->ID;
query_posts( 'page_id=' . $matchingPage->ID );
include( get_template_directory() . '/page.php');
die();
}
add_filter( 'category_template', 'loadPageFirst' );
You can easily do this by using templates. Use a child theme and make a template called category-$slug.php and replace $slug for the slug you want to make a page for. You can also just make a category.php. For more options check this template hierarchy.
My WordPress site uses one default "post" and "books" custom post. I have two different archive page design for different post types (ile. author.php and books-archive.php).
Now, I want to create a custom user profile page with two links, "All Posts by user" and "All books by user". My current user archive page looks like as follows;
xxxxxxx.com/author/nilanchala
Can someone help me how to create two author archive pages filtered by post types? One for "Post" and other for "Books"?
Please do not suggest any plugin.
This is just an example and you should modify it the way you want, we will be using a custom query and rewrite rule to build the url
First thing you need to do is to create rewrite rule for the two custom query you want to display.
example, you have to reset the permalink in order for the new rewrite rule to take effect,
this is better to be created in a class and in a custom plugin so you can simply call flush_rewrite_rules() function
during plugin activation to reset the permalink.
function _custom_rewrite() {
// we are telling wordpress that if somebody access yoursite.com/all-post/user/username
// wordpress will do a request on this query var yoursite.com/index.php?query_type=all_post&uname=username
add_rewrite_rule( "^all-post/user/?(.+)/?$", 'index.php?query_type=all_post&uname=$matches[1]', "top");
}
function _custom_query( $vars ) {
// we will register the two custom query var on wordpress rewrite rule
$vars[] = 'query_type';
$vars[] = 'uname';
return $vars;
}
// Then add those two functions on thier appropriate hook and filter
add_action( 'init', '_custom_rewrite' );
add_filter( 'query_vars', '_custom_query' );
Now that you have build a custom URL you can then load a custom query on that custom url by creating a custom .php file as template and using template_include filter to load the template if the url/request contains query_type=all_post
function _template_loader($template){
// get the custom query var we registered
$query_var = get_query_var('query_type');
// load the custom template if ?query_type=all_post is found on wordpress url/request
if( $query_var == 'all_post' ){
return get_stylesheet_directory_uri() . 'whatever-filename-you-have.php';
}
return $template;
}
add_filter('template_include', '_template_loader');
You should then be able to access yoursite.com/index.php?query_type=all_post&uname=username or yoursite.com/all-post/user/username
and it should display whatever you put on that php file.
Now that you have the custom url and custom php file, you can start creating your custom query inside the php file to query post type based on user_nicename/author_name,
e.g.
<?php
// get the username based from uname value in query var request.
$user = get_query_var('uname');
// Query param
$arg = array(
'post_type' => 'books',
'posts_per_page' => -1,
'orderby' => 'date',
'order' => 'DESC',
'author_name' => $user;
);
//build query
$query = new WP_QUery( $arg );
// get query request
$books = $query->get_posts();
// check if there's any results
if ( $books ) {
echo '<pre>', print_r( $books, 1 ), '</pre>';
} else {
'Author Doesn\'t have any books';
}
I'm not sure why you need to build a custom query for all post as the default author profile loads all the default posts.
Hi Currently in our wordpress our default permalink structure is
abc.com/%postname%-%post_id%.html
But for specific category I want different permalink structure
Ex: for Photos Category i want url structure as
abc.com/%post_id%.html
Current i am using the following code But it is not working
add_filter( 'post_link', 'custom_permalink', 10, 3 );
function custom_permalink( $permalink, $post, $leavename ) {
// Get the categories for the post
$category = get_the_category($post->ID);
if ( !empty($category) && $category[0]->cat_name == "Photos" ) {
$permalink = home_url('/'.$post->ID .'.html' );
}
return $permalink;
}
But i am not getting different permalink structure for specific category posts. Please help me.
Posts must have a non-numeric slug because slug is an unique identifier.
So please use %post_id% in conjunction with other text. That will surely work.
The custom permalink url like abc.com/%post_id%.html for specific category post is not possible and the same topic is discussed on https://wordpress.org/support/topic/post_id-only-permalink-comes-with-date-and-death-links.
I'm having trouble setting up my Wordpress template. I'm not sure how to achieve the following. My website setup is the following:
I have a Custom Post Type "Chapter". A "Chapter" is parent to other CPT's. I have a few post types such as: reviews, interviews, blogposts, ...
On a chapter page I do a different WP_Query for every post type within this chapter.
page template single-chapter.php
Now I want to be able to click on "Blogpost (2)" and open an archive page of all blogpost within the current chapter. How can I achieve this? I assume I should create a template page "archive-blogpost.php". I already found that I can link to this page using:
<?php echo get_post_type_archive_link( 'blogpost' ); ?>
However I can't see how this page could know in what chapter i'm currently in?
I never used the WP Types plugin so far, but here is what you could do: add a chapter parameter to the archive link (with a rewrite rule for it), that you'll get in your blogposts archive template and display the posts conditionally to this parameter.
First we change the archive link to send the chapter slug:
<?php echo get_post_type_archive_link( 'blogpost' ) . '/' . $post->post_name; ?>
Then we define in functions.php this new rewrite tag:
function custom_rewrite_tag() {
add_rewrite_tag('%chapter%', '([^&]+)');
}
add_action('init', 'custom_rewrite_tag', 10, 0);
To make the URL looks good, we'll add a rewrite rule (so you don't have a url like /archive/?chapter=chapter-1, but /archive/chapter-1) - this still go to functions.php:
function custom_rewrite_rule($rules) {
add_rewrite_rule('^archive/([^/]+)/?$', 'index.php?post_type=blogposts&chapter=$matches[1]', 'top');
}
add_filter('init', 'custom_rewrite_rule', 10, 0);
You may have to change the URL / post type name depending of your configuration.
And last, you can get this query arg in your blogposts archive template with $wp_query->query_vars array:
$wp_query->query_vars['chapter']
Since I don't know much about WP Types, I'm not really sure about what follows, but it seems you could query the childs posts of the chapter with this:
if(isset($wp_query->query_vars['chapter']) && $chapter = get_page_by_path($wp_query->query_vars['chapter'])) {
$childargs = array(
'post_type' => 'blogposts',
'numberposts' => -1,
'meta_query' => array(array('
key' => '_wpcf_belongs_property_id', 'value' => $chapter->ID
))
);
$child_posts = get_posts($childargs);
} else {
// default template : display all posts
}
I used a WP types functions to achieve what I wanted:
$child_posts = types_child_posts("blogpost", array('post_id' => $_GET['wpv-pr-child-of']));
With this I can query all the child posts from the custom post type on a page.
I also use a session variable to keep track of the current chapter I'm in.
I am having trouble trying to pass an extra variable in the url to my WordPress installation.
For example /news?c=123
For some reason, it works only on the website root www.example.com?c=123 but it does not work if the url contains any more information www.example.com/news?c=123. I have the following code in my functions.php file in the theme directory.
if (isset($_GET['c']))
{
setcookie("cCookie", $_GET['c']);
}
if (isset($_SERVER['HTTP_REFERER']))
{
setcookie("rCookie", $_SERVER['HTTP_REFERER']);
}
Any Ideas?
To make the round trip "The WordPress Way" on the "front-end" (doesn't work in the context of wp-admin), you need to use 3 WordPress functions:
add_query_arg() - to create the URL with your new query variable ('c' in your example)
the query_vars filter - to modify the list of public query variables that WordPress knows about (this only works on the front-end, because the WP Query is not used on the back end - wp-admin - so this will also not be available in admin-ajax)
get_query_var() - to retrieve the value of your custom query variable passed in your URL.
Note: there's no need to even touch the superglobals ($_GET) if you do it this way.
Example
On the page where you need to create the link / set the query variable:
if it's a link back to this page, just adding the query variable
<a href="<?php echo esc_url( add_query_arg( 'c', $my_value_for_c ) )?>">
if it's a link to some other page
<a href="<?php echo esc_url(
add_query_arg( 'c', $my_value_for_c, site_url( '/some_other_page/' ) )
)?>">
In your functions.php, or some plugin file or custom class (front-end only):
function add_custom_query_var( $vars ){
$vars[] = "c";
return $vars;
}
add_filter( 'query_vars', 'add_custom_query_var' );
On the page / function where you wish to retrieve and work with the query var set in your URL:
$my_c = get_query_var( 'c' );
On the Back End (wp-admin)
On the back end we don't ever run wp(), so the main WP Query does not get run. As a result, there are no query vars and the query_vars hook is not run.
In this case, you'll need to revert to the more standard approach of examining your $_GET superglobal. The best way to do this is probably:
$my_c = filter_input( INPUT_GET, "c", FILTER_SANITIZE_STRING );
though in a pinch you could do the tried and true
$my_c = isset( $_GET['c'] ) ? $_GET['c'] : "";
or some variant thereof.
There are quite few solutions to tackle this issue. First you can go for a plugin if you want:
WordPress Quickie: Custom Query String Plugin
Or code manually, check out this post:
Passing Query String Parameters in WordPress URL
Also check out:
add_query_arg
Since this is a frequently visited post i thought to post my solution in case it helps anyone. In WordPress along with using query vars you can change permalinks too like this
www.example.com?c=123 to www.example.com/c/123
For this you have to add these lines of code in functions.php or your plugin base file.
From shankhan's anwer
add_filter( 'query_vars', 'addnew_query_vars', 10, 1 );
function addnew_query_vars($vars)
{
$vars[] = 'c'; // c is the name of variable you want to add
return $vars;
}
And additionally this snipped to add custom rewriting rules.
function custom_rewrite_basic()
{
add_rewrite_rule('^c/([0-9]+)/?', '?c=$1', 'top');
}
add_action('init', 'custom_rewrite_basic');
For the case where you need to add rewrite rules for a specifc page you can use that page slug to write a rewrite rule for that specific page. Like in the question OP has asked about
www.example.com/news?c=123 to www.example.com/news/123
We can change it to the desired behaviour by adding a little modification to our previous function.
function custom_rewrite_basic()
{
add_rewrite_rule('^news/([0-9]+)/?', 'news?c=$1', 'top');
}
add_action('init', 'custom_rewrite_basic');
Hoping that it becomes useful for someone.
add following code in function.php
add_filter( 'query_vars', 'addnew_query_vars', 10, 1 );
function addnew_query_vars($vars)
{
$vars[] = 'var1'; // var1 is the name of variable you want to add
return $vars;
}
then you will b able to use $_GET['var1']
<?php
$edit_post = add_query_arg('c', '123', 'news' );
?>
Go to New page
You can add any page inplace of "news".
One issue you might run into is is_home() returns true when a registered query_var is present in the home URL. For example, if http://example.com displays a static page instead of the blog, http://example.com/?c=123 will return the blog.
See https://core.trac.wordpress.org/ticket/25143 and https://wordpress.org/support/topic/adding-query-var-makes-front-page-missing/ for more info on this.
What you can do (if you're not attempting to affect the query) is use add_rewrite_endpoint(). It should be run during the init action as it affects the rewrite rules. Eg.
add_action( 'init', 'add_custom_setcookie_rewrite_endpoints' );
function add_custom_setcookie_rewrite_endpoints() {
//add ?c=123 endpoint with
//EP_ALL so endpoint is present across all places
//no effect on the query vars
add_rewrite_endpoint( 'c', EP_ALL, $query_vars = false );
}
This should give you access to $_GET['c'] when the url contains more information like www.example.com/news?c=123.
Remember to flush your rewrite rules after adding/modifying this.
to add parameter to post urls (to perma-links), i use this:
add_filter( 'post_type_link', 'append_query_string', 10, 2 );
function append_query_string( $url, $post )
{
return add_query_arg('my_pid',$post->ID, $url);
}
output:
http://yoursite.com/pagename?my_pid=12345678
This was the only way I could get this to work
add_action('init','add_query_args');
function add_query_args()
{
add_query_arg( 'var1', 'val1' );
}
http://codex.wordpress.org/Function_Reference/add_query_arg
In your case, Just add / after url and then put query arguments. like
www.example.com/news/?c=123 or news/?c=123
instead of
www.example.com/news?c=123 or news?c=123