Community Forums › Forums › Archived Forums › Design Tips and Tricks › CPTs and ACF – single- and archive-template
Tagged: ACF
- This topic has 10 replies, 4 voices, and was last updated 10 years, 7 months ago by
mmjaeger.
-
AuthorPosts
-
May 3, 2015 at 7:58 pm #150062
mmjaeger
MemberHello
I've a few custom post types with a few custom fields - what I discovered is that the content part of the single page and the archive page is basically identical therefore I've a lot of duplicated code in the two files.what's the best way to optimize this - I'm thinking about putting the content part into the functions file or maybe use template_part - hope somebody can shed some light please?
Thank you in advance for your help.
May 4, 2015 at 6:50 am #150087DTHkelly
MemberThis is a premium tutorial:
Single and Archive templates for Custom Post Type in Genesis
May 4, 2015 at 4:46 pm #150133mmjaeger
Memberanybody else has an idea?
May 4, 2015 at 6:38 pm #150140mmjaeger
Membercould this be a start? somehow I get the impression it's slowing down WordPress?!
add_action( 'pre_get_posts', 'ta_pre_get_posts' ); function ta_pre_get_posts( $query ) { if ( !is_admin() && $query->is_main_query() && in_array( $query->get( 'post_type' ), array( 'ta_case', 'ta_client', 'ta_event', 'ta_member', 'ta_opportuntiy', 'ta_position', 'ta_testimonial' ) ) ) { //* Remove the post content //remove_action( 'genesis_entry_content', 'genesis_do_post_content' ); //* Add custom post content //add_action( 'genesis_entry_content', 'ta_custom_entry_content' ); //* add_action( 'get_header', 'ta_get_template_part' ); } return; } function ta_get_template_part() { $post_type = get_query_var( 'post_type' ); get_template_part( 'content', $post_type ); }Somebody please help.
May 4, 2015 at 11:52 pm #150159coralseait
MemberUse something like this:
add_filter( 'template_include', 'csit_template_redirect' ); function csit_template_redirect( $template ) { if ( is_tax( 'my-custom-taxonomy' ) ) $template = get_query_template( 'archive-my-custom-post-type' ); return $template; }This basically detects a custom taxonomy type and sends single / archive to the archive's template. You could mod for your conditionals / situation.
May 5, 2015 at 5:59 am #150190mmjaeger
Memberactually, I don't have any taxonomies at all - maybe my question wasn't very clear.
May 5, 2015 at 7:04 am #150194coralseait
MemberMay 5, 2015 at 7:16 am #150196mmjaeger
MemberI understand but as I interpret it I would still end up with 7 cpt archive files and 7 cpt single files - what I'm trying to do is basically use the standard single/archive file and use some sort of template for the cpt output which is basically identical for single and archive.
May 6, 2015 at 10:23 am #150386mmjaeger
Memberbump
May 6, 2015 at 1:15 pm #150415Brad Dalton
ParticipantWordPress generates archives pages and single pages so need need to add any templates for single or archive when using CPT's.
The content is generated by the genesis function genesis();
May 6, 2015 at 1:43 pm #150448mmjaeger
MemberI'm using all custom fields so guess I have to have some sort of template - I like to optimize that code so I don't have that much duplicate code for single and archive pages
-
AuthorPosts
- The forum ‘Design Tips and Tricks’ is closed to new topics and replies.