简体   繁体   English

Wordpress 中的带有类别的菜单(+ Twig/Timber)

[英]Menu with categories in Wordpress (+ Twig/Timber)

I am new to Wordpress so I'm sorry (and please let me know) if I'm missing out on something very basic/trivial.我是 Wordpress 的新手,所以如果我错过了一些非常基本/微不足道的东西,我很抱歉(并请告诉我)。

I have a page which displays all posts (of a Custom Type "Content").我有一个页面显示所有帖子(自定义类型“内容”)。 And I would like to have a menu with category items so that clicking a given category would display all posts in that category.我想要一个包含类别项目的菜单,以便单击给定类别将显示该类别中的所有帖子。

What I currently have:我目前拥有的:

<!-- 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 );

I have categories displayed as expected but when I click on them, I'm routed to {url}/content_taxonomy/a/ ("a" is an example category), and that page is empty.我按预期显示了类别,但是当我点击它们时,我被路由到{url}/content_taxonomy/a/ (“a”是一个示例类别),并且该页面是空的。 What do I need to do to get the page to display posts?我需要做什么才能让页面显示帖子?

I've read everything I could find and as far as I understand this should be done automatically, without me needing to to configure anything (?).我已经阅读了我能找到的所有内容,据我所知,这应该自动完成,无需配置任何内容(?)。

Idea : As I'm writing this, I'm thinking maybe I need to create a .php (and corresponding .twig) file (like content_taxonomy.php) and specify explicitly that I'd like to get posts of a selected category… Please let me know if that makes any sense.想法:在我写这篇文章的时候,我在想也许我需要创建一个 .php(和相应的 .twig)文件(比如 content_taxonomy.php)并明确指定我想要获取选定类别的帖子......请让我知道这是否有意义。

The simplest way to do this is to (minus the menu) would be to create an archive template for the custom post type.最简单的方法是(减去菜单)为自定义帖子类型创建一个存档模板。

Take the current wordpress archive.php template, copy this and re-name it to archive-POSTTYPE.php < Rename 'POSTTYPE' to match your custom post type.获取当前的 wordpress archive.php模板,复制它并将其重命名为archive-POSTTYPE.php < Rename 'POSTTYPE' 以匹配您的自定义帖子类型。 For you this would be archive-content.php对你来说,这将是archive-content.php

This, by default should create a custom archive page for loading your custom post type.默认情况下,这应该创建一个自定义存档页面来加载您的自定义帖子类型。 You can also do this with Taxonomies and other post types.您也可以使用分类法和其他帖子类型来执行此操作。

In regards to the menu, you could create a function like you have above that displays the custom post type names or post type taxonomies that links to the 'archive-posttype' to display a separate page for each.关于菜单,您可以创建一个类似于上面的功能,显示自定义帖子类型名称或链接到“归档帖子类型”的帖子类型分类法,以显示每个单独的页面。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM