自定义分类法,用于自定义帖子类型永久链接和存档页面

时间:2018-09-30 23:00:49

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

我有3种自定义帖子类型,均具有自定义定义分类法( abc _types)。

我的第一个问题是,我以前使用的是archive-abc.php页面来显示我所拥有的不同帖子类型的存档页面,但是自从我对永久链接使用重写后,它似乎都停止了工作(在此之前)以前是/ publications / TITLE-OF-POST,现在重写使我也可以使用URL中的自定义分类法,例如/ publications / BOOKS / TITLE-OF-POST)。

我可以创建一个页面模板来显示出版物的内容,但宁愿使用存档页面,因为我面临的第二个问题...我复制了帖子类型,分类法之一的代码并重写了以下内容

第二个问题是自定义分类法。如果我直接访问自定义分类页面(/ publications / books),它将使用archive.php显示内容,这是一个非常类似于博客的模板。有没有一种方法可以为自定义分类法定义标准显示页面?我尝试使用taxonomy-publications.php,但无法正常工作。

我希望能够将archive-publications.php用于自定义帖子类型的存档和分类法。

这是在function.php中创建我的帖子类型的方式

    $labels = array(
    "name" => __( "Publications"),
    "singular_name" => __( "Publication"),
    "featured_image" => __( "Publication Cover"),
    "set_featured_image" => __( "Set Publication Cover"),
    "remove_featured_image" => __( "Remove Publication Cover"),
    "use_featured_image" => __( "Use Publication Cover"),
);

$args = array(
    "label" => __( "Publications"),
    "labels" => $labels,
    "description" => "",
    "public" => true,
    "publicly_queryable" => true,
    "show_ui" => true,
    "show_in_rest" => false,
    "rest_base" => "",
    "has_archive" => true,
    "show_in_menu" => true,
    "show_in_nav_menus" => true,
    "exclude_from_search" => false,
    "capability_type" => "post",
    "map_meta_cap" => true,
    "hierarchical" => false,
    "rewrite" => array( "slug" => "publications/%publications_types%", "with_front" => true ),
    "query_var" => true,
    "menu_icon" => "dashicons-book-alt",
    "supports" => array( "title", "editor", "thumbnail" ),
    "taxonomies" => array( "category", "post_tag" ),
);

register_post_type( "publications", $args );

这是分类法

$labels = array(
    "name" => __( "Publication types" ),
    "singular_name" => __( "Publication type" ),
);

$args = array(
    "label" => __( "Publication types" ),
    "labels" => $labels,
    "public" => true,
    "hierarchical" => true,
    "label" => "Publication types",
    "has_archive" => true,
    "show_ui" => true,
    "show_in_menu" => true,
    "show_in_nav_menus" => true,
    "query_var" => true,
    "rewrite" => array( 'slug' => 'publications', 'with_front' => true, ),
    "show_admin_column" => false,
    "show_in_rest" => false,
    "rest_base" => "publications_types",
    "show_in_quick_edit" => false,
);
register_taxonomy( "publications_types", array( "publications" ), $args );

然后为cpt分类法重写URL (我从https://wordpress.stackexchange.com/questions/5308/custom-post-types-taxonomies-and-permalinks获得了代码)

function wpse_5308_post_type_link( $link, $post ) {
    if ( $post->post_type === 'publications' ) {
        if ( $terms = get_the_terms( $post->ID, 'publications_types' ) )
            $link = str_replace( '%publications_types%', current( $terms )->slug, $link );
    }

    return $link;
}

add_filter( 'post_type_link', 'wpse_5308_post_type_link', 10, 2 );

0 个答案:

没有答案