最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp query - How can I get custom post types to recognize pretty permalink variables?

programmeradmin0浏览0评论

I'm using PODS to create a custom post type called "automobile" on my site. This results in a permalink structure that looks like this:

/automobile/automobile-page

I want to be able to send variables to this page by adding 1 OR 2 additional elements

/automobile/{brand is the default page}/%model%/%edition%/

For example

/automobile/porsche/911/carrera

So the root automobile page would be the brand (porsche), which calls a PODS template. The PODS template executes code that allows the user to drill down. It's important for SEO reasons that the structure looks like this.

I've tried dozens of solutions, but as soon as I tack on anything beyond the page, I either get a 404 error or get redirected to the home page.

From everything I've researched, this should work added to functions.php:

add_action('init','initialize_automobile_vars');

function initialize_automobile_vars()
{
    add_rewrite_tag('%model%','([^/]+)');
    add_rewrite_tag('%edition%','([^/]+)');
    add_rewrite_rule('^automobile/([^/]+)/([^/]+)/?$','index.php?automobile=$matches[1]&model=$matches[2]','top');
    add_rewrite_rule('^automobile/([^/]+)/([^/]+)/([^/]+)/?$','index.php?automobile=$matches[1]&model=$matches[2]&edition=$matches[3]','top');
}

Result:

If I enter this, I am directed to the Porsche page:

/automobile/porsche

But if I enter either of these, I get a 404 error:

/automobile/porsche/911
/automobile/porsche/911/carrera

I've tried almost everything, but haven't cracked the nut on this one. Any help would be appreciated!

I'm using PODS to create a custom post type called "automobile" on my site. This results in a permalink structure that looks like this:

/automobile/automobile-page

I want to be able to send variables to this page by adding 1 OR 2 additional elements

/automobile/{brand is the default page}/%model%/%edition%/

For example

/automobile/porsche/911/carrera

So the root automobile page would be the brand (porsche), which calls a PODS template. The PODS template executes code that allows the user to drill down. It's important for SEO reasons that the structure looks like this.

I've tried dozens of solutions, but as soon as I tack on anything beyond the page, I either get a 404 error or get redirected to the home page.

From everything I've researched, this should work added to functions.php:

add_action('init','initialize_automobile_vars');

function initialize_automobile_vars()
{
    add_rewrite_tag('%model%','([^/]+)');
    add_rewrite_tag('%edition%','([^/]+)');
    add_rewrite_rule('^automobile/([^/]+)/([^/]+)/?$','index.php?automobile=$matches[1]&model=$matches[2]','top');
    add_rewrite_rule('^automobile/([^/]+)/([^/]+)/([^/]+)/?$','index.php?automobile=$matches[1]&model=$matches[2]&edition=$matches[3]','top');
}

Result:

If I enter this, I am directed to the Porsche page:

/automobile/porsche

But if I enter either of these, I get a 404 error:

/automobile/porsche/911
/automobile/porsche/911/carrera

I've tried almost everything, but haven't cracked the nut on this one. Any help would be appreciated!

Share Improve this question asked Aug 24, 2019 at 0:20 BillBill 1
Add a comment  | 

1 Answer 1

Reset to default 0

Building Custom URLs in Wordpress discusses how to use Pretty Permalinks to display posts using additional parameters.

Jump to the section on Custom Query Vars. That's where your situation begins to be addressed directly.

In particular, you may want to use the example using 'pre_get_posts' to retrieve parameters and adjust the WP_Query using a meta_query, so it doesn't result in a 404 Error as a result of your additional parameters:

add_action( 'pre_get_posts', 'your_function_name', 1 );

NOTE: You may need to modify your customized url to read more like the following, in order to fit the example provided by this tutorial:

https://your-domain/automobile/porsche/model/911/name/carrera

In this example, "model" and "name" are keys, while "911" and "carrera" are their respective values. These can then be used for meta_query searching.

发布评论

评论列表(0)

  1. 暂无评论