• Skip to main content
  • Skip to forum navigation

StudioPress

  • Shop for Themes
  • My StudioPress

Forum navigation

  • Home
  • General Genesis Discussions
  • StudioPress Themes
  • Genesis Blocks
    • Genesis Blocks
    • Genesis Custom Blocks
  • Retired Themes
  • FAQs
  • Forum Rules
  • Internationalization and Translations
  • Forum Bugs and Suggestions
  • Forum Log In

Are You Using The WordPress Block Editor?

Genesis now offers plugins that help you build better sites faster with the WordPress block editor (Gutenberg). Try the feature-rich free versions of each plugin for yourself!

Genesis Blocks Genesis Custom Blocks

CPTs and ACF – single- and archive-template

Welcome!

These forums are for general discussion on WordPress and Genesis. Official support for StudioPress themes is offered exclusively at My StudioPress. Responses in this forum are not guaranteed. Please note that this forum will require a new username, separate from the one used for My.StudioPress.

Log In
Register Lost Password

Community Forums › Forums › Archived Forums › Design Tips and Tricks › CPTs and ACF – single- and archive-template

This topic is: not resolved

Tagged: ACF

  • This topic has 10 replies, 4 voices, and was last updated 7 years, 9 months ago by mmjaeger.
Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • May 3, 2015 at 7:58 pm #150062
    mmjaeger
    Member

    Hello
    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 #150087
    DTHkelly
    Member

    This is a premium tutorial:
    https://sridharkatakam.com/single-archive-templates-custom-post-type-genesis/

    May 4, 2015 at 4:46 pm #150133
    mmjaeger
    Member

    anybody else has an idea?

    May 4, 2015 at 6:38 pm #150140
    mmjaeger
    Member

    could 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 #150159
    coralseait
    Member

    Use 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.


    Coral Sea IT

    May 5, 2015 at 5:59 am #150190
    mmjaeger
    Member

    actually, I don't have any taxonomies at all - maybe my question wasn't very clear.

    May 5, 2015 at 7:04 am #150194
    coralseait
    Member

    That's no worries, just change the conditional check for your CPT type.


    Coral Sea IT

    May 5, 2015 at 7:16 am #150196
    mmjaeger
    Member

    I 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 #150386
    mmjaeger
    Member

    bump

    May 6, 2015 at 1:15 pm #150415
    Brad Dalton
    Participant

    WordPress 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();


    Get Help – Book Consultation.

    May 6, 2015 at 1:43 pm #150448
    mmjaeger
    Member

    I'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

  • Author
    Posts
Viewing 11 posts - 1 through 11 (of 11 total)
  • The forum ‘Design Tips and Tricks’ is closed to new topics and replies.

CTA

Ready to get started? Create a site or shop for themes.

Create a site with WP EngineShop for Themes

Footer

StudioPress

© 2023 WPEngine, Inc.

Products
  • Create a Site with WP Engine
  • Shop for Themes
  • Theme Features
  • Get Started
  • Showcase
Company
  • Brand Assets
  • Terms of Service
  • Accptable Usse Policy
  • Privacy Policy
  • Refund Policy
  • Contact Us
Community
  • Find Developers
  • Forums
  • Facebook Group
  • #GenesisWP
  • Showcase
Resources
  • StudioPress Blog
  • Help & Documentation
  • FAQs
  • Code Snippets
  • Affiliates
Connect
  • StudioPress Live
  • StudioPress FM
  • Facebook
  • Twitter
  • Dribbble