[Resolved] Best practices for handling custom post types and custom fields

Home Forums Support [Resolved] Best practices for handling custom post types and custom fields

Home Forums Support Best practices for handling custom post types and custom fields

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #191315
    Addison

    I’m finally about to use GeneratePress for a larger site that requires more customization (using CPT UI and Advanced Custom Fields) and I’ve been working on a way to incorporate the data output when I need to go beyond what GP does out-of-the-box. Here’s what I’ve come up with so far:

    In my child theme functions.php:

    
    add_filter( 'the_content', 'my_cpt_content' );
    function my_cpt_content( $content ) {
    	if ( is_singular( 'property' ) && is_main_query() ) {
    		include dirname( __FILE__ ) . '/partials/content-cpt-single.php';
    	} else {
    		return $content;
    	}
    }
    

    And in my partials/content-cpt-single.php:

    
    <?php defined('ABSPATH') or die('You cannot load this page directly.'); ?>
    
    <div class="text-description">
    	<?php echo $content; ?>
    </div>
    
    <ul>
    	<li><?php the_field( 'square_feet' ); ?> SF</li>
    	<li>$<?php the_field( 'price' ); ?></li>
    </ul>
    

    The idea was to keep my child theme templates to a minimum (or none, if possible) so that theme updates would be less likely to do harm. Are there any downsides to this method? Should I just suck it up and create custom archive and post templates?

    Thanks,
    Addison

    • This topic was modified 5 years, 4 months ago by Addison.
    #191364
    Tom
    Lead Developer
    Lead Developer

    Interesting method, I don’t see anything wrong with doing it that way, although I do try to stay away from filtering the_content.

    However, if it’s working and everything else is working as it should, I think you’re good to go πŸ™‚

    #191383
    Addison

    Thanks, Tom. It seems to work, so I’ll find out. πŸ˜‰ I found this article to help with keeping the customized output where I wanted it to be: https://pippinsplugins.com/playing-nice-with-the-content-filter/

    So you would use the archive-{custom}.php and single-{custom}.php for this sort of thing? I’m guessing filtering the_content could produce unexpected results?

    #191415
    Tom
    Lead Developer
    Lead Developer

    Using archive-{custom}.php etc.. is a little safer I think.

    I don’t like overwriting core theme files in child themes as those files sometimes change and then you miss out on the changes.

    However, adding new files like the above is always pretty safe.

    #191683
    Addison

    Gotcha. I was thinking that creating the additional template files would actually create more work, in that with every theme update I’d have to check my child theme templates to make sure they were up to speed. Using my child theme functions.php to “inject” my custom output directly into the parent’s theme templates seemed more DRY (Don’t Repeat Yourself).

    BUT, being that I’m hearing this directly from the theme developer (and someone far more experienced with WordPress), I’m going to take your advice. I’m assuming that I should copy the archive.php and single.php directly from GeneratePress to make my child theme templates… I appreciate your insight, Tom. Thanks!

    #191735
    Tom
    Lead Developer
    Lead Developer

    Well, in a way you’re right. Creating new files means you will have to stay on top of updates and see if I add anything new into those files you’re copying from and update them accordingly.

    If you can use your functions.php file to inject your edits without causing issues elsewhere, that would be a great solution.

    There’s really no right way to do it – it’s all preference. If you can make your DRY method work, then that’s awesome.

    If not, adding template files isn’t the end of the world πŸ™‚

    #191763
    Addison

    Yeah, I may try it and see what happens. I’m definitely still figuring things out… Thanks again!

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