首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Wordpress中包含类别的菜单(+ Twig/Timber)

在Wordpress中包含类别的菜单(+ Twig/Timber)
EN

Stack Overflow用户
提问于 2021-07-02 10:23:57
回答 1查看 278关注 0票数 0

我是Wordpress的新手,所以我很抱歉(请告诉我),如果我错过了一些非常基本/琐碎的东西。

我有一个页面,显示所有的帖子(自定义类型“内容”)。我希望有一个包含类别项目的菜单,这样点击一个给定的类别就可以显示该类别中的所有帖子。

我现在拥有的是:

代码语言:javascript
复制
<!-- menu.twig -->
<li>
  {% for item in menu.items %}
    <ul>
      <a target='{{ item.target }}' href='{{ item.link }}'>{{ item.title }}</a>
    </ul>
  {% endfor %}
</li>
代码语言:javascript
复制
// 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“是一个示例类别),而该页面是空的。我需要做些什么才能让页面显示帖子?

我已经阅读了我能找到的所有内容,据我理解,这应该是自动完成的,而不需要配置任何东西(?)。

Idea:在我编写这篇文章时,我在想,也许我需要创建一个.php (和相应的.twig)文件(如content_taxonomy.php),并明确指定我希望获得选定类别…的帖子如果这有什么意义请告诉我。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-07-02 10:38:10

最简单的方法是(减去菜单)为自定义post类型创建一个归档模板。

使用当前的wordpress archive.php模板,将其复制并重新命名为archive-POSTTYPE.php < Rename 'POSTTYPE‘以匹配您的自定义post类型。对你来说,这将是archive-content.php

在默认情况下,应该创建一个自定义归档页来加载您的自定义post类型。您也可以使用分类法和其他post类型来实现这一点。

关于菜单,您可以创建一个像上面这样的函数,它显示自定义的post类型名称或post类型分类,这些分类链接到“归档-post类型”,以显示每个页面的单独页面。

更多信息:https://developer.wordpress.org/themes/basics/template-hierarchy/

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68223551

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档