• Resolved afdownload

    (@afdownload)


    i have a blog with 2 categories, 1- movie 2-music
    for each of their post i have many custom files added.

    buy i want to be able to create a single.php file for each of my category like this single-movie.php single-music.php so that when a user goes to the post see’s the custom filed related to the category.
    for example the custom files DIRECTOR should not appear in a music post and viceversa a SINGER custom filed value should not appear in a movie post. as a had a look to the hierarchical template for wordpress 3 i could not find a way for it. just like the seperate template whic i can make for my category i want the single template to be seperated file too.

    or maybe if it’s not possible to do so i want the custom field to be like on and off. for example if i have a music post i let the DIRECTOR custom field empty, so there should be a way that the custom file DIRECTOR does not show up in the posts that the value is not entered.

    i hope somebody find me a way to have both my category and single template files seperated !!!

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    Thread Starter afdownload

    (@afdownload)

    dear guys i found a way to create single templates for a specific category, you can add this code to the “functions.php” file of your template

    <?php
    
    /**
    * Define a constant path to our single template folder
    */
    define(SINGLE_PATH, TEMPLATEPATH . '');
    
    /**
    * Filter the single_template with our custom function
    */
    add_filter('single_template', 'my_single_template');
    
    /**
    * Single template function which will choose our template
    */
    function my_single_template($single) {
    	global $wp_query, $post;
    	/**
    	* Checks for single template by ID
    	*/
    	if(file_exists(SINGLE_PATH . '/single-' . $post->ID . '.php'))
    		return SINGLE_PATH . '/single-' . $post->ID . '.php';
    	/**
    	* Checks for single template by category
    	* Check by category slug and ID
    	*/
    	foreach((array)get_the_category() as $cat) :
    
    		if(file_exists(SINGLE_PATH . '/single-cat-' . $cat->slug . '.php'))
    			return SINGLE_PATH . '/single-cat-' . $cat->slug . '.php';
    
    		elseif(file_exists(SINGLE_PATH . '/single-cat-' . $cat->term_id . '.php'))
    			return SINGLE_PATH . '/single-cat-' . $cat->term_id . '.php';
    
    	endforeach;
    	/**
    	* Checks for default single post files within the single folder
    	*/
    	if(file_exists(SINGLE_PATH . '/single.php'))
    		return SINGLE_PATH . '/single.php';
    
    	elseif(file_exists(SINGLE_PATH . '/default.php'))
    		return SINGLE_PATH . '/default.php';
    	return $single;
    
    }
    ?>

    now you can define single templates for your categories, for example if you have category named video and you want the single post to be displayed in a different way than the normal single file you create a file named “single-cat-video.php”, that’s just it!!!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘creating a single template file for a category?’ is closed to new replies.