Replace the custom post type permalink

admin2025-01-08  4

I am quite new to wordpress and I'd like to know what I am doing wrong here:

I've created a custom post type, let's say, custom_post_type_jobs.

And I have a page which is called jobs.

When I create a custom post type post, it has a permalink like this: .../custom_post_type_jobs/post-title.

Since I have a shortcode on the page "jobs", which renders some stuff, I would like to render the posts there and when clicking them I want a structure as follows: /jobs/post-title.

Am I missing here something?

When creating the custom post type, I gave it the args:

'rewrites' => array(
  'slug' => 'jobs'
 ),

I am quite new to wordpress and I'd like to know what I am doing wrong here:

I've created a custom post type, let's say, custom_post_type_jobs.

And I have a page which is called jobs.

When I create a custom post type post, it has a permalink like this: .../custom_post_type_jobs/post-title.

Since I have a shortcode on the page "jobs", which renders some stuff, I would like to render the posts there and when clicking them I want a structure as follows: /jobs/post-title.

Am I missing here something?

When creating the custom post type, I gave it the args:

'rewrites' => array(
  'slug' => 'jobs'
 ),
Share Improve this question asked Feb 5, 2023 at 15:14 LemonatedLemonated 111 bronze badge
Add a comment  | 

2 Answers 2

Reset to default 0

Wow, I just realized: the argument 'rewrites' was wrong. It's rewrite. Thank you guys anyway.

To replace the permalink structure for a custom post type in WordPress, you can use the following code snippet in your theme's functions.php file:

add_filter( 'post_type_link', 'replace_custom_post_type_permalink', 10, 4 );
function replace_custom_post_type_permalink( $post_link, $post, $leavename, $sample ) {
    if ( 'custom_post_type' === $post->post_type ) {
        $slug = 'custom-post-type';
        $post_link = str_replace( '%' . $post->post_type . '%', $slug, $post_link );
    }
    return $post_link;
}

In this example, the custom post type's permalink structure is defined using a custom placeholder, %custom_post_type%, in the permalink settings. The code replaces the placeholder with a custom string, custom-post-type, in the permalink for each post of this custom post type.

Note: Make sure to replace "custom_post_type" with the actual name of your custom post type and "custom-post-type" with the desired permalink structure.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736269664a1369.html

最新回复(0)