Avatar elsue asked

How to Setup Template Files for Custom Post Types

Hi all,

I thought I would share the steps for creating templates for custom post types. Hopefully I have everything here and it is clear enough.

Creating Templates for Custom Post Types Using Pippins Easy Custom Posts Types Plugin

I use Easy Custom Post Types to set up a Treatments cpt and a custom taxonomy Conditions.

First I want to create my templates.

  • In your WordPress Dashboard go to Content Types --> Settings

  • Select the templates you want to enable.
    Image

  • In your theme look for the templates created.
    Image

  • Each template contains:
    <?php
    /**

    // get warp
    $warp = Warp::getInstance();

    // load main template file, located in /layouts/template.php
    echo $warp['template']->render('template');

If you aren't using the plugin, just create your archive, single and/or taxonomy templates using the code above.

  • Now you need to create those templates in styles --> (style name) --> layouts.

I copied single.php and archive.php from warp --> systems --> wordpress --> layouts and put them into my styles --> (style name) --> layouts.

single-treatments.php has no changes

archive-treatments.php - the line query_posts calls my post type treatments.

<div id="system">   
<?php  
if ( get_query_var('paged') ) $paged = get_query_var('paged');    
if ( get_query_var('page') ) $paged = get_query_var('page');  
global $query_string;  
query_posts($query_string . '&post_type=treatments&posts_per_page=-10&paged=' . $paged);  
if (have_posts()) : ?>

There is no category.php file to copy so I created my own taxonomy-conditions.php. Under the div I call the term for my custom taxonomy and then I created a if/while loop to get the posts.

    <div id="system">   
<?php $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); ?>  

<h1 class="title"><?php printf(__("%s Treatments", "warp"),  '<span>' . $term->name . '</span>' ); ?></h1>  

<?php  


if ( have_posts() ) : while (have_posts()) : the_post();?>  


        <?php echo $this->render('_post'); ?>  


        <?php echo $this->render("_pagination", array("type"=>"posts")); ?>  

        <?php endwhile; ?>  

            <?php else : ?>  



            <h1 class="title"><?php printf(__("Sorry, but there aren't any posts in the %s category yet.", "warp"),  '<span>' . $term->name . '</span>' ); ?></h1>  


        <?php get_search_form(); ?>  


<?php endif; ?>  
</div>      


        <?php echo $this->render('_post'); ?>  


        <?php echo $this->render("_pagination", array("type"=>"posts")); ?>  

        <?php endwhile; ?>  

            <?php else : ?>  



            <h1 class="title"><?php printf(__("Sorry, but there aren't any posts in the %s category yet.", "warp"),  '<span>' . $term->name . '</span>' ); ?></h1>  


        <?php get_search_form(); ?>  


<?php endif; ?>  
</div>    
  • Next I added the templates to content.php. This is where I get the post type and the custom taxonomy.

    $content = '';  
    
    if (is_home()) {  
        $content = 'index';  
    } elseif (is_page()) {  
        $content = 'page';  
    } elseif (is_attachment()) {  
        $content = 'attachment';  
    }elseif ((is_single()) && (get_post_type() == 'treatments')) {  
        $content = 'single-treatments';  
    } elseif (is_single()) {  
        $content = 'single';  
    } elseif (is_search()) {  
        $content = 'search';  
    } elseif ((is_archive()) && is_post_type_archive( 'treatments')) {  
        $content = 'archive-treatments';      
    } elseif ((is_archive())  && get_taxonomy( 'conditions')) {  
        $content = 'taxonomy-conditions';     
    } elseif (is_archive() && is_author()) {  
        $content = 'author';  
    } elseif (is_archive()) {  
        $content = 'archive';  
    } elseif (is_404()) {  
        $content = '404';  
    }
    

I hope I have everything here. Next up will be how to use Widget Options with custom post type templates and taxonomies.

  • WordPress
  • Warp Theme

Edited

13 Answers

1

Avatar david.carroll answered

Hi Chris,

If you have a specific need or issue to overcome, it might be worth posting a new question to address your specific needs.

If you do, please post a link here to that question so we can be notified and so others can follow the discussion continued on the new thread.

In regards to your comment about implementing this with Nano 2:

The issue is that this theme doesnt seem to use single.php

Just to clarify, Nano 2 does use single.php. It might not be located in the yoo_nano2_wp/layouts/ directory. This directory is used to customize the default layout for single.php in the Warp Framework folders.

You should see that file in:

*yoo_nano2_wp/warp/systems/wordpress/layouts/single.php*

However, you never want to modify this file because it will get overwritten during a theme update. Instead, just copy single.php to the yoo_nano2_wp/layouts/ directory and apply your custom edits there. That will preserve your file and override the Warp Framework default layout.

Best Regards,

David Carroll

1

Avatar chris.kyriacou answered

Ok I now have the same issue as nikishna above. I've managed to get my custom post type "jobs" to use my custom template "single-jobs.php" by placing the template in the root folder but it looks like a basic word document with simple formatting, no theme showing.

Can you help resolve please? I have posted this question in the forum but no one is answering...

Regards

0

Avatar alex.kahl.40 answered

Hi Ellen,

your solution is great, but now i need something else and can`t figure this out.

I have a cpt called wines:
cpt:wines
taxonomy:character
term:openwines and redwine(as a child of openwines)

For the first i used it with a usual twentyeleven wp template, there was only a taxonomy-character.php inside (with a special html tag for testing the called template => <h3>charactertaxonomy</h3> :

If i called mysite/character/redwine
...then everything worked with my title <h3>charactertaxonomy</h3>

Then i created a taxonomy-character-redwine.php in the themes folder (with a spiecial html tag for testing the called template => <h3>charactertaxonomy-red</h3> :
If i called mysite/character/redwine
...then everything worked with my title <h3>charactertaxonomy-red</h3>

...and that`s exactly the feature i need...cause i have to create different layouts for different terms...i.e. redwine, whitewine, champagne...

i tried to do similar actions to warp as descriped by your tutorial

1.created taxonomy-character-redwine.php in the root with the warp instance call
2.created taxonomy-character-redwine.php in the layout folder with your taxonomy-conditions.php code
3.extended the content.php...i think did some errors here...?? as i think, that taxonomy-character-redwine.php would call an archive...i don`t know if i am right.
here my code:

} elseif ((is_archive())  &&  get_taxonomy( 'character-redwine')) {    
    $content = 'taxonomy-character-redwine';  

i tried it too with :

} elseif ((is_archive())  &&  get_term( 'character-redwine')) {    
    $content = 'taxonomy-character-redwine';  

but it didn`t work

usually i have german expressions, but i wanted to take an example in english for better understanding

what do i do wrong? any help from you would be appreciated^^

best regards alex

btw...it might be, that your code in the taxonomy-conditions.php that you posted here has some errors...as the last few lines after the closing </div> tag are repeated from above

Edited

0

Avatar alex.kahl.40 answered

Hi,

i use the ecpt plugin from pippin...it`s really great, but i had to go through a lot of troubleshooting to get it work at warp. Now i figured out a solution for my needs, maybe not the best way to reach the aim, but it works very well for me this way.
So i try to explain what i have done in order to get some different ecpt layouts at warp...might be useful for anybody with similar issues.
First of all, a lot of thanks to ellen, she made it possible with her solution some posts above.^^

Now i will tell in presence about my needs:

ecpt :wines
taxonomy:character
terms: redwine, whitewine,champagne.....

I need different layouts for my terms(redwine, whitewine,...)... usually it works with taxonomy-{taxonomy}-{term}.php files...but only with an usual wp theme...so what to do?
for the first follow ellen in her great tutorial...then you are able to call your terms by your termlinks...that will only work for the taxonomy links without a special css format
btw...taxonomy-{taxonomy}-{term}.php doesnt work this way...but it doesnt matter...there is a better solution...

the first step then, is to create a mu-plugins folder inside of wp-content...then create a php file called i.e. wines.php in this folder...that`s the file where your all functions will be after all...i prefer my own files instead of blowing up the functions.php
inside there you can hook all your layouts by functions...(btw...that is much better solution than creating several taxonomy-{taxonomy}-{term}.php files ...all of them twice...(root...instance for calling warp and in the layout folder...after all, adapting content.php and the widgetoptions..to much work...ellen is the great one^^)

<?php  
/**  
 * Created by JetBrains PhpStorm.  
 * User: slotty  
 * Date: 26.04.12  
 * Time: 14:08  
 * To change this template use File | Settings | File Templates.  
 */  

