WordPress.org

Ready to get started?Download WordPress

Forums

setting a query_var to another template file (5 posts)

  1. da_silva
    Member
    Posted 7 years ago #

    i would like to have the following situation: some posts(that are assigned to a certain category) to show up in single.php when in single post view and the others i want them to show in index.php.

    So i need to define/change what template wordpress is using dinamically. I guess that some query variable holds the name of the template file it should use if a certain condition is met. Example: some query variable is set to single.php for posts in single view if single.php is detected in the theme and to index.php if there's no single.php.

    I want to know what variable is that and set it to a different value (file name) for a certain condition but i don't know if my info is right and if that's even possible.

    I would like all posts from category "no sidebar custom header" to be displayed by single.php and all the rest by index.php.

    Can i do that?

  2. KatGirl
    Member
    Posted 7 years ago #

    Yes.

    Copy single.php and store on your desktop for safe keeping.

    Copy single.php AGAIN (from desktop) and rename it single1.php or whatever you want. Upload to server (yourthemename). Go to Theme editor and open single.php and delete everything inside it.

    Put this inside it instead.
    <?php
    $post = $wp_query->post;
    if ( in_category('1') ) {
    include(TEMPLATEPATH . '/single1.php');
    } else {
    include (TEMPLATEPATH . '/index.php'); ?
    >

    where category 1 is the posts you want to use single1.php template. You can add to that single.php as your site grows, delegating what categories use what templates as you see fit.

  3. da_silva
    Member
    Posted 7 years ago #

    yes this would have been an easy solution but i am trying to get the internals of wordpress and the thing with redirection was vey interesting, i'll wait for an answer concerning this .

    thanks anyway:)

  4. Kafkaesqui

    Posted 7 years ago #

    If you're interested in the 'internals' for this, you might want to look over the source of a couple of plugins by Ryan Boren and myself:

    http://boren.nu/archives/2005/03/13/custom-post-templates-plugin/
    http://guff.szub.net/2005/07/21/post-templates-by-category/

  5. da_silva
    Member
    Posted 7 years ago #

    that would be really nice and i will, but is it so complicated that i need to read a plug-in? i thought it was a couple of lines of code...:-/

    ...oh, yhey are a couple of lines of code :D, but what about the redirection thing, all i read i didn't understand very well...i would need an example, can u provide one?

Topic Closed

This topic has been closed to new replies.

About this Topic