Custom Post Type on front page
-
HI,
I’ve setup a custom post type for events in a WP build. Now, I want to include these posts in a query on the front page, and list them in along with my regular posts (I’m using an ‘if have_posts loop’).
function create_event_postype() { $labels = array( 'name' => _x( 'Events', 'post type general name'), 'singular_name' => _x( 'Event', 'post type singular name'), 'menu_name' => _x( 'Events', 'admin menu'), 'name_admin_bar' => _x( 'Event', 'add new on admin bar'), 'add_new' => _x( 'Add New', 'Event' ), 'add_new_item' => __( 'Add New Event' ), 'new_item' => __( 'New Event' ), 'edit_item' => __( 'Edit Event' ), 'view_item' => __( 'View Event' ), 'all_items' => __( 'All Events' ), 'search_items' => __( 'Search Events' ), 'parent_item_colon' => __( 'Parent Events:' ), 'not_found' => __( 'No Events found.' ), 'not_found_in_trash' => __( 'No Events found in Trash.' ) ); $args = array( 'description' => 'Holds our event specific data', 'labels' => $labels, 'public' => true, 'publicly_queryable' => true, 'show_ui' => true, 'show_in_menu' => true, 'query_var' => true, 'rewrite' => array( "slug" => "events", 'with_front' => true ), 'capability_type' => 'post', 'has_archive' => true, 'hierarchical' => false, 'menu_position' => 4, 'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt' ), 'taxonomies' => array( 'dmc_eventcategory', 'post_tag') ); register_post_type( 'dmc_events', $args ); } add_action( 'init', 'create_event_postype' );I figured out that I need to add my custom post type to the main query, so I added the following code which has included these posts in my main query on my front page.
function add_custom_post_type_to_query( $query ) { if ( $query->is_home() && $query->is_main_query() ) { $query->set( 'post_type', array('post', 'dmc_events') ); } } add_action( 'pre_get_posts', 'add_custom_post_type_to_query' );The problem I’m having now is that I want to display information about these posts on the front page.
I can get the title, thumbnail image, and author to display, but I can’t seem to get at any of the other information (like category title, or post type title).
Is there something I’m missing? I’ve setup the taxonomy for the post type and everything seems to work fine for adding categories and assigning categories. Do I need to do something extra in the code above to bring over more of the post type vars so I can access them in the query and display them?
The topic ‘Custom Post Type on front page’ is closed to new replies.