How to remove custom taxonomy-base from URL in WordPress - php

Please help me in wordpress.
I want to remove custom texnonomy base from URL in wordpress
http://domainname/travel-category/beaches
to
http://domainname/beaches
where "travel-category" is custom register taxonomy base and "beaches" is category
MY CODE IS :
register_taxonomy(
'travel-category', //taxonomy base
'destination', // custom post type
...
);

DO one thing add the following code for the rewrite argument for register_taxonomy function:
'rewrite' => array( 'slug' => '/', 'with_front' => FALSE ),
Hope this works and if not try refreshing the permalink page.

Related

Is it possible to combine tag URL with category URL?

I need a tag URL that looks like
site/category/my-tag or site/category/sub-category/my-tag
Is it possible to make this?
You can rewrite the slug path for your custom tag:
'rewrite' => array( 'slug' => 'category/sub-category/my-tag', 'hierarchical' => true ) );
Add this to your custom taxonomy tag.

How to change Wordpress' default posts permalinks programmatically?

In the backend of Wordpress I use the default http://localhost/sitename/example-post/ value for creating permalinks.
For custom post types I defined a custom slug this way, here is services for example:
register_post_type( 'service',
array(
'labels' => array(
'name' => __( 'Services' ),
'singular_name' => __( 'Service' )
),
'public' => true,
'has_archive' => true,
'rewrite' => array(
'slug' => 'services',
'with_front' => true
),
'supports' => array(
'title',
'editor',
'excerpt',
'thumbnail'
),
'taxonomies' => array( 'category' ),
)
);
It creates services/post-name.
I also use this hook to create a custom page to create a custom page permalink:
function custom_base_rules() {
global $wp_rewrite;
$wp_rewrite->page_structure = $wp_rewrite->root . '/page/%pagename%/';
}
add_action( 'init', 'custom_base_rules' );
It creates page/post-name
Now the only thing I need to do is to create another custom permalink path for the normal Wordpress posts.
So the outcome world be for the post type of post:
post/post-name
I can't use the backed for this because I already defined a default way of handling the permalinks. I already managed to rewrite the paths of custom post types and pages...
How do I rewrite the normal post post type permalink path in Wordpress programmatically?
You need to do it in two steps.
First enable rewrite with 'with_front' => true for the buildin post registration
add_filter(
'register_post_type_args',
function ($args, $post_type) {
if ($post_type !== 'post') {
return $args;
}
$args['rewrite'] = [
'slug' => 'posts',
'with_front' => true,
];
return $args;
},
10,
2
);
This way urls like http://foo.example/posts/a-title work but generated links in are now wrong.
Links can be fixed by forcing custom permalink structure for the buildin posts
add_filter(
'pre_post_link',
function ($permalink, $post) {
if ($post->post_type !== 'post') {
return $permalink;
}
return '/posts/%postname%/';
},
10,
2
);
See https://github.com/WordPress/WordPress/blob/d46f9b4fb8fdf64e02a4a995c0c2ce9f014b9cb7/wp-includes/link-template.php#L166
Posts have to use the default permalink structure, they do not have a special entry in the rewrite object in the same way that pages or custom post types do. If you want to change the default structure programmatically, you can add something like this to your hook.
$wp_rewrite->permalink_structure = '/post/%postname%';
I'm not super clear what you mean when you say
I can't use the backed for this because I already defined a default way of handling the permalinks. I already managed to rewrite the paths of custom post types and pages...
It sounds as though you're overriding the default behavior for permalinks everywhere except for posts so if you changed the default it would likely only affect posts anyway.
The permalink_structure property suggested by GentlemanMax did not work for me. But I found a method that does work, set_permalink_structure(). See code example below.
function custom_permalinks() {
global $wp_rewrite;
$wp_rewrite->page_structure = $wp_rewrite->root . '/page/%pagename%/'; // custom page permalinks
$wp_rewrite->set_permalink_structure( $wp_rewrite->root . '/post/%postname%/' ); // custom post permalinks
}
add_action( 'init', 'custom_permalinks' );

Wordpress custom post type with categories archive page

