在404中从自定义开机自检类型中删除插件

时间:2018-01-21 作者:Paranoia

我正在开发一个创建列表的插件。创建列表后,我想从url中删除slug

岗位类型:

$rewrite = [
        \'slug\'       => \'single-link\',
        \'with_front\' => false,
        \'pages\'      => false,
        \'feeds\'      => false,
    ];
    $args    = [
        \'label\'               => esc_html__( \'Single Link\', \'single-link\' ),
        \'labels\'              => $labels,
        \'supports\'            => [ \'title\' ],
        \'hierarchical\'        => false,
        \'public\'              => true,
        \'show_ui\'             => true,
        \'show_in_menu\'        => true,
        \'menu_position\'       => 15,
        \'menu_icon\'           => \'dashicons-admin-links\',
        \'show_in_admin_bar\'   => false,
        \'show_in_nav_menus\'   => true,
        \'can_export\'          => true,
        \'has_archive\'         => false,
        \'exclude_from_search\' => true,
        \'publicly_queryable\'  => true,
        \'rewrite\'             => $rewrite,
        \'capability_type\'     => \'page\',
        \'show_in_rest\'        => true,
    ];

    $args = apply_filters( \'single-link/post_type/args\', $args );
    register_post_type( \'single-link\', $args );
从URL中删除段塞:

function remove_cpt_slug( $post_link, $post, $leavename ) {

if ( \'single-link\' != $post->post_type || \'publish\' != $post->post_status ) {
    return $post_link;
}

$post_link = str_replace( \'/\' . $post->post_type . \'/\', \'/\', $post_link );

return $post_link;
 }
add_filter( \'post_type_link\', \'remove_cpt_slug\', 10, 3 );


function change_slug_struct( $query ) {

if ( ! $query->is_main_query() || 2 != count( $query->query ) || ! isset( $query->query[\'page\'] ) ) {
    return;
}

if ( ! empty( $query->query[\'name\'] ) ) {
    $query->set( \'post_type\', array( \'post\', \'single-link\', \'page\' ) );
}
}
add_action( \'pre_get_posts\', \'change_slug_struct\' );
(此代码来自here)

现在点击publish后,slug/single-link/被删除,但我们在访问页面时总是得到404。更改/重新保存永久链接没有帮助。我做错了什么?

3 个回复
最合适的回答,由SO网友:Vlad Olaru 整理而成

自定义post类型的注册和permalink修改是可以的。问题在于WordPress重写规则很可能会匹配到页面的简单链接的“清理”URL,它会设置pagename 查询var notname 作为您的change_slug_struct() 假定的功能。

因此,将函数更改为此以考虑所有情况:

function change_slug_struct( $query ) {

    if ( ! $query->is_main_query() || 2 != count( $query->query ) || ! isset( $query->query[\'page\'] ) ) {
        return;
    }

    if ( ! empty( $query->query[\'name\'] ) ) {
        $query->set( \'post_type\', array( \'post\', \'single-link\', \'page\' ) );
    } elseif ( ! empty( $query->query[\'pagename\'] ) && false === strpos( $query->query[\'pagename\'], \'/\' ) ) {
        $query->set( \'post_type\', array( \'post\', \'single-link\', \'page\' ) );

        // We also need to set the name query var since redirect_guess_404_permalink() relies on it.
        $query->set( \'name\', $query->query[\'pagename\'] );
    }
}
add_action( \'pre_get_posts\', \'change_slug_struct\' );

SO网友:luukvhoudt

你必须改变perma结构。默认情况下,只有url以slug前缀开头时,才能找到自定义帖子类型的帖子。更改前缀时,您将遇到与删除前缀时类似的问题,have a look at this post.

要实现删除post类型slug前缀,您应该钩住single-link_rewrite_rules 然后遍历这些规则并删除其中的前缀。

Note: perma结构中的更改可能会导致url冲突。

SO网友:Dimitrije Djekanovic

对于多个自定义帖子类型,请按以下方式进行调整

     $query->set( \'post_type\', array( \'post\', \'custom1\', \'page\' ) && array( \'post\', \'custom2\', \'page\' ) );

结束