Remove CPT's taxonomy base slug from wordpress URL - php

I'm having "Tour" as CPT. In this I'm having Main page, Category page and Detail page.
Main page - https://www.exapmle.com/tour/
Category Page - https://www.exapmle.com/tour/category-page/
Detail Page - https://www.exapmle.com/tour/detail-page/
I need these as Below
Main page - https://www.exapmle.com/tour/
Category Page - https://www.exapmle.com/category-page/
Detail Page - https://www.exapmle.com/detail-page/

When registering your CPT you provided some arguments... One of the arguments is 'rewrite'. You can provide an array of options,like 'slug', 'with_front', 'pages', etc.
Maybe you can try to set the slug to be empty? Or to be '/'?
$args = [
...
'rewrite' => ['slug' => '', 'with_front' => false],
...
];
register_post_type('your-cpt-slug', $args);
I hope it works!

Related

Wordpress - Add case studies then link 3 onto homepage

I am new to wordpress so sorry for the basic question. How do I have a case studies page, but link 3 case studies to display a shortened version onto my homepage.
Any ideas?
Thanks!
Scott
I would start by creating a Custom Post Type called case studies. Add the following to your theme's (or child theme) function.php.
// Create Custom Post Type
function my_custom_posttype(){
register_post_type('case studies',
// Options
array(
'labels' => array(
'name' => __('Case Studies'),
'singular_name' => __('Case Study')
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'case-studies'),
)
);
}
add_action( 'init', 'my_custom_posttype' );
This will add a new link in the admin to add new case studies, just like posts and pages. This is a very basic example, check the link above for more information on post types.
Adding them into the homepage will depend on what theme you are using.

Custom Post Type URL rewrite with Post Type Template

