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

时间:2016-12-19 20:41:38

标签: wordpress url-rewriting custom-post-type taxonomy

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

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

任何人都可以帮助我?

3 个答案:

答案 0 :(得分:1)

我想你忘记重写自定义分类法了 在register_post_type方法中写下这个。

'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');

参考:Remove The Slugs from Custom Post Type URL

希望这有帮助!

答案 1 :(得分:0)

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;
}

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

Sub PrintIt()

Dim objWord As Word.Application
Dim objDocTotal As Word.Document
Dim objDoc As Word.Document
Dim i As Integer
Dim strOutfile As String
Dim rg As Word.Range

    ActiveSheet.OLEObjects("SalaryPaycheck").Activate
    Set objWord = GetObject(, "Word.Application")
    objWord.Visible = True
    Set objDoc = objWord.ActiveDocument
    Set objDocTotal = Documents.Add
    objWord.Application.DisplayAlerts = wdAlertsNone
    objWord.Application.ScreenUpdating = True

    For i = 1 To 10

        Range("Key").Value = i

        With objDoc
            .Fields.Update
            .Content.Copy
        End With

        Set rg = objDocTotal.Content
        With rg
            .Collapse Direction:=wdCollapseEnd
            If i > 1 Then .InsertBreak wdPageBreak
            .PasteAndFormat wdFormatOriginalFormatting
        End With
    Next i


    strOutfile = "<Path>\Salary.pdf"

    objDocTotal.ExportAsFixedFormat outputfileName:= _
                                    strOutfile, exportformat:=wdExportFormatPDF, _
                                    openafterexport:=False, optimizefor:=wdExportOptimizeForPrint, Range:= _
                                    wdExportAllDocument, From:=1, To:=1, Item:=wdExportDocumentContent



    objDocTotal.Close False
    objWord.Quit
    Set objDoc = Nothing
    Set objWord = Nothing

End Sub

答案 2 :(得分:0)

我有类似的问题。这个问题已经很老了,但是任何寻求适当答案的人都可以看到。

请勿将“ /”传递给重写段,因为它引起的问题超出了解决的范围,在这种情况下,这会导致其他页面出现404错误。

首先,我们需要从已发布帖子的网址中删除该标签。将代码粘贴到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也将拥有不带子弹的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