Reputation: 69
I am new to Wordpress so I'm sorry (and please let me know) if I'm missing out on something very basic/trivial.
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. 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.
Upvotes: 0
Views: 447
Reputation: 469
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. For you this would be 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/
Upvotes: 0