Hi guys,
I am struggling with getting a Custom Post Type to display in/with a loop. Your code works beautifully for adding these posts (or excerpts) on every page. My struggle is to get my Custom Posts displayed only on specific single-post pages and category/archive pages.
My goal: I am creating a Wordpress theme for a playwright. Ultimately, I want to have at least three, possibly four Custom Post Types: full-length plays, one-act plays, monologues, and "mouthoffs" which are very short monologues. I'm using a post type so I can have a taxonomy of categories and tags. Since I was having problems debugging code, I decided to cut back to a basic CPT that I called "simple", plural "simples". I made this through my own custom plugin, which seems to be working fine. That is, I have created two simples and assigned them to categories. I have also succeeded in getting these categories displayed in the main nav menu. (I would be equally happy to display them in the sidebar Categories menu.)
I would be happy to share any / all code. (What code do you need to see?)
Looking forward to your reply,
Hey Wyn,
Beans displays custom post type posts and taxonomies they same way it would display pages. Accessing your post type and tanoxomies would depend on how your permalinks are set, Beans will treat is like WP Core themes would do.
I am not sure I understand 100% what you are trying to do and what can't you get to work. Could you very simply explain what part isn't working and where you are stuck at.
Thanks 🙂
I found this post when I search solution for custom types for show testymonials... After more search in WP codex i found solution and I want to share with community.
For this i used approach describede here: https://www.smashingmagazine.com/2012/11/complete-guide-custom-post-types/
I split my code in three parts:
- Add Custom post type,
- Add custom taxonomy (for categories),
- Display all post type posts by name in a loop.
1. Add Custom post type
function custom_post_testymonial() {
$labels = array(
'name' => _x( 'Testymonials', 'post type general name' ),
'singular_name' => _x( 'Testymonial', 'post type singular name' ),
'add_new' => _x( 'Add New', 'testymonial' ),
'add_new_item' => __( 'Add New Testymonial' ),
'edit_item' => __( 'Edit Testymonial' ),
'new_item' => __( 'New Testymonial' ),
'all_items' => __( 'All Testymonials' ),
'view_item' => __( 'View Testymonial' ),
'search_items' => __( 'Search Testymonial' ),
'not_found' => __( 'No Testymonial found' ),
'not_found_in_trash' => __( 'No Testymonial found in trash' ),
'parent_item_colon' => '',
'menu_name' => 'Testymonials'
);
$args = array(
'labels' => $labels,
'description' => 'Holds Testymonials data',
'public' => true,
'menu_position' => 5,
'supports' => array( 'title', 'editor' ),
'has_archive' => true,
);
register_post_type( 'testymonial', $args );
}
add_action( 'init', 'custom_post_testymonial' );
2. Add custom taxonomy
function custom_taxonomies_testymonial() {
$labels = array(
'name' => _x( 'Testymonial Categories', 'taxonomy general name' ),
'singular_name' => _x( 'Testymonial Category', 'taxonomy singular name' ),
'search_items' => __( 'Search Testymonial Categories' ),
'all_items' => __( 'All Testymonial Categories' ),
'parent_item' => __( 'Parent Testymonial Category' ),
'parent_item_colon' => __( 'Parent Testymonial Category:' ),
'edit_item' => __( 'Edit Testymonial Category' ),
'update_item' => __( 'Update Testymonial Category' ),
'add_new_item' => __( 'Add New Testymonial Category' ),
'new_item_name' => __( 'New Testymonial Category' ),
'menu_name' => __( 'Testymonial Categories' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
);
register_taxonomy( 'testymonial_category', 'testymonial', $args );
}
add_action( 'init', 'custom_taxonomies_testymonial', 0 );
3. Display posts in a loop
add_action( 'beans_main_after_markup', 'add_testymonials' );
function add_testymonials() {
?>
<section class="uk-container uk-container-center uk-block tm-testymonials">
<?php
//Define custom post type name in the arguments
$args = array('post_type' => 'testymonial');
// Define the loop based on arguments
$loop = new WP_Query( $args );
//Display the contents
while ( $loop->have_posts() ) : $loop->the_post();
?>
<h1><?php the_title(); ?></h1>
<div>
<?php the_content(); ?>
</div>
<?php endwhile;?>
</section>
<?php
}