简体   繁体   English

WordPress自定义URL结构在访问不存在的页面时导致奇怪的结果

[英]WordPress Custom URL Structure Causes Weird Results When Visiting Non Existent Page

We are creating a website in WordPress and we need a unique ID in the permalink structure for a jobs post type. 我们正在用WordPress创建一个网站,并且在永久链接结构中需要职位职位类型的唯一ID。 The permalink structure looks like this: http://headway.xpandcreative.co.uk/jobs/5934-sales-executive-4/ 永久链接结构如下: http : //headway.xpandcreative.co.uk/jobs/5934-sales-executive-4/

With the ID at the front being the post ID. 前面的ID是职位ID。 The problem is, if we go to a URL like: http://headway.xpandcreative.co.uk/jobs/5934-sales-executive-4dediejidjiedjiejdiendjeijdejdjie/ it still displays the job even though it should clearly be a 404. 问题是,如果我们转到以下网址: http : //headway.xpandcreative.co.uk/jobs/5934-sales-executive-4dediejidjiedjiejdiendjeijdejdjie/ ,即使它显然应该是404,它仍然显示该作业。

The code I use to create this permalink structure is: 我用于创建此永久链接结构的代码是:

// Rewrite permalink structure
function jobs_rewrite() {
    global $wp_rewrite;
    $queryarg = 'post_type=jobs&p=';
    $wp_rewrite->add_rewrite_tag( '%cpt_id%', '([^/]+)', $queryarg );
    $wp_rewrite->add_permastruct( 'jobs', '/jobs/%cpt_id%/', false );
}
add_action( 'init', 'jobs_rewrite' );

function jobs_permalink( $post_link, $id = 0, $leavename ) {
    global $wp_rewrite;
//     $post = &get_post( $id );
    global $post;
    if ( is_wp_error( $post ) )
        return $post;
        $newlink = $wp_rewrite->get_extra_permastruct( 'jobs' );
        $newlink = str_replace( '%cpt_id%', $post->ID . '-' . $post->post_name, $newlink );
        $newlink = home_url( user_trailingslashit( $newlink ) );
    return $newlink;
}
add_filter('post_type_link', 'jobs_permalink', 1, 3);

Please check out the website here http://headway.xpandcreative.co.uk/jobs/ 请在这里查看网站http://headway.xpandcreative.co.uk/jobs/

Can anyone give me a suggestion on this? 有人可以给我一个建议吗?

Thanks 谢谢

Plugin Debug This may help you figuring out which rewrite rule causes the job page being loaded. 插件调试这可以帮助您确定哪个重写规则导致加载作业页面。 I had a similar problem and solved it by deleting and inserting rewrite rules using the rewrite_rules_array filter and additionally manipulating the posts query by means of the posts_where filter. 我遇到了类似的问题,并通过使用rewrite_rules_array过滤器删除和插入重写规则,以及通过posts_where过滤器来处理posts查询来解决该问题。 Hope it helps. 希望能帮助到你。

You have $queryarg = 'post_type=jobs&p=' which means in your example turns into $queryarg = 'post_type=jobs&p=5934-sales-executive-4' . 您有$queryarg = 'post_type=jobs&p=' ,这意味着在您的示例中变成了$queryarg = 'post_type=jobs&p=5934-sales-executive-4'

Since p is intended to be post ID which type is int , Wordpress sanitizes the input. 由于p旨在成为类型为int帖子ID,因此Wordpress会清除输入内容。

I haven't searched how it is done exactly but when you try yourself to do something like: 我还没有搜索确切的完成方式,但是当您尝试执行以下操作时:

$returnValue = intval('5934-sales-executive-4', 10);

you'll get in this case value 5934 , and I think that is happening here. 在这种情况下,您将获得值5934 ,我认为这正在发生。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM