自定义帖子类型-保留存档页面

时间:2021-03-02 作者:Jon

我试图弄清楚以下几点:

我有一个CPT叫做;播客“;其中有一个slug as/podcasts,我有一个页面叫做;播客“;其中有一个slug as/podcasts/我希望我的单个podcast帖子有一个永久链接/podcasts/%podcast\\u category%/post\\u name

当我这样做时,我无法编辑播客页面,因为wordpress会显示归档页面。如何修改代码,使归档页面具有不同的slug,但仍保留单个播客的/podcasts/%podcast\\u category%/post\\u名称,并且/podcasts/可以用作页面。

//register podcast posts
function custom_post_podcasts_register(){

$labels = array(
    \'name\'               => _x(\'Podcast\', \'post type general name\'),
    \'singular_name\'      => _x(\'Podcast\', \'post type singular name\'),
    \'add_new\'            => _x(\'Add New\', \'podcast\'),
    \'add_new_item\'       => __(\'Add New Podcast\'),
    \'edit_item\'          => __(\'Edit Podcast\'),
    \'new_item\'           => __(\'New Integration\'),
    \'view_item\'          => __(\'View Podcast\'),
    \'search_items\'       => __(\'Search Podcast\'),
    \'not_found\'          => __(\'Nothing found\'),
    \'not_found_in_trash\' => __(\'Nothing found in Trash\'),
    \'parent_item_colon\'  => \'\'
);

$args = array(
    \'labels\'             => $labels,
    \'public\'             => true,
    \'publicly_queryable\' => true,
    \'show_ui\'            => true,
    \'query_var\'          => true,
    \'rewrite\'            => true,
    \'capability_type\'    => \'post\',
    \'hierarchical\'       => false,
    \'menu_position\'      => null,
    \'show_in_rest\'       => true,
    \'rewrite\'            => array(\'slug\' => \'podcasts\',\'with_front\' => false),
    \'has_archive\'        => \'podcast-all\',
    \'supports\'           => array(\'title\', \'editor\', \'thumbnail\', \'excerpt\',\'podcast_category\', \'podcast_tags\'),
    \'taxonomies\'         => array( \'podcast_category\', \'podcast_tags\' ),
    \'menu_icon\'          => \'dashicons-format-quote\',
    \'has_archive\'        => true
  ); 
register_post_type(\'podcast\' , $args);
}
add_action(\'init\', \'custom_post_podcasts_register\');
add_filter(\'manage_edit-podcast_columns\', \'admin_remove_columns\');

//create a custom taxonomy name it "type" for your posts
function podcast_custom_taxonomy() {

$labels = array(
  \'name\' => _x( \'Podcast Category\', \'taxonomy general name\' ),
  \'singular_name\' => _x( \'podcast_category\', \'taxonomy singular name\' ),
  \'edit_item\' => __( \'Edit Podcast Category\' ), 
  \'update_item\' => __( \'Update Podcast Category\' ),
  \'add_new_item\' => __( \'Add New Podcast Category\' ),
  \'new_item_name\' => __( \'New Podcast Category Name\' ),
  \'menu_name\' => __( \'Podcast Category\' ),
);  

register_taxonomy(\'podcast_category\',array(\'podcast\'), array(
  \'labels\'                     => $labels,
      \'hierarchical\'               => true,
      \'public\'                     => true,
      \'show_ui\'                    => true,
      \'show_admin_column\'          => true,
      \'show_in_nav_menus\'          => true,
      \'show_tagcloud\'              => true,
      \'show_in_rest\'               => true,
  \'rewrite\' => array( \'slug\' => \'podcast_category\' ),
));
}
// Let us create Taxonomy for Custom Post Type
add_action( \'init\', \'podcast_custom_taxonomy\', 0 );

// Register Custom Taxonomy
function podcast_tags_taxononmy() {

$labels = array(
    \'name\'                       => \'Podcast Tag\',
    \'singular_name\'              => \'Podcast Tag\',
    \'menu_name\'                  => \'Podcast Tags\',
    \'all_items\'                  => \'All Podcast Tags\',
    \'parent_item\'                => \'Parent Podcast Tag\',
    \'parent_item_colon\'          => \'Parent Podcast Tag:\',
    \'new_item_name\'              => \'New Podcast Tag\',
    \'add_new_item\'               => \'Add New Podcast Tag\',
    \'edit_item\'                  => \'Edit Podcast Tag\',
    \'update_item\'                => \'Update Podcast Tag\',
    \'separate_items_with_commas\' => \'Separate Podcast Tags with commas\',
    \'search_items\'               => \'Search Podcast Tags\',
    \'add_or_remove_items\'        => \'Add or remove Podcast Tags\',
    \'choose_from_most_used\'      => \'Choose from the most used Podcast Tags\',
    \'not_found\'                  => \'Not Found\',
);
$args = array(
    \'labels\'                     => $labels,
    \'hierarchical\'               => true,
    \'public\'                     => true,
    \'show_ui\'                    => true,
    \'show_admin_column\'          => true,
    \'show_in_nav_menus\'          => true,
    \'show_tagcloud\'              => true,
    \'show_in_rest\'               => true,
);
register_taxonomy( \'podcast-tags\', array( \'podcast\' ), $args );

}

// Hook into the \'init\' action
add_action( \'init\', \'podcast_tags_taxononmy\', 0 );

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

以下假设播客归档文件的slug与播客页面的slug不同。否则,当用户访问时,WordPress将不知道要为哪个资源提供服务/podcasts/.

为分类法指定默认术语。这确保了每个播客在一个类别中登陆,并且在永久链接中有一个类别。看见register_taxonomy() 对于完整参数。

register_taxonomy( \'podcast_category\', array(\'podcast\'), array(
    \'rewrite\' => array( \'slug\' => \'podcasts-category\' ),
    \'default_term\' => array(
        \'name\' => esc_html__( \'Uncategorized\' ),
    ),
) );
在Post Type rewrite slug中添加任意占位符。

register_post_type( \'podcast\' , array(
    \'rewrite\' => array(
        \'slug\'       => \'podcasts/%podcast_category%\',
        \'with_front\' => false
    ),
    \'has_archive\' => \'podcasts-all\',
) );
替换Postcast URL中的任意占位符。

/**
 * Post Type Link
 * Replace placeholder with post term.
 *
 * @param String $post_link
 * @param WP_Post $post
 *
 * @return String $post_link
 */
function podcast_post_link( $post_link, $post ) {

    if( \'podcast\' !== $post_type ) {
        return $post_link;
    }

    $terms = wp_get_object_terms( $post->ID, \'podcast_category\' );

    if( ! ( empty( $terms ) || is_wp_error( $terms ) ) ) {
        $post_link = str_replace( \'%podcast_category%\' , $terms[0]->slug , $post_link );
    }

    return $post_link;
}
add_filter( \'post_type_link\', \'podcast_post_link\', 10, 2 );
保存永久链接。前往设置(>);永久链接并单击保存。这将刷新permalink结构。你应该得到什么:

播客页面:/podcasts/
播客存档:/podcasts-all/
播客类别:/podcast-category/uncategorized/
播客单数:/podcasts/uncategorized/podcast-slug/

请注意register_post_type() 你在打电话has_archive 两次第二个has_archive => true 正在覆盖您的post-type存档段塞。

相关推荐