// Rotwein offen  

function funcRedwine()  
{  
    if (have_posts()) : ?>  
    <?php while (have_posts()) : the_post(); ?>  
        <div class="entry">  
            <h2 class="title"><?php the_title(); ?></h2>  
            <?php echo get_post_meta(get_the_ID(), 'ecpt_weinname', true);  
            echo get_post_meta(get_the_ID(), 'ecpt_weinbeschreibung', true);  
            echo get_post_meta(get_the_ID(), 'ecpt_preis1', true);?>  
        </div>  
        <?php endwhile; ?>  
    <!-- show pagination here -->  
    <?php else : ?>  
    <!-- show 404 error here -->  
    <?php endif; ?>  

<?php  
}  

add_filter('content', 'funcRedwine');  

?>

after that you will have a hook file with all your functions for that ecpt( in our case wines)
now the only thing you have to do , is to create an if/ifelse statement for the different cases( you may also use the php case possibility if you want)

<div id="system">  

    <?php $term = get_term_by('slug', get_query_var('term'), get_query_var('taxonomy')); ?>  

    <h1 class="title"><?php printf(__("%skarte", "warp"), '<span>' . $term->name . '</span>'); ?></h1>  

    <?php  
    if ($term->name = "Redwine") {  
    funcRedwine();  
}  
    elseif ($term->name = "Whitewine") {  
        funcWhitewine();  
    }  

    ?>  

