我正在尝试创建一个菜单,它可以循环浏览已注册的自定义帖子类型和指向其存档页面的链接。我已经能够获得帖子类型名称列表,但它没有生成链接。它只输出一个围绕帖子类型名称的en空锚定标记。
$args = array( \'public\' => true,\'_builtin\' => false );
$output = \'names\';
$operator = \'and\';
$post_types = get_post_types( $args, $output, $operator );
foreach ( $post_types as $post_type ) {
$link = get_post_type_archive_link($post_type);
echo \'<li><a href="\' . $link . \'">\' . $post_type . \'</a></li>\';
}
这是我的输出。
<li><a href="">posttypeA</a></li>
<li><a href="">posttypeB</a></li>
<li><a href="">posttypeC</a></li>
我有posttypeX存档。每个帖子类型的php文件。
我的所有自定义帖子类型的注册方式与此类似:
function post_type_snippets() {
register_post_type(\'snippets\', array(
\'label\'=>\'Snippets\',
\'menu_icon\' => \'\',
\'labels\'=>array(
\'name\'=>_x(\'Snippets\', \'post type general name\'),
\'singular_name\'=>_x(\'Snippet\', \'post type singular name\'),
\'add_new\'=>_x(\'Add New\', \'snippets\'),
\'add_new_item\'=>__(\'Add New Snippet\'),
\'edit_item\'=>__(\'Edit Snippet\'),
\'new_item\'=>__(\'New Snippet\'),
\'view_item\'=>__(\'View Snippet\'),
\'search_items\'=>__(\'Search Snippets\'),
\'not_found\'=>__(\'No snippets found\'),
\'not_found_in_trash\'=>__(\'No snippets found in Trash\'),
\'parent_item_colon\'=>\'\'),
\'public\'=>true,
\'publicly_queryable\'=>true,
\'show_ui\'=>true,
\'query_var\'=>true,
\'rewrite\'=>false,
\'capability_type\'=>\'post\',
\'supports\'=>array(\'title\',\'thumbnail\',\'comments\',\'revisions\',\'post-formats\'),
\'taxonomies\'=>array(\'category\', \'post_tag\'),
\'slug\'=>\'snippets\',
\'hierarchical\'=>false,
\'menu_position\'=>4
));
}
add_action(\'init\', \'post_type_snippets\');