I am building a Wordpress site for a video production company. They produce videos for adults as well as for kids and therefore I split the posts in the backend to keep a structure. So the setup is the following:
Two Custom Post Types named productions_big and productions_small
A page named Productions, placed in the main navigation with links to
For Big and For Small pages
One page listing the posts of productions_big, one listing posts of
productions_small. The page names are for-big and for-small and both are children of the Productions page
The URLs are perfectly fine for the pages as I made a parent/child relation of the relevant pages. example.com/productions leads to the right page as well as example.com/productions/for-big and example.com/productions/for-small do.
For reasons that are irrelevant to this question the templates for
the CPTs differ, but in some occasions template-big is used for productions_small and vice versa. There is no actual logic behind this and therefore the decision on which template to use is done in the backend via the Post Type Template feature introduced with Wordpress 4.7
What I want to achieve is a logical URL structure like example.com/productions/for-big/name-of-adult-production. Right now I have URLs like example.com/productions_big/name-of-adult-production as productions_big is the name of the CPT.
I am familiar with the rewrite argument you should pass when registering custom post types. I have done this in all the ways suggested at stackoverflow but it is just not working. Here is an excerpt of the code I use to register my CPTs:
add_action('init', 'all_custom_post_types');
function all_custom_post_types() {
$types = array(
array('the_type' => 'productions_big',
...
array('the_type' => 'productions_small',
...
);
foreach ($types as $type) {
$the_type = $type['the_type'];
...
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'rewrite' => true,
'capabilities' => array(
...),
'rewrite' => array('has_archive' => false, 'slug' => 'bla', 'with_front' => false),
'has_archive' => false,
'map_meta_cap' => true,
'hierarchical' => false,
'menu_position' => 5,
'supports' => array('title','editor','thumbnail', 'taxonomies'),
'taxonomies' => array( 'category' )
);
register_post_type($the_type, $args);
}
}
As you see I tried 'rewrite' => array('slug' => 'bla') because I wanted to know if it does not work because of a conflict of page names when I write 'rewrite' => array('slug' => 'productions/for-big')
But whatever value I give the slug, it always redirects me to index.php.
I thought the reason why it is not working is caused by the way I am assigning post templates (remember: as a post attribute in the backend, not a dedicated single-xxx.php file) but I couldn't find a solution anywhere. All solution-seekers posting their questions were assigning templates the good old fashioned single-name_of_cpt.php way (which is not possible for me).
Sidenote: The ultimate goal would be to make the slug "dynamic". Meaning that it changes in case the page names are altered in the backend. I might try this approach but beforehand I'll have to get rid of the basic problems described here.
Any idea is much appreciated. And thank you for reading this far.
Oh boy, seems you have to post your questions in order to find an answer yourself. After trying to setup another CPT everything worked like a charm.
That made me investigate the registration of the previous CPTs. As you can see above I tried to register my CPTs inside of one function
function all_custom_post_types() {
$types = array(
array('the_type' => 'productions_big', ...
array('the_type' => 'productions_small', ...
);
foreach ($types as $type) {
$args = array(...
'rewrite' => array('slug' => 'bla'),...
);
register_post_type($the_type, $args);
}
}
So ultimately there was no conflict between the slugs of my pages and custom post types but a conflict of giving one and the same slug to two different post types.

Using wp_dropdown_page() to display a dropdown of a custom post type

I'm trying to create a dropdown list with the list of a certain post type.
I saw that I could use the wp_dropdown_pages function to do that.
I added this code inside my td but when the page displays the code is not interpreted. The are only few examples to look at.
<td><?php wp_dropdown_pages(array('id'=>'marque0','post_type' => 'Brands')); ?></td>
additionally I need to filter a second dropdown list which contains only child post of the parent selected in the first dropdown. To do that I need the id of the parent post. How can I get this from the element selected in the first dropdown list ?
Thanks a lot.
Update :
I found a way to go around the usage of PHP inside the wordpress page, I created shortcodes to call the wp_dropdown_pages, then I use the shortcodes inside my page.
Surprisingly enough (!), just specify in your custom post type's settings 'hierarchical' => true, not the hierarchical argument in wp_dropdown_pages() function.
register_post_type( 'brands', array(
'labels' => array(
..........
),
.........
'hierarchical' => true,
.........
));
Then it should work:
wp_dropdown_pages(array(
'id' => 'marque0',
..........
'post_type' => 'brands'
));

Show product on main page by id opencart

I need to show one certain product on front page. I didn't find a module for this. What do I need to do? I need to show product with countdown timer but I can do this, I only do not know how to get product from database by ID or SKU. I guess the code should be like this:
echo $product_option_data[] = array(
'product_option_id' => $product_option['product_option_id'],
'option_id' => $product_option['option_id'],
'name' => $product_option['name'],
'type' => $product_option['type'],
'option_value' => $product_option['option_value'],
'required' => $product_option['required']
);
In OpenCart controller you can call
$this->load->model('catalog/product'); // only if not yet 'loaded' within the controller
$product = $this->model_catalog_product->getProduct($productId);
print_r($product);
which will return you the product by it's ID. Do with it whatever you need afterwards.

cant show details of a post of custom-post-type -wordpress

I have custom post type like this
add_action( 'init', 'create_post_type_feedback' );
function create_post_type_feedback() {
register_post_type( 'testimonial',
array(
'labels' => array(
'name' => __( 'Feedbacks' ),
'singular_name' => __( 'Feedback' )
),
'supports' => array('title','editor','thumbnail','custom-fields'),
'public' => true,
'has_archive' => true,
)
);
}
In a page i am showing the list of the testimonials and using the function get_permalink() to get hyperlink. To show details of a testimonial post i have "single-testimonial.php" and but whenever i click to view details of the testimonial it redirects me to a link like '..../testimonial/postname'. Though there single-testimonial. php , it shows me the 404.php's content.
How can i show details of a post of custom-post-type ?
to get the single post pages to work you will have to add a few things to your register custom post type function... ie:
'public' => true, // yes you want it to be public?
'show_ui' => true, // you want it shown in the admin area
'show_in_menu' => true, // show it in menus etc..
'rewrite' => array('slug' => 'testimonial'), // this is the permalink structure
'show_in_nav_menus' => true, // show in navigation menus
'publicly_queryable' => true, // include this in searches
'query_var' => true, // do you want to pass values?
'capability_type' => 'post', // is it like a post or page?
'menu_position' => 25, // the position on the admin menu!
adding the slug should do the trick,
NOTICE: once slug has been set, go to settings->permalinks set back to default, then set back to postname, to flush the re-write rules, and so wordpress knows its there and working! that should do the trick, you dont need to add any template code to any files,
simply add the single-testimonial.php and it should work or
page-testimonial.php depending how you want it setup..
Marty
In your single-testimonial.php, make sure you have this line at the top:
// Template Name: Single Testimonial template
This is so Wordpress can know that this is a template file and allow you to select it from the 'page attributes' section when you are creating a testimonial post.
Secondly, use the get_post_permalink() function instead of get_permalink(). Here's why

Categories