首页 > 解决方案 > 带有自定义永久链接和标签的 WordPress 自定义帖子类型

问题描述

我在这里找到了一些示例来编写代码。但现在我被困住了,找不到任何解决这个问题的方法:

我创建了一个自定义帖子类型和一个设置页面,用户可以在其中为此 CPT 设置自己的永久链接结构。比如用户可以设置分类、帖子id、帖子名称等等……下面是第一次运行的截图:

在此处输入图像描述

问题是,如果帖子名称在 URL 中但没有 ID,我只能访问前端的页面。我是否使用%post_id%%postname% als 占位符都没关系。

但它也应该与其他永久链接标签/占位符一起使用。

这是代码......在https://shibashake.com/wordpress-theme/custom-post-type-permalinks-part-2的帮助下

我也测试过 add $wp_rewrite->flush_rules();,但没有成功。

    add_action( 'init', 'init_landingpage_cpt' );

function init_landingpage_cpt() {
    $settings  = new LP_Admin_Settings();
    $post_type = 'landingpage';
    $slug      = $settings->get( 'slug' ); // Value: swipefile/%postname%

    register_post_type( $post_type, array(
        'description'        => esc_html__( 'Library with landingpages.', 'lp_txt' ),
        'public'             => true,
        'publicly_queryable' => true,
        'query_var'          => true,
        'show_ui'            => true,
        'show_in_menu'       => 'landingapge-lib',
        'rewrite'            => array(
            'slug'       => $slug,
            'with_front' => false,
        ),
        'capability_type'    => 'page',
        'has_archive'        => true,
        'hierarchical'       => true,
        'menu_position'      => null,
        'supports'           => array( 'title', 'editor', 'thumbnail', 'excerpt' ),
        'labels'             => array(
            'name'               => esc_html__( 'Landingpages', 'lp_txt' ),
            'singular_name'      => esc_html__( 'Landingpage', 'lp_txt' ),
            'menu_name'          => esc_html__( 'Landingpages', 'lp_txt' ),
            'name_admin_bar'     => esc_html__( 'Landingpages', 'lp_txt' ),
            'add_new'            => esc_html__( 'Add new', 'lp_txt' ),
            'add_new_item'       => esc_html__( 'Add new landingpage', 'lp_txt' ),
            'new_item'           => esc_html__( 'New landingpage', 'lp_txt' ),
            'edit_item'          => esc_html__( 'Edit landingpage', 'lp_txt' ),
            'view_item'          => esc_html__( 'View landingpage', 'lp_txt' ),
            'all_items'          => esc_html__( 'Landingpages', 'lp_txt' ),
            'search_items'       => esc_html__( 'Search landingpages', 'lp_txt' ),
            'parent_item_colon'  => esc_html__( 'Parent landingpages:', 'lp_txt' ),
            'not_found'          => esc_html__( 'No landingpages found.', 'lp_txt' ),
            'not_found_in_trash' => esc_html__( 'No landingpages found in trash.', 'lp_txt' ),
        ),
    ) );

    global $wp_rewrite;

    $wp_rewrite->add_rewrite_tag( "%post_id%", '([^/]+)', "{$post_type}=" );
    $wp_rewrite->add_rewrite_tag( "%postname%", '([^/]+)', "{$post_type}=" );
    $wp_rewrite->add_permastruct( $post_type, "/{$slug}/", false );
}

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

function post_type_link_landingpage( $permalink, $post_id, $leavename ) {
    $post = get_post( $post_id );

    if ( ! empty( $post ) && ! empty( $permalink ) && ! in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) ) ) {

        $search = array(
            $leavename ? '' : '%postname%',
            '%post_id%',
        );

        $replace = array(
            $post->post_name,
            $post->ID,
        );

        $permalink = str_replace( $search, $replace, $permalink );
    }

    return $permalink;
}

标签: phpwordpresscustom-post-typepermalinks

解决方案


推荐阅读