David Tunnell
David Tunnell

Reputation: 7542

Why does a wordpress know to use a specific template and how do I customize this?

Hello and thanks for reading. I am a .NET developer and don't know PHP (Although I am working on learning on the job) and what I am working on was made my consultants that we dont have contact with anymore. When a news post is clicked it appears to display using a template single.php. Followed is the code for this page:

        <div id="marketBanner">
        <div id="banner"> 
            <img src="<?php bloginfo('stylesheet_directory'); ?>/images/services-banner.jpg" alt="" />              
        </div>
    <div id="breadcrumbWrap">
        <div id="breadcrumbs">
            <?php
            if(function_exists('bcn_display'))
            {
                bcn_display();
            }
            ?>
        </div>
    </div>
    </div>
    <div id="content">
        <div class="left">  
        <h2><?php the_title(); ?></h2><br />

            <?php if (have_posts()) : while (have_posts()) : the_post(); ?> 
                <div class="the_details">
                    Posted: <?php the_date(); ?> &nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;<a href="<?php bloginfo('url'); ?>/category/recent-news/">View All News</a>
                </div>
                <?php the_content(''); ?>
            <?php endwhile; endif; ?>

        </div>      

Why does this page get used when a post is chosen? I want only a certain category of post to use this page and another category of post to use a different template. How do I achieve this?

Upvotes: 1

Views: 92

Answers (2)

Matt Chepeleff
Matt Chepeleff

Reputation: 419

Take a look at the Wordpress Template Hierarchy. It explains which templates get used and will probably be super helpful if you're just starting out with WP. You can use WP syntax to create category pages - but at the archive level - not the single post level.

To create separate pages based on post categories see here and below:

<?php
$post = $wp_query->post;
if (in_category(9)) {
    include (TEMPLATEPATH.'/single-specific9.php');
    return;
}
if (in_category(8)) {
    include (TEMPLATEPATH.'/single-specific8.php');
    return;
}
if (in_category(11)) {
    include (TEMPLATEPATH.'/single-specific11.php');
    return;
}

Upvotes: 0

Matt The Ninja
Matt The Ninja

Reputation: 2731

You need to study the Wordpress Template Hierarchy @ https://codex.wordpress.org/Template_Hierarchy#Single_Post_display

A template (PHP file) is looked for in this order (only on wordpress).

 1 - single-{post_type}.php - If the post type were product, WordPress would look for single-   

 2 - product.php

 3 - single.php

 4 - index.php

In your case I would use single.php to create a template for the average post then specifically create a template for the one you want different using single-'post_type'.php

You can change the post type when creating a post.

Upvotes: 1

Related Questions