如何为自定义帖子类型更新Everywhere固定链接?

时间:2017-06-02 作者:bhamrick

我正在尝试将我的营销登录页(挤压页面)分成不同的帖子类型,以便更容易地组织它们,也更容易与我的一些插件交互。在通过生成自定义帖子类型后,我现在就可以使用它了GenerateWP, 我已经修改了permalink,通过利用这个片段来删除post类型的slughere. 自动生成的指向新帖子类型的链接似乎使用了未修改的URL,其中包括post\\u类型,现在解析为404。例如,自定义帖子类型列表屏幕中项目列表中的“查看”链接。

有没有一种方法可以永久性地修改永久链接,这样对帖子的任何引用都会生成相同的URL?以下是我现在拥有的:

if ( ! function_exists(\'bh_squeeze_page\') ) {

    // Register Custom Post Type
    function bh_squeeze_page() {

        $labels = array(
            \'name\'                  => _x( \'Squeeze Pages\', \'Post Type General Name\', \'text_domain\' ),
            \'singular_name\'         => _x( \'Squeeze Page\', \'Post Type Singular Name\', \'text_domain\' ),
            \'menu_name\'             => __( \'Squeeze Pages\', \'text_domain\' ),
            \'name_admin_bar\'        => __( \'Squeeze Page\', \'text_domain\' ),
            \'archives\'              => __( \'Squeeze Page Archives\', \'text_domain\' ),
            \'attributes\'            => __( \'Squeeze Page Attributes\', \'text_domain\' ),
            \'parent_item_colon\'     => __( \'Parent Squeeze Page:\', \'text_domain\' ),
            \'all_items\'             => __( \'All Squeeze Pages\', \'text_domain\' ),
            \'add_new_item\'          => __( \'Add New Squeeze Page\', \'text_domain\' ),
            \'add_new\'               => __( \'Add New\', \'text_domain\' ),
            \'new_item\'              => __( \'New Squeeze Page\', \'text_domain\' ),
            \'edit_item\'             => __( \'Edit Squeeze Page\', \'text_domain\' ),
            \'update_item\'           => __( \'Update Squeeze Page\', \'text_domain\' ),
            \'view_item\'             => __( \'View Squeeze Page\', \'text_domain\' ),
            \'view_items\'            => __( \'View Squeeze Pages\', \'text_domain\' ),
            \'search_items\'          => __( \'Search Squeeze Page\', \'text_domain\' ),
            \'not_found\'             => __( \'Not found\', \'text_domain\' ),
            \'not_found_in_trash\'    => __( \'Not found in Trash\', \'text_domain\' ),
            \'featured_image\'        => __( \'Featured Image\', \'text_domain\' ),
            \'set_featured_image\'    => __( \'Set featured image\', \'text_domain\' ),
            \'remove_featured_image\' => __( \'Remove featured image\', \'text_domain\' ),
            \'use_featured_image\'    => __( \'Use as featured image\', \'text_domain\' ),
            \'insert_into_item\'      => __( \'Insert into item\', \'text_domain\' ),
            \'uploaded_to_this_item\' => __( \'Uploaded to this item\', \'text_domain\' ),
            \'items_list\'            => __( \'Items list\', \'text_domain\' ),
            \'items_list_navigation\' => __( \'Items list navigation\', \'text_domain\' ),
            \'filter_items_list\'     => __( \'Filter items list\', \'text_domain\' ),
        );
        $rewrite = array(
            \'slug\'                  => \'sp\',
            \'with_front\'            => false,
            \'pages\'                 => true,
            \'feeds\'                 => true,
        );
        $args = array(
            \'label\'                 => __( \'Squeeze Page\', \'text_domain\' ),
            \'description\'           => __( \'Squeeze Pages\', \'text_domain\' ),
            \'labels\'                => $labels,
            \'supports\'              => array( \'title\', \'editor\', \'excerpt\', \'author\', \'thumbnail\', \'comments\', \'trackbacks\', \'revisions\', \'custom-fields\', \'page-attributes\', \'post-formats\', ),
            \'taxonomies\'            => array( \'category\' ),
            \'hierarchical\'          => true,
            \'public\'                => true,
            \'show_ui\'               => true,
            \'show_in_menu\'          => true,
            \'menu_position\'         => 5,
            \'show_in_admin_bar\'     => true,
            \'show_in_nav_menus\'     => true,
            \'can_export\'            => true,
            \'has_archive\'           => false,
            \'exclude_from_search\'   => false,
            \'publicly_queryable\'    => true,
            \'rewrite\'               => $rewrite,
            \'capability_type\'       => \'page\',
            \'show_in_rest\'          => false,
            \'rest_base\'             => \'bh_squeeze_page\',
        );
        register_post_type( \'bh_squeeze_page\', $args );

    }
    add_action( \'init\', \'bh_squeeze_page\', 0 );

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

    if ( \'bh_squeeze_page\' != $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\', \'na_remove_slug\', 10, 3 );

    function na_parse_request( $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\', \'bh_squeeze_page\', \'page\' ) );
    }
    }
    add_action( \'pre_get_posts\', \'na_parse_request\' );

}

1 个回复
最合适的回答,由SO网友:Milo 整理而成

该行:

$post_link = str_replace( \'/\' . $post->post_type . \'/\', \'/\', $post_link );
没有达到你的预期,因为你的post slug没有$post->post_type, 它是sp, 如下所示:

$rewrite = array(
    \'slug\'        => \'sp\',
    \'with_front\'  => false,
    \'pages\'       => true,
    \'feeds\'       => true,
);

结束

相关推荐

Change Taxonomy Permalinks

我有自定义帖子,我创建了一个显示所有自定义帖子的页面。示例:www.example.com/archive-page我想知道是否可以更改与此自定义帖子相关的类别和标签的永久链接。现在我有:www.example.com/my-custom-post-type-cats/my-category-1www.example.com/my-custom-post-type-tags/my-tag-1</我想要这样的东西:www.example.com/archive-page?category=1www.e