Moving custom post type code from functions.php to cpt template?

Community Forums Forums Design Tips and Tricks Moving custom post type code from functions.php to cpt template?

This topic is: not resolved

This topic contains 3 replies, has 3 voices, and was last updated by  ameeker 7 months, 2 weeks ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #84775

    ameeker
    Participant
    Post count: 29

    I have code for displaying a custom post type currently in the functions.php file – I’m wondering if I can move it to a template for the single-(post-type).php instead (single-tipoftheday.php in this case). I will have several other CPT’s to format and think this will make it easier for the client to eventually modify these if they need to. Will those functions still work that way? Here is a paste of what I expect the template would be.

    http://pastie.org/8626678

    (Displaying the custom fields works in functions.php – I’m not necessarily asking for a review of THAT – but rather if it will continue to work as a template instead of in functions, and if this is a good practice).

    #84828

    cdils
    Participant
    Post count: 421

    Not sure if I understand correctly, but it’s too late to register the CPT by the time you’re in a template page.

    I’d pull all the CPTs out into a single file (call it my-theme-cpts.php or something equally obvious) and then include that file in functions.php. It’ll keep your code a little cleaner.

    Then you can still do your template files as is, except you won’t need the conditionals (i.e. no need to check if it’s a single page if you’re already in single-tipoftheday.php.

    I may have completely missed your question. If so, try me again. :)


    Have you been helped in this forum? Pay it forward and answer someone else’s question. I bet you’ll know the answer to at least one question. :)

    I host a weekly Genesis-focused podcast called Genesis Office Hours. I tweet @cdils.

    #84863

    Sridhar Katakam
    Participant
    Post count: 970

    My personal preference is to use a plugin for managing the CPTs.


    #84872

    ameeker
    Participant
    Post count: 29

    I already have the CPT’s defined (using the Custom Post Type UI plugin so the clients can more easily manage them in the future).

    @Carrie – so the rest of the paste, minus the conditional to check for the CPT, the it would work as it is in single-tipsoftheday.php ?

    I’ve got the entire part about displaying custom fields in the functions.php file now.

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.