I want to remove the custom post type name of the permalink.
I created a custom post type like this:
add_action( 'init', 'register_projects' );
function register_projects() {
register_post_type( 'proyectos_post',
array(
'labels' => array(
'name' => __( 'Proyectos' )
),
'public' => true,
'supports' => array( 'title', 'editor'),
'hierarchical' => true,
'menu_position' => 4
)
);
}
The problem is that when I enter to a post, the url gets: www.myweb.com/proyectos_post/mypost
I want it to be like this:
www.myweb.com/mypost
Is it possible to do it without external plugins?
Try this.
'rewrite' => array('slug' => 'custom-post-name', 'with_front' => FALSE)
Related
I am trying to create a filter in my custom post type- I have created a custom post type with the name of ourrecipes.. you can check the code below for custom post type-
function ourrecipes_posttype() {
register_post_type( 'ourrecipes',
// CPT Options
array(
'labels' => array(
'name' => __( 'Ourrecipes' ),
'singular_name' => __( 'Ourrecipes' )
),
'public' => true,
'taxonomies' => array('category','post_tag'),
'has_archive' => true,
'rewrite' => array('slug' => 'ourrecipes'),
'supports' => array( 'title','thumbnail' ,'editor','excerpt','custom-fields','page-attributes')
)
);
}
// Hooking up our function to theme setup
add_action( 'init', 'ourrecipes_posttype' );
For the reference please check the design as well-
https://previns.co.uk/our-recipes/
What is the best way to structure a parent/child custom post type combo, so that my slug url can look like this
https://www.mysite.co.uk/system/parent/child
The system part of the slug is vital, it is part of the multiple theme plugin for wordpress. Also, the parent/child parts of the url will not always be the same. Lets say I make a course called "How to ride a bike", and a series inside it called "Series 1", my slug needs to look like this
https://www.mysite.co.uk/system/how-to-ride-a-bike/series-1
at the moment my custom post types look like this
<?php
function create_post_type() {
//Courses
register_post_type( 'courses',
[
'labels' => array(
'name' => __( 'Courses' ),
'singular_name' => __( 'Course' )
),
'description' => 'All courses',
'public' => true,
'rewrite' => array(
'slug' => 'system',
),
'menu_icon' => 'dashicons-welcome-learn-more',
'supports' => ['title', 'custom_fields', 'page-attributes']
]
);
//Series
register_post_type( 'series',
[
'labels' => array(
'name' => __( 'Series' ),
'singular_name' => __( 'Series' )
),
'show_ui' => true,
'show_in_menu' => 'edit.php?post_type=courses',
'description' => 'Course series',
'public' => true,
'rewrite' => array('slug' => '/'),
'hierarchical' => true,
'with_front' => false,
'capability_type' => 'post',
'has_archive' => false,
'supports' => ['title', 'custom_fields', 'page-attributes']
]
);
}
add_action( 'init', 'create_post_type' );
?>
I'm not 100% sure on the best way to actually link the child element to the parent? At the moment I have an advanced custom field on the child post type (series) where you select the parent (course) its linked to.
What is the best way to structure my custom post types, so that I can link a child to a parent and have the slug specified above?
You can use parent/child structures of the same custom post type using the page-attributes value in the supports array.
You do need to flush the rewrite rules of Wordpress before adding the sub-item. Just visit the permalinks page and hit save a couple of times.
Here is my example with the custom post type subcase:
register_post_type( 'subcase',
array(
'hierarchical' => true, // needs to be true
'labels' => array(
'name' => __( 'Sub Cases' ),
'singular_name' => __( 'Sub Case' )
),
'taxonomies' => array( 'category', 'post_tag' ),
'public' => true,
'has_archive' => false,
'rewrite' => array(
'slug' => 'cases/subcases',
'with_front' => true
),
'supports' => array(
'page-attributes', // creates parent select box
'title',
'editor',
'excerpt',
'thumbnail'
)
)
);
I have raised this issue on the WPML forums, but hoping someone here will be able to assist.
I am trying to translate the slug for a custom post type
The English URL is http://brigade-electronics.com/nl/products/backeye360/
The translated URL should be http://brigade-electronics.com/nl/producten/backeye360/
Instead I get a 404 error when navigating to the URL after enabling the translate slug option
Steps to duplicate the issue:
Click On WPML -> Translation options
Enable the Translate custom posts slugs (via WPML String Translation).
Under the Custom posts settings (on the same page) Click the translate checkbox
Added the translated slug for each language
Hit save
Navigate to the front end and see a 404 error on the products section only.
I have run all options in the troubleshooting page, to clear up the database.
This only seems to apply to certain pages within the product section. The weirdest part of this is the Canadian section of the site, as the term 'product' is in English therefore the URLs remain the same with or without the translated slugs in place, however, I still get the 404 error on these pages.
It is also worth noting that all other custom post types work without issue.
The custom post types have been registered in the standard way
function register_products_post_type() {
$labels = array(
'name' => __( 'Products', '' ),
'singular_name' => __( 'Product', '' )
);
$args = array(
'label' => __( 'Products', '' ),
'labels' => $labels,
'description' => '',
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_rest' => false,
'rest_base' => '',
'has_archive' => false,
'show_in_menu' => true,
'exclude_from_search' => false,
'capability_type' => 'post',
'map_meta_cap' => true,
'hierarchical' => true,
'rewrite' => array( 'slug' => 'products', 'with_front' => false ),
'query_var' => true,
'menu_position' => 6,
'menu_icon' => 'dashicons-cart',
'supports' => array( 'title', 'thumbnail', 'page-attributes' )
);
register_post_type( 'products', $args );
}
add_action( 'init', 'register_products_post_type' );
As per the below answer, the above code has been updated to
add_action( 'init', 'create_post_type');
function create_post_type() {
$labels = array(
'name' => _x( 'Products', 'general name of the post type' ),
'singular_name' => _x( 'Products', 'name for one object of this post type' ),
);
$args = array(
'labels' => $labels, // An array that defines the different labels assigned to the custom post type
'public' => true, // To show the custom post type on the WordPress dashboard
'supports' => array( 'title', 'thumbnail', 'page-attributes' ),
'has_archive' => true, //Enables the custom post type archive at
'hierarchical' => true, //Enables the custom post type to have a hierarchy
'rewrite' => array( 'slug' => _x('products', 'URL slug')),
);
register_post_type( 'products', $args );
}
The new translation for the slug appears in the 'String Translation' section, when updating these strings, I get the same 404 error. If I leave these as English the products section works with no problem.
Thanks
Try this
add_action( 'init', 'create_post_type');
function create_post_type() {
$labels = array(
'name' => _x( 'Products', 'general name of the post type' ),
'singular_name' => _x( 'Products', 'name for one object of this post type' ),
);
$args = array(
'labels' => $labels, // An array that defines the different labels assigned to the custom post type
'public' => true, // To show the custom post type on the WordPress dashboard
'supports' => array( 'title', 'thumbnail', 'page-attributes' ),
'has_archive' => true, //Enables the custom post type archive at
'hierarchical' => true, //Enables the custom post type to have a hierarchy
'rewrite' => array( _x('slug' => 'products'), 'with_front' => false ),
);
register_post_type( 'products', $args );
}
Have you flushed the rewrite rules?
Go to Settings > Permalinks and refresh.
Note: If registering a post type inside of a plugin, call
flush_rewrite_rules() in your activation and deactivation hook (see
Flushing Rewrite on Activation below). If flush_rewrite_rules() is not
used, then you will have to manually go to Settings > Permalinks and
refresh your permalink structure before your custom post type will
show the correct structure.
source: https://codex.wordpress.org/Function_Reference/register_post_type
I have created a custom post type in WordPress, but when I add a new post to this post-type it doesn't show in the list of all posts in the post-type. Instead it displays all posts from the default posts-page.
Function:
function create_post_type() {
register_post_type( 'winactie',
array(
'labels' => array (
'name' => __( 'Winactie' ),
'singular_name' => __( 'Actie' )),
'public' => true,
'has_archive' => true,
'menu_position' => 5,
'rewrite' => array('slug' => 'winactie'),
)
);
}
add_action( 'init', 'create_post_type' );
Am I missing something?
Please use this link to create new custom post type in admin side it will work perfect result for you .
http://www.wpbeginner.com/wp-tutorials/how-to-create-custom-post-types-in-wordpress/
Also please change your post type name to demo post type name.
function create_post_type() {
register_post_type( 'winactie',
array(
'labels' => array (
'name' => __( 'Winactie' ),
'singular_name' => __( 'Actie' )),
'public' => true,
'has_archive' => true,
'menu_position' => 5,
'rewrite' => array('slug' => 'winactie'),
)
);
}
add_action( 'init', 'create_post_type' );
Your code is right and i had tried with your code and there is no error. All post are listing in winactie custom post.
To put a long story short, I'm trying to get the custom post type category to show where it says 'project-item' as you can see HERE
I presume it has something to do with the way I've registered the custom post type such as in this code:
<?php
add_action( 'init', 'register_posts' );
function register_posts() {
register_post_type( 'team_post',
array(
'labels' => array(
'name' => __( "Team" ,"um_lang"),
'singular_name' => __( "Team" ,"um_lang")
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => "project_item", 'with_front' => TRUE),
'supports' => array('title','editor','thumbnail','page-attributes')
)
);
register_post_type( 'project_post',
array(
'labels' => array(
'name' => __( "Projects","um_lang"),
'singular_name' => __( "Project" ,"um_lang")
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => "project_item", 'with_front' => TRUE),
'supports' => array('title','editor','thumbnail','page-attributes')
)
);
register_taxonomy('project_category',array (
0 => 'project_post',
),array( 'hierarchical' => true, 'label' => __('Projects Category',"um_lang"),'show_ui' => true,'query_var' => true,'singular_label' => __('Projects Category',"um_lang")) );
}
?>
This tells wordpress to insert the text project_item into the slug, which is what is happening:
'rewrite' => array('slug' => "project_item", 'with_front' => TRUE)
And this says to insert the value of project_item:
'rewrite' => array('slug' => "%project_item%", 'with_front' => TRUE)
Try this:
global $wp_rewrite;
$movies_structure = '/movies/%year%/%monthnum%/%day%/%movies%';
$wp_rewrite->add_rewrite_tag("%movies%", '([^/]+)', "movies=");
$wp_rewrite->add_permastruct('movies', $movies_structure, false);
return link:
domain.com/movies/movie_name
I figured it out. I installed this plugin: https://wordpress.org/plugins/custom-post-type-permalinks/
Then instead of using %categories% (Which didn't work) I used the specific custom post type category name (which was %project_category%)