0

I checked all subjects in forum about this but i didn't found way to resolve my problem. sorry for my english, i'm french.. :)

So.. i got a custom post listing ( with template listing pro ) there : http://easycowork.fr/bureau/easy-office-lille/

I created a custom post named service and i display all service associate on listing on the listing single page. When i click on a service, i display his data on the same page with a GET parameter ( the service id ) like this : http://easycowork.fr/bureau/easy-office-lille/?show_service=196

But i need the name of service instead of show_service=196 after the name of listing "easy-office-lille"

How can i do that ? I'm really lost in url rewriting

I use Custom post type UI plugin to create custom post, I added rewrite option to true, and Custom Rewrite Slug option to service and registered permalink but that didn't work.

Thanks for help !

4
  • how do you store which listing should be in the URL of each service?
    – Milo
    Jul 10, 2017 at 16:19
  • i'm not sure to understand your answer ( im french ), but.. my service is displaying on listing single page. i have associate each service with listing ( i got a front form to add service, i got a select to choose listing i want for the service ) so easy on single listing page to know which service is from the listing. when you are on listing page, you click on service's image and the page reload with $_GET parameter and i hide some listing data to display service's data. Sorry if is not your question :/
    – 1way
    Jul 10, 2017 at 21:34
  • Where is the association between listing and service stored? Is that post meta data? To generate a permalink for a service, you have to be able to get the name of the listing that it is associated to.
    – Milo
    Jul 11, 2017 at 0:03
  • the association is in service's custom field so post meta data yes, i register the name of listing. So i know the name of the listing because i'm on the listing page, and if i have to create new page for my service, i can know which listing is associate to, but i really don't how to do this
    – 1way
    Jul 11, 2017 at 10:53

1 Answer 1

0

To achieve result for custom URL rewriting for your custom post type service using the Custom Post Type UI plugin,

you can follow these steps

Register the custom post type with the desired rewrite slug

Register Custom Post Type with Rewrite Slug:

In the Custom Post Type UI plugin, go to CPT UI > Add/Edit Post Types and edit your "service" post type. Make sure you have set the Rewrite option to true and the Custom Rewrite Slug option to the desired slug, e.g( service )

Add a rewrite rule to handle the custom URL structure

Add a custom rewrite rule to handle the custom URL structure. You can do this by adding code to your themes functions.php file or a custom plugin

function custom_rewrite_rule() {
    add_rewrite_rule(
        '^bureau/([^/]+)/([^/]+)/?$',
        'index.php?pagename=$matches[1]&show_service=$matches[2]',
        'top'
    );
}
add_action('init', 'custom_rewrite_rule');

This rule will rewrite URLs like http://easycowork.fr/bureau/easy-office-lille/123 to http://easycowork.fr/index.php?pagename=bureau/easy-office-lille&show_service=123

Modify your template to handle the custom URL structure

In your template file single-service.php, you can retrieve the service ID from the show_service parameter and display the corresponding content.

$service_id = isset($_GET['show_service']) ? intval($_GET['show_service']) : 0;
if ($service_id) {
    // Query the service post based on the ID and display its content
    // Example: $service_post = get_post($service_id);
} else {
    // Display default content for the listing page
}

Remember to flush the rewrite rules after adding or modifying them. You can do this by visiting the "Settings" > "Permalinks" page in your WordPress admin and clicking the Save Changes button

After following these steps, you should be able to access your service pages with URLs like http://easycowork.fr/bureau/easy-office-lille/123, where 123 is the ID of the service

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.