WordPress.org

Ready to get started?Download WordPress

Forums

Applying templates to custom post types (12 posts)

  1. sdaponte
    Member
    Posted 2 years ago #

    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 :)

  2. sdaponte
    Member
    Posted 2 years ago #

    I just found the solution to my first problem (applying templates to custom post types). Here is a link if anyone is interested:
    http://wordpress.org/support/topic/custom-templates-with-custom-post-type?replies=3#post-2850196

    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?

  3. sdaponte
    Member
    Posted 2 years ago #

    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.

  4. Kevin Kwok
    Member
    Posted 1 year ago #

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

  5. sdaponte
    Member
    Posted 1 year ago #

    Sorry? Is there something I can help you with?

  6. db9429
    Member
    Posted 1 year ago #

    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 :)

  7. sdaponte
    Member
    Posted 1 year ago #

    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:

    <?php
                                        <!--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' ); ?>
    
    					<?php
    						// 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!

  8. sdaponte
    Member
    Posted 1 year ago #

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

  9. nasca
    Member
    Posted 1 year ago #

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

    http://codex.wordpress.org/Conditional_Tags

  10. db9429
    Member
    Posted 1 year ago #

    Thanks sdaponte - I'll take a look and work through it.

    Much appreciated :)

  11. Keyboard_Headaches
    Member
    Posted 1 year ago #

    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.

  12. sdaponte
    Member
    Posted 1 year ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic