I have this URL for my Resort page page-resorts.php:
http://localhost/testwordpress/resorts/
After clicking the link to a post under Resort custom page I will have this URL for my custom page template (CPT) single-resort.php:
http://localhost/testwordpress/resort/kurumba-maldives/
As you can see the resorts was changed to resort because I can't use resorts slug for the slug post.
How can I achive this kind of URL:
http://localhost/testwordpress/resorts/kurumba-maldives/
where the resorts word is used and not resort word only?
I've heard about custom slug and search for it, but I still can't understand it.
You can create your custom post type this way.
function create_posttype() {
register_post_type( 'resort',
array(
'labels' => array(
'name' => __( 'Resorts' ),
'singular_name' => __( 'Resort' )
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'resorts'),
)
);
}
add_action( 'init', 'create_posttype' );
'rewrite' => array('slug' => 'resorts'), this used to add the slur on URL. this way you can acheive your URL.
Or you can override your current slug in functions.php
add_filter( 'register_post_type_args', 'wpse247328_register_post_type_args', 10, 2 );
function wpse247328_register_post_type_args( $args, $post_type ) {
if ( 'resort' === $post_type ) {
$args['rewrite']['slug'] = 'resorts';
}
return $args;
}
For more, please visit.
register post type
Change Custom Post Type slug
Related
I use a plugin that has a custom post type and when I publish a post it is displayed on a single page but when the post is automatically published using an author's robot plugin, unfortunately, the single page is not found and shows error 404!!
//------------------------
//function init
//-------------------------
static function init(){
add_action( 'init', array( __CLASS__, 'register_post_types' ) );
}
//------------------------
//register post type
//-------------------------
static function register_post_types(){
$labels = array(
//custom post type
);
$args = array(
'labels' => $labels,
'public' => true,
'show_ui' => true,
'hierarchical' => false,
'rewrite' => array('slug' => 'jobtest'),
'query_var' => true,
'has_archive' => true,
'supports' => array('title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )
);
register_post_type( 'iwj_job' , $args );
}
I have the main plugin file with this function:
public function init_hooks() {
register_activation_hook( __FILE__, array( 'IWJ_active', 'activate' ) );
}
Then within my class, I have this:
static function activate() {
IWJ_Post_Types::register_post_types();
flush_rewrite_rules();
}
I don't know why when I use the Author Robot Plugin for publishing a post, custom post type single page not found?? but it works when I manually update it again!
For fixing custom post not found please use below code in your functions.php:
flush_rewrite_rules( false );
And,
Simply go to settings>permalinks and choose a permalink structure if you haven’t done so already. Once done click “save changes” and make any changes to .htaccess if prompted to do so. Now view you custom post again and all should be right with the world.
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' );
I wanna be able to post my WooCommerce products into my "posts" categories. Based based off this cod below, it's possible. Here's the code I'm using in my functions.php. The categories are clickable when I make a new product in Woo however, it's not posting to the category itself. Appreciate any insight on this matter.
Add category selection to custom post type
function reg_cat() {
register_taxonomy_for_object_type('category','CUSTOM_POST_TYPE');
}
add_action('init', 'reg_cat');
For register custom taxonomy you can try below code, for more https://codex.wordpress.org/Function_Reference/register_taxonomy
<?php
add_action( 'init', 'create_book_tax' );
function create_book_tax() {
register_taxonomy(
'genre',
'book',
array(
'label' => __( 'Genre' ),
'rewrite' => array( 'slug' => 'genre' ),
'hierarchical' => true,
)
);
}
?>
Please try This Code for register new texonomy
function custom_taxonomy() {
register_taxonomy(
'custom_categories', //The name of the taxonomy. Name should be in slug form (must not contain capital letters or spaces).
'post_type', //post type name
array(
'hierarchical' => true,
'label' => 'Themes store', //Display name
'query_var' => true,
'rewrite' => array(
'slug' => 'themes', // This controls the base slug that will display before each term
'with_front' => false // Don't display the category base before
)
)
);
}
add_action( 'init', 'custom_taxonomy');
Note: Please add this code in theme function.php or your plugin
=======================================================================
function reg_cat() {
register_taxonomy_for_object_type('category','CUSTOM_POST_TYPE');
}
add_action('init', 'reg_cat');
This code working post category display in custom post type texonomy section Please refer
I am trying to add terms using code but it's not added into wordpress. I am using below code to add
$term = wp_insert_term('Jogger', 'product_style',0);
Every time it return Invalid taxonomy. I have check my db and I don't found any entry for this. Even I have check with term_exists('Jogger', 'pa_style',0); and it also return 0. Can some have any idea about it.
I am able to add other terms like
$term = wp_insert_term('SPORTS', 'product_cat',0);
Use this code in functions.php
add_action( 'init', 'create_new_taxonomy' );
function create_new_taxonomy() {
register_taxonomy(
'product_style',
'products',
array(
'label' => __( 'Product Style' ),
'rewrite' => array( 'slug' => 'product_style' ),
'hierarchical' => true,
)
);
}
You can't insert term here because your taxonomy is not registered. So use the above code register it first. Then insert term.
I am working on a post_type and taxonomy. I create a page template "foo_template.php" for its main page, in main page I write a query that shows all taxonomies and related (5) Five posts. When i click on any taxonomy the new page open and it shows all post titles of clicked taxonomy and its slug is "foo_taxonomy", i also create for it a page "taxonomy-foo_post.php" and when i click any title it goes on single page which i created for it "single-foo_post.php" and its slug is "foo_post".
Now the main issue is i want that post type and taxonomy slug is same when i do this my page layout is disturbed and it goes to archive.php
My friend give me some code i write it, where post_type and taxonomy slug is same, when i click on the taxonomy the page open with new same slug but when i click on the post title for single page it shows that "Page not found"
What is the issue i don't get it.
Here is my code:
Post type and Taxonomy code:
add_action('init', 'foo_articles');
function foo_articles() {
register_post_type('foo_knowledgebase', array(
'labels' => array(
'name' => 'Articles',
'singular_name' => 'Article'
),
'public' => true,
'rewrite' => array(
'slug' => 'My_slug')
));
}
add_action( 'init', 'foo_taxonomies', 0 );
function foo_taxonomies() {
register_taxonomy('foo_taxonomy', array('My_slug'), array(
'labels' => array(
'name' => 'Articles Category'
),
'show_ui' => true,
'show_admin_column' => true,
'show_tagcloud' => FALSE,
'hierarchical' => true,
'rewrite' => array('slug' => 'My_slug', 'with_front' => TRUE)
));
}
For same slug code:
$rules = array();
$taxonomies = get_taxonomies(array('_builtin' => false), 'objects');
$post_types = get_post_types(array('public' => true, '_builtin' => false), 'names');
foreach ($post_types as $post_type) {
$post_type_data = get_post_type_object( $post_type );
$post_type_slug = $post_type_data->rewrite['slug'];
foreach ($taxonomies as $taxonomy) {
if ($taxonomy->object_type[0] == $post_type_slug) {
$categories = get_categories(array('type' => $post_type_slug, 'taxonomy' => $taxonomy->name, 'hide_empty' => 0));
/* #var $category type */
foreach ($categories as $category) {
$rules[$post_type_slug . '/' . $category->slug . '/?$'] = 'index.php?' . $category->taxonomy . '=' . $category->slug;
}
}
}
}
$wp_rewrite->rules = $rules + $wp_rewrite->rules;
}
add_filter( 'generate_rewrite_rules', 'taxonomy_slug_rewrite' );
My friend said that this code work for him but i dont now what happen here i copied same code.
This condition is also set true
if ($taxonomy->object_type[0] == $post_type_slug)
but i don't know why my slug not work.
Please help me
For what you want to achieve you will need to define the Custom Taxonomy BEFORE the Custom Post Type.
This way will work but in the unlikely event that your Taxonomy and your Post will have the same slug won't be able to view the entry.
e.g. if you will have a taxonomy term called "press" and a CPT with the title "press".