[Resolved] Add CPT to Accept Theme Blog/Archive Layout

Home Forums Support Add CPT to Accept Theme Blog/Archive Layout

  • This topic has 14 replies, 3 voices, and was last updated 11 months ago by Tom.
Viewing 15 posts - 1 through 15 (of 15 total)
  • Author
    Posts
  • #1036401
    Heather

    Hi,

    I’ve created a custom post type for events within the theme’s functions.php, and the new post type shows up in my dashboard and I can create and view posts, however, it’s archive page does not accept the layout I input from Appearance > Customize > Layout > Blog.

    It also seems that, even though all my categories show up to be added to these posts, it does not register them – ie: I add a category of “cool” to my event post, but if i go to /blog/category/cool where all the posts with that category exist, it does not show up. Same thing with tags.

    Is there a way to fix this?

    #1036672
    Heather

    More specifically, it seems like my archive page is not being wrapped in the “generate-columns-container” class that exists after the <main>. Is there a way for me to add this? See attached

    #1036777
    David
    Staff
    Customer Support

    Hi there,

    can you link me to the site so i can take a look?

    You can edit your original topic and use the Site URL field to share it privately.

    #1037490
    Heather

    Unfortunately I am working off of local and doing work for a client so I have no way of linking. Here is my code in my functions.php however:

    //Custom post type - event
    
    	// register custom post type to work with
    function lc_create_post_type() {
    	// set up labels
    	$labels = array (
    	'name' => 'Events',
    	'singular_name' => 'Event',
    	'add_new' => 'Add New Event',
    	'add_new_item' => 'Add New Event',
    	'edit_item' => 'Edit Event',
    	'new_item' => 'New Event',
    	'all_items' => 'All Events',
    	'view_item' => 'View Event',
    	'search_items' => 'Search Events',
    	'not_found' => 'No Events Found',
    	'not_found_in_trash' => 'No Events found in Trash',
    	'parent_item_colon' => '',
    	'menu_name' => 'Events',
    	);
    	//register post type
    	register_post_type ( 'event', array(
    	'labels' => $labels,
    	'has_archive' => 'blog/events',
    	'public' => true,
    	'supports' => array( 'title', 'editor', 'excerpt', 'custom-fields', 'thumbnail','page-attributes' ),
    	'taxonomies' => array( 'post_tag', 'category' ),
    	'exclude_from_search' => false,
    	'capability_type' => 'post',
    	'rewrite' => true,
    	)
    	);
    	}
    	add_action( 'init', 'lc_create_post_type' );
    
    #1037730
    Tom
    Lead Developer
    Lead Developer
    #1040686
    Heather

    Hi,

    This worked great! Thank you!

    Im now having a couple of different issues –

    1. I would like the start and end time of the event to be displayed on the archive page/be able to isolate that information via a query or shortcode. I noticed it is not being displayed as part of the post; do I need to update something in the single.php and archive.php? see attached for photo

    2. I noticed although my categories come up in the post to be selected, but when I go to the archive page for that category, the event post does not show up. Is there additional code I need to add to my functions.php?
    see my updated code:

    //Custom post type - event
    
    // register custom post type to work with
    function lc_create_post_type() {
    	// set up labels
    	$labels = array (
    	'name' => 'Events',
    	'singular_name' => 'Event',
    	'add_new' => 'Add New Event',
    	'add_new_item' => 'Add New Event',
    	'edit_item' => 'Edit Event',
    	'new_item' => 'New Event',
    	'all_items' => 'All Events',
    	'view_item' => 'View Event',
    	'search_items' => 'Search Events',
    	'not_found' => 'No Events Found',
    	'not_found_in_trash' => 'No Events found in Trash',
    	'parent_item_colon' => '',
    	'menu_name' => 'Events',
    	);
    	//register post type
    	register_post_type ( 'event', array(
    	'labels' => $labels,
    	'has_archive' => true,
    	'public' => true,
    	'supports' => array( 'title', 'editor', 'excerpt', 'custom-fields', 'thumbnail','page-attributes' ),
    	'taxonomies' => array( 'post_tag', 'category' ),
    	'exclude_from_search' => false,
    	'capability_type' => 'post',
    	'rewrite' => array( 'slug' => 'events' ),
    	)
    	);
    	}
    	add_action( 'init', 'lc_create_post_type' );
    
    	add_filter( 'generate_blog_columns','tu_portfolio_columns' );
    	function tu_portfolio_columns( $columns ) {
    		if ( is_post_type_archive( 'event' ) ) {
    			return true;
    		}
    	
    		return $columns;
    	}
    
    	add_filter( 'generate_blog_get_column_count','tu_search_column_count' );
    	function tu_search_column_count( $count ) {
    		if ( is_search() ) {
    			return 33;
    		}
    	
    		return $count;
    	}
    #1040800
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    1. Where do you want your custom field to show up, exactly? You’ll likely need to hook it into the post using our Hook Elements: https://docs.generatepress.com/article/hooks-element-overview/

    2. Just so I understand right, you’re adding regular categories to our custom post type, but the posts aren’t showing up in those category archives? Maybe this will help?: https://wordpress.stackexchange.com/questions/251939/posts-from-custom-post-type-not-shown-in-category-archive

    #1041713
    Heather

    Is there no way to add code to one of the .php files so that it shows up everywhere? I want it to show up under the event post title.

    #1041833
    Tom
    Lead Developer
    Lead Developer

    You could try the after_entry_title hook in a Hook Element: https://docs.generatepress.com/article/hooks-element-overview/

    Then just apply it to your events/archives in the Display Rules.

    #1042688
    Heather

    I was able to get the shortcode to show up with that hook, so that will do for now.

    For the categories, unfortunately the info on that link did not work. But yes, you have the right idea – The events page comes with it’s own categories I think, but I have a set of 7 cats that also show up when making an event post that I can select. But, even though these categories are the same, the event posts do not show up in the selected categories.

    What I am trying to do is display event information in a shortcode based on a category and tag, so the tag is always supposed to be ‘event’, and then a specific post will come up based on the category filter, so [display_posts=”1″ category=”happy” tag=”event”]. But that happy category that already exists does not register for any cpt.

    #1042690
    Heather

    Or, given the code below, if I could isolate the custom post type, it probably wouldn’t matter because then I can base the short code pull off the category and/or tag on the custom post type.

    ie: [display_posts=”1″ post_type=”events” category=”happy”]

    or something like that

    function lc_create_post_type() {
    	// set up labels
    	$labels = array (
    	'name' => 'Events',
    	'singular_name' => 'Event',
    	'add_new' => 'Add New Event',
    	'add_new_item' => 'Add New Event',
    	'edit_item' => 'Edit Event',
    	'new_item' => 'New Event',
    	'all_items' => 'All Events',
    	'view_item' => 'View Event',
    	'search_items' => 'Search Events',
    	'not_found' => 'No Events Found',
    	'not_found_in_trash' => 'No Events found in Trash',
    	'parent_item_colon' => '',
    	'menu_name' => 'Events',
    	);
    	//register post type
    	register_post_type ( 'event', array(
    	'labels' => $labels,
    	'has_archive' => true,
    	'public' => true,
    	'supports' => array( 'title', 'editor', 'excerpt', 'custom-fields', 'thumbnail','page-attributes' ),
    	'taxonomies' => array( 'post_tag', 'category' ),
    	'exclude_from_search' => false,
    	'capability_type' => 'post',
    	'rewrite' => array( 'slug' => 'events' ),
    	)
    	);
    	}
    	add_action( 'init', 'lc_create_post_type' );
    
    	add_shortcode('field', 'shortcode_field');
     
    function shortcode_field($atts){
         extract(shortcode_atts(array(
                      'post_id' => NULL,
                   ), $atts));
      if(!isset($atts[0])) return;
           $field = esc_attr($atts[0]);
           global $post;
           $post_id = (NULL === $post_id) ? $post->ID : $post_id;
           return get_post_meta($post_id, $field, true);
    }
    
    	add_filter( 'generate_blog_columns','tu_portfolio_columns' );
    	function tu_portfolio_columns( $columns ) {
    		if ( is_post_type_archive( 'event' ) ) {
    			return true;
    		}
    	
    		return $columns;
    	}
    
    	add_filter( 'generate_blog_get_column_count','tu_search_column_count' );
    	function tu_search_column_count( $count ) {
    		if ( is_search() ) {
    			return 33;
    		}
    	
    		return $count;
    	}
    #1042725
    Heather

    Nevermind, post_type is already able to be used within display-posts, and I can isolate the category selected of the cpt as well.

    #1042931
    Tom
    Lead Developer
    Lead Developer

    Glad you got that sorted – is there anything else you’re stuck on? 🙂

    #1044389
    Heather

    I think I am ok for now, I will mark this as resolved and if something else comes up along the same vein I will create another specialized ticket.

    Thanks for the help.

    #1044489
    Tom
    Lead Developer
    Lead Developer
Viewing 15 posts - 1 through 15 (of 15 total)
  • You must be logged in to reply to this topic.