如何解决Wordpress 中的带有类别的菜单+ Twig/Timber
我是 wordpress 的新手,所以如果我错过了一些非常基本/微不足道的东西,我很抱歉(并请告诉我)。
我有一个页面显示所有帖子(自定义类型“内容”)。我想要一个包含类别项目的菜单,以便单击给定类别将显示该类别中的所有帖子。
我目前拥有的:
<!-- menu.twig -->
<li>
{% for item in menu.items %}
<ul>
<a target='{{ item.target }}' href='{{ item.link }}'>{{ item.title }}</a>
</ul>
{% endfor %}
</li>
// functions.PHP
// Code from the Timber starter theme
/** This is where you add some context
*
* @param string $context context['this'] Being the Twig's {{ this }}.
*/
public function add_to_context( $context ) {
$context['menu'] = new Timber\Menu();
$context['site'] = $this;
return $context;
}
// ...
// Content Post Type
function content() {
$labels = array(
'name' => _x( 'Content','post type general name' ),'singular_name' => _x( 'Content','post type singular name' ),'add_new' => _x( 'Add New','Content' ),'add_new_item' => __( 'Add New Content' ),'edit_item' => __( 'Edit Content' ),'new_item' => __( 'New Content' ),'all_items' => __( 'All Content' ),'view_item' => __( 'View Content' ),'search_items' => __( 'Search Content' ),'not_found' => __( 'No content found' ),'not_found_in_trash' => __( 'No content found in the Trash' ),'parent_item_colon' => '','menu_name' => 'Content',);
$args = array(
'labels' => $labels,'description' => 'Content','public' => true,'supports' => array( 'title','editor','excerpt' ),'has_archive' => true,'rewrite' => array( 'slug' => '/' ),'hierarchical' => true,);
register_post_type( 'content',$args );
}
add_action( 'init','content' );
// Content Taxonomy
function content_taxonomy_init() {
$args = array(
'show_admin_column' => true,'hierarchical' => true,);
register_taxonomy( 'content_taxonomy',array('content'),$args );
}
add_action( 'init','content_taxonomy_init',0 );
我按预期显示了类别,但是当我点击它们时,我被路由到 {url}/content_taxonomy/a/
(“a”是一个示例类别),并且该页面是空的。我需要做什么才能让页面显示帖子?
我已经阅读了我能找到的所有内容,据我所知,这应该是自动完成的,无需我进行任何配置 (?)。
想法:在我写这篇文章的时候,我在想也许我需要创建一个 .PHP(和相应的 .twig)文件(比如 content_taxonomy.PHP)并明确指定我'想要获得选定类别的帖子……如果有任何意义,请告诉我。
解决方法
最简单的方法是(减去菜单)为自定义帖子类型创建存档模板。
采用当前的 wordpress archive.php
模板,复制它并将其重命名为 archive-POSTTYPE.php
archive-content.php
默认情况下,这应该创建一个自定义存档页面来加载您的自定义帖子类型。您也可以使用分类法和其他帖子类型来执行此操作。
关于菜单,您可以创建一个类似于上面的功能,显示自定义帖子类型名称或链接到“归档帖子类型”的帖子类型分类法,以显示每个单独的页面。
更多信息:https://developer.wordpress.org/themes/basics/template-hierarchy/
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。