</div>  

now you have a well structured mu-plugins folder with your files(i.e. wines, food, events......) and a taxonomy file with less code inside(will look like content.php and better)
i use phpstorm, there you have a nice overview over your functions in the structureviewv(in this case then your layouts)...but aptana studio and netbeans will do it as well^^...they are free)

notice!!! $post->ID won`t work...use get_the_ID() instead of $post

hope this helps with pippins ecpt^^
cheers
slotty

0

Avatar javier.jimenez.90 answered

Hello everybody!

I think this is the way to resolve the problem that some users set in the support forum:

How can we assign style to the custom content type of "The events calendar pro" plugin. http://www.yootheme.com/support/question/8658

The ECP Support team submit me to this forum.

I have tried this but I can´t resolve the problem... please... How can I assign style to events calendar pro?!?! We need HELP!

Thanks.

0

Avatar nikishna answered

@elsue, thanks for your GREAT posts! The Widget & Taxonomy inclusion worked from part II of your tutorial, yet I could not get the Inspire theme to use my custom post template unless it was at the root of the theme folder. And when I did that, it stripped out all of them page formatting, leaving me with the right CONTENT, yet not THEME!

I tried putting my single-member_profiles.php into all of the other layout folders within WARP and in the style folders, yet to no avail : ( Do you think that it is a content.php issue, wordpress issue, yootheme warp issues...?

This should be a pretty standard thing to do, yet I cannot seem to get it to work properly and I have a deadline to finish this project by the end of the week..! Any help would be greatly appreciated; thanks and take care...

0

Avatar chris.kyriacou answered

Hi,

I would like to achieve the same using Nano 2 theme. The issue is that this theme doesnt seem to use single.php :-S

Any Ideas?

0

Avatar david.henry answered

Hi Ellen,

Do you know how to have the archive templates appear in Widget Options just like the single templates?

David

0

Avatar joon2008 answered

does anybody know how to tweak _post.php??

Know someone who can answer? Share a link to this question via email or twitter.