【问题标题】:How to rewrite URL of custom post type without post type in URL如何在 URL 中重写没有帖子类型的自定义帖子类型的 URL
【发布时间】:2018-12-27 16:19:11
【问题描述】:

我在一个旅游网站工作。他们有一个使用自定义插件构建的自定义帖子类型。 该自定义帖子类型具有类似“/destination/”.$post->post_name 之类的基本信息。 他们想让我删除那个基本的蛞蝓,这样它就只能是 $post->post_name

我尝试了下面列出的互联网代码。

它适用于该目的地的单层。 但是当我在美国有一个像纽约这样的父母目的地时。 所以它不起作用。 这是一个例子:

function update_destination_page_link_filter($post_link, $post, $leavename = null, $sample = null ) {
    if ( $post->post_type === 'destination' ) {
        $post_link = str_replace('/destination/', '/', $post_link);
        if($post->post_parent !== 0){
            $parent_slug = get_parent_link($post->post_parent, '');
            $post_link = '/'.$parent_slug.$post->post_name;
        }
        $post_link = update_url_base( $post, $post_link );
    }
    return $post_link;
}

add_filter( 'post_type_link', 'update_destination_page_link_filter', 1, 3 );


function allow_destination_direct_by_name( $query ) {
     // Only noop the main query
    if ( ! $query->is_main_query() )
        return;

    // Only noop our very specific rewrite rule match
    if ( 2 != count( $query->query )
        || ! isset( $query->query['page'] ) )
        return;

    // 'name' will be set if post permalinks are just post_name, otherwise the page rule will match
    if ( ! empty( $query->query['name'] ) )
        $query->set( 'post_type', array( 'post', 'destination', 'page' ) );
}
add_action( 'pre_get_posts', 'allow_destination_direct_by_name', 1);

单层 http://siteurl/united-state-america 效果很好 http://siteurl/united-state-america/new-york 不工作。它应该打开纽约位置页面,但显示 404 它也可能在位置上更规范 赞http://siteurl/united-state-america/new-york/brooklyn

【问题讨论】:

    标签: wordpress .htaccess mod-rewrite url-rewriting custom-post-type


    【解决方案1】:

    在这方面,以下代码可能会对您有所帮助。

    add_filter( 'post_type_link', 'my_post_type_link', 10, 3);
    function my_post_type_link($permalink, $post, $leavename) {
    
        if  ($post->post_type == <your-post-type>) 
        {
            $p_name=$post->post_name;
            $parent_slug = get_parent_link($post->post_parent, '');
            if (isset($parent_slug) && !empty($parent_slug)) 
            {
                $permalink = home_url( "" . $parent_slug . "/" . $p_name . "/");
            }
        }
    
        return $permalink;
    
    }
    
    add_filter( 'rewrite_rules_array', 'my_rewrite_rules_array');
    function my_rewrite_rules_array($rules) {
    
        $rules = array('([^/]*)/([^/]*)/?$' => 'index.php?post_type=<your-post-type>&name=$matches[2]&meta=$matches[1]') + $rules;
    
        return $rules;
    }
    

    【讨论】:

    • 嗨,它有效,但只有一个问题。它也可能在位置上有更多规范,例如 siteurl/united-state-america/new-york/brooklyn 所以对于第三个位置......它不起作用。
    • 主要是,我只需要最后一场比赛。因此,如果我到达那里 $matches[(last_match)] 无论最后一个是 2 还是 3 还是 4 。这将解决我的问题。
    • 另外,它不适用于多个 CPT。所以假设我有目的地、信息和地点。
    • 您必须为函数“my_post_type_link”添加更多规范,还需要为其他帖子类型的规则编写另一个函数。
    猜你喜欢
    • 2017-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-15
    • 1970-01-01
    • 1970-01-01
    • 2013-09-28
    • 2014-02-21
    相关资源
    最近更新 更多