Support » Themes and Templates » Applying templates to custom post types

  • I’m actually having two issues, but let’s first address the main one.

    I’ve created two templates for my two custom post types:
    Type A: Writings (Newspaper / Magazine Articles)
    Type B: Television (Media Appearances)

    They both have custom fields attached to them so the corresponding templates reflect this. However I can’t quite figure out how to apply those custom templates to the custom post types, not just to a page.

    Second problem; I have two links in my main nav: Writings & Television. I would like these to link to a page that will display of all posts under the corresponding post types.

    If anyone could help me find a solution to either of these problems it would be greatly appreciated 🙂

Viewing 11 replies - 1 through 11 (of 11 total)
  • I just found the solution to my first problem (applying templates to custom post types). Here is a link if anyone is interested:

    Still not sure how to bring up the list of custom post types though.
    I’m not sure if I can make a page (eg. Television) and get that Television page to bring up the list of Television posts?

    So I ended up solving my own problem. Again, if anyone is interested:

    I made two files, television.php and writing.php (these are not the template files I was referring to earlier).
    I also made two regular pages, one called writing which used writing.php as a template, and television respectively.

    Here’s my code for writing.php:

    [Code moderated as per the Forum Rules. The maximum number of lines of code that you can post in these forums is ten lines. Please use the pastebin]

    Important things to note:
    1. The template name in the comment at the top MUST be changed
    2. Assign ‘post_type’ => to your slug name for your custom post type.

    argh are you kidding wordpress! now i’ll never know the solution. thanks a lot.

    Sorry? Is there something I can help you with?

    Hi sdaponte

    Could you paste your code again please (I notice that the post is 9 months old so you may not have it still)

    I’m using a plugin that uses custom post types, and I’m trying to template it.

    I think Kevin was referring to the same thing – ie wordpress has removed the code as it was over 10 lines long.

    Thanks in advance 🙂

    Hey db9429!

    Yes, this was from an old project, but I can still try to help you if I can.

    For each custom post type you will need 2 templates – 1 (television.php) that will spit out the list of articles for that post type, and 1 (single-television.php) that will spit out the entire article. My custom post type was called ‘television’ for the client’s tv appearances.

    So for the television.php, right under the opening php tag, you have to include a comment that names the template according to the name of your custom post type, like so:

    Template Name: Television

    This will allow you to go into the page that you’ve created to list your articles and select the template name that you’ve just created. Then I did the following:

                                        <!--begin loop - select the post type by name-->
                                        query_posts( array( 'post_type' => 'television', order_by => 'meta_value&meta_key=date', order => 'desc') );
                                        if ( have_posts() ) : while ( have_posts() ) : the_post();
                                       <!--output the content-->
                                        <p><a href = '<?php the_permalink(); ?>'><h2><?php the_title(); ?></h2></a>
    				    <strong><?php echo(types_render_field("show", array("arg1"=>"val1","arg2"=>"val2"))); ?></strong><br />
    				    <?php echo(types_render_field("date", array("arg1"=>"val1","arg2"=>"val2"))); ?></p>
                                      <?php endwhile; endif; wp_reset_query(); ?>

    Now on the single-television.php, I did the following:

    <?php while ( have_posts() ) : the_post(); ?>
    					<!--Video Type-->
    					<h2>Field One <?php echo(types_render_field("fieldOne", array("arg1"=>"val1","arg2"=>"val2"))); ?></h2>
    					<strong>Field Two: </strong><?php echo(types_render_field("field-two", array("arg1"=>"val1","arg2"=>"val2"))); ?><br />
    					<!--Video Content-->
    					<?php get_template_part( 'content', 'single' ); ?>
    						// If comments are open or we have at least one comment, load up the comment template
    						if ( comments_open() || '0' != get_comments_number() )
    							comments_template( '', true );
    				<?php endwhile; // end of the loop. ?>

    Unfortunately I don’t remember how I linked the appropriate single template type to the corresponding post type. I don’t believe this was done in the same way with the comment at the top of the file. Might have to do some googling for this one.

    So in summary, you want to create a loop `<?php
    query_posts( array( ‘post_type’ => ‘television’, order_by => ‘meta_value&meta_key=date’, order => ‘desc’) );
    if ( have_posts() ) : while ( have_posts() ) : the_post();
    ?>that make sure you have posts to display and to reference the custom post type by name. If you want to output any fields, you use this line of code:<?php echo(types_render_field(“field-name”, array(“arg1″=>”val1″,”arg2″=>”val2”))); ?>` and just replace field-name with your field name.

    Hope this helps!

    Take a look at the link I posted in the second post re: that part I told you to Google.

    thanks for the snip…. is is possible that you used a conditional tag?

    Thanks sdaponte – I’ll take a look and work through it.

    Much appreciated 🙂

    Just wondering what the vals and args are for? And if they are needed for something simple like just a basic post template. I mean to say, on the line of code you have this

    <strong><?php echo(types_render_field("show", array("arg1"=>"val1","arg2"=>"val2"))); ?>

    and I am not sure if those vals and args are actually something you have specifically set up in your theme or do they just need to be there because wordpress requires all kinds of extra junk to make things work, lol. These are the parts that are always confusing for me.

    “or do they just need to be there because wordpress requires all kinds of extra junk to make things work”

    Yes, very much so. I’m not really sure what they’re doing either to be honest, but if you remove that array it doesn’t display anything.

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Applying templates to custom post types’ is closed to new replies.