繁体   English   中英

如何从自定义帖子类型网址中删除分类标本?

[英]How to remove taxonomy slug from custom post type url?

我有一个自定义的帖子类型(产品)与分类product-type 我的一个网址是这样的:
http://www.naturesbioscience.com/product-type/immune-support-supplements/
我希望这样:
http://www.naturesbioscience.com/immune-support-supplements/

我在register_taxonomy函数中使用了"rewrite" => array('slug' => '/ ', 'with_front' => false ,我得到的网址如下:
http://www.naturesbioscience.com/immune-support-supplements/
但我在其他页面中找不到404。

有人可以帮帮我吗?

我想你忘了重写自定义分类法post slug。
register_post_type methord中写下这个。

'rewrite' => array('slug' => 'product-type')

现在,您必须从自定义产品中删除product-type slug

/**
 * Remove the slug from published post permalinks.
 */
function custom_remove_cpt_slug($post_link, $post, $leavename)
{
    if ('product-type' != $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', 'custom_remove_cpt_slug', 10, 3);

现在,因为你已经删除了自定义帖子类型slug所以WordPress将尝试将其与页面或帖子匹配,所以你告诉WP也检查自定义帖子类型中的URL。 所以用它来:

function custom_parse_request_tricksy($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', 'product-type', 'page'));
    }
}

add_action('pre_get_posts', 'custom_parse_request_tricksy');

参考: 从自定义帖子类型URL中删除Slugs

希望这可以帮助!

add_filter( 'post_type_link', 'change_product_request', 10, 3 );

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

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

        $post_link = str_replace( "/{$post->post_type}/" , '/', $post_link );

        return $post_link;
}

现在,您将获得一个404页面,因为WordPress只允许帖子和页面以这种方式运行。 您还必须添加以下内容:

add_action( 'pre_get_posts', 'product_permalink' );

function product_permalink( $query ) {

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

        if ( ! empty( $query->query['pagename'] ) ) { // name has been updated to pagename so  $query->query['pagename']

            global $wpdb;
            $pt = $wpdb->get_var(
                "SELECT post_type FROM `{$wpdb->posts}` " .
                "WHERE post_name = '{$query->query['pagename']}'"
            );
            $query->set( 'post_type', $pt );
        }

        return $query;
}

我有类似的问题。 这个问题很老了,但是任何寻找合适答案的人都可以看到这一点。

不要为重写slug传递“/”,因为它会导致比它解决的问题更多,因为在这种情况下,导致其他页面出现404错误。

首先,我们需要从已发布帖子的URL中删除slug。 将代码粘贴到functions.php

/**
 * Remove the slug from published post permalinks. Only affect our CPT though.
 */
function sh_remove_cpt_slug( $post_link, $post, $leavename ) {

    if ( in_array( $post->post_type, array( 'product-type' ) )
        || 'publish' == $post->post_status )
        $post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );
    return $post_link;
}
add_filter( 'post_type_link', 'sh_remove_cpt_slug', 10, 3 );

这仍然会导致错误,因为它指定只有'post'和'page'帖子类型可以有没有post-type slug的url。

现在教WP,CPT也会有没有slug的URL,我们需要在我们的functions.php得到它

function sh_parse_request_tricksy( $query ) {

    // Only loop the main query
    if ( ! $query->is_main_query() ) {
        return;
    }

    // Only loop 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( 'product-type' ) );
    }
}
add_action( 'pre_get_posts', 'sh_parse_request_tricksy' );

就是这个。 参考: https//wordpress.stackexchange.com/a/320711/98322

暂无
暂无

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

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