I'm struggling to get my custom post type to use my custom archive template, hopefully someone can see where I'm going wrong and help me get back on track please?
Here is the code I've used to create the custom post type:
add_action( 'init', 'news_post_type' );
function news_post_type() {
register_post_type( 'news',
array(
'labels' => array(
'name' => __( 'News' ),
'singular_name' => __( 'News' )
),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => true,
'public' => true,
'supports' => array('title','editor','excerpt','trackbacks','custom-fields','revisions','thumbnail','author','page-attributes',)
)
);
}
register_taxonomy( 'news_category', 'news',array('label' => __( 'Categories' ),'rewrite' => array( 'slug' => 'news/category' ),'hierarchical' => true, ) );
Which is great and the URL returns: www.mysite.com/news/category/%the_category% just like I want.
The problem is that I want each of the categories in this CPT to use my custom template, but when I create a file called archive-news.php it is ignored. However if I create a file called archive.php then it works but obviously this is applied to all other post archives which I don't want.
Am I not naming the template file correctly? Is there an error in how I've created the CPT?
If anyone can help that would be greatly appreciated.
Many thanks
i have check with your code
as its working properly on my side.
i think you checking on detail page which single-$posttype.php file.
Using taxonomy template worked in this instance, so naming my file taxonomy-news_category.php allowed me to create my own template for all categories in my custom post type.
I also found that having a page named the same as the custom post type was causing issues too. So in summary, rename the page or use the taxonomy template as above.

Wordpress Costum Post Type - ACF in URL

I have some problem that I need help in them -
I register costum post type - is slug is "project"
But I need to make 3 categories (with ACF) and make url changed due to it
category1 = http://www.example.com/mycaturl/nameoftitle
category2 = http://www.example.com/othercat/nameoftitle
category3 = http://www.example.com/customedit/nameoftitle
How can I make it without plugins...?
Thanks for helpers :)
Tested and verified:
I am writting in steps to make it clear:
Make sure when you registered your custom post type rewrite for more: register custom post type
'rewrite' => array('slug' => '/%project_categories%','with_front' => FALSE),
Then register your taxonomies project_categories
You need to register custom taxonomy project_categories for this custom (project) post type. register_taxonomy please read here for more
function project_taxonomy() {
register_taxonomy(
'project_categories', //The name of the taxonomy. Name should be in slug form (must not contain capital letters or spaces).
'project', //post type name
array(
'hierarchical' => true,
'label' => 'project store', //Display name
'query_var' => true,
'rewrite' => array(
'slug' => 'project', // This controls the base slug that will display before each term
'with_front' => false // Don't display the category base before
)
)
);
}
add_action( 'init', 'project_taxonomy');
Last make, your post type liks filters.
function filter_post_type_link($link, $post)
{
if ($post->post_type != 'project')
return $link;
if ($cats = get_the_terms($post->ID, 'project_categories'))
$link = str_replace('%project_categories%', array_pop($cats)->slug, $link);
return $link;
}
add_filter('post_type_link', 'filter_post_type_link', 10, 2);
Note: if you see post not found error. then change your permalink settings to default and save changes. And again set permalink to Post name and it will work.

wordpress custom post type category slug in url

I have custom post type (portfolio) with two different categories, then I have 2 pages showing posts from each of both categories.
The url when I call the page is something like mysite.com/pagename where pagename has the same name of the portfolio category.
My problem is when I go in the single portfolio the url changes into mysite.com/portfolio/portfolio-name
Is there any way to show the portfolio category in the url instead?
It should be like mysite.com/portfolio-category/portfolio-name
Thanks in advance.
Yes, any of those are possible and configurable under Settings > Permalinks. Have a look at the Permalinks page for other possibilities.
One thing to note though, it's suggested to add a number at the beginning of your permalinks to reduce the number of rewrite rules WordPress has to generate to resolve all of your URLs.
Register Your Taxonomy as Below.
Here "portfolio" is your "Custom Post Type" And
"portfolio-category" is your "portfolio category".
After adding this hook URL will display with "portfolio-category".As You want.
Try This will help you.
/* add action hook in function.php */
add_action( 'init', 'custom_function_toadd_taxonomyurl',0);
function custom_function_toadd_taxonomyurl() {
register_taxonomy( 'portfolio-category', array( 'portfolio' ), array(
'hierarchical' => true,
'label' => 'Portfolio Categories',
'singular_name' => 'Portfolio Category',
'show_ui' => true,
'query_var' => 'portfolio-category',
'rewrite' => array( 'slug' => 'portfolio-category')
));
}

Categories