• Hopefully this one won’t be too challenging. I want the Loop to do it’s normal thing (that I finally figured out) of giving only excerpts on the main page, categories, and archives, and when a single post is requested, it uses the single.php template, which looks different from the index.php, category.php, and archives.php. And of course, I want to make this more difficult.

    How would the Loop be formatted to add a condition that says:

    IF category 17, use single2.php and not single.php

    I have a category of pages that require massive amounts of unique CSS (trying to convert my popular css experiments to fit within WordPress) and I don’t want to load them up in the style.css and I really don’t want these to be in Pages because they I have to mess around with making sure that these pages show up on the categories and archives in a special way because they aren’t part of the Loop.

    If there is a better way to do this, I’m game. The style sheet for these experiments is huge.

Viewing 15 replies - 1 through 15 (of 28 total)
  • Ummm, you could do something like the following (I think this will work).

    -copy single.php to single1.php and single2.php. leave single1.php alone, modify the crap out of single2.php…
    -edit single.php so that all it basically does is:

    <?php
    $thisdir = dirname(__FILE__).'/';
    if ($post->post_category==17)
    include($thisdir.'single2.php');
    else
    include($thisdir.'single1.php');
    ?>

    I >think< that’ll do what you are looking for.
    -d

    Thread Starter Lorelle

    (@lorelle)

    I’m tired so I hope this makes sense. I put this in the single.php…where in the Loop?…. and it acts like a sorter that says “any link coming here, go to single1 unless you are category 17 then go to single2” and have nothing in single.php but this redirect thingy?

    Is that what this is? It’s a little more complicated (adding two more singles instead of just one) than I thought, but I just need it clarified.

    Thanks!

    You replace the content of single.php with that code. single.php would consist only of that code. It would have no loop. single1.php and single2.php would have a loop (although techinically single post templates don’t need a loop since there’s just a single post).

    Per post templates can easily be done with a plugin that hooks onto the ‘single_template’ filter. The plugin could lookup a key in the database like we do with the page templates or load singleX.php (where X is a post number) like we do with the category templates.

    Don’t think of these these template loading questions in terms of the loop. Templating decisions that affect an entire page should be made outside of the loop.

    P.S. post_category is deprecated now that we do multiple categories per post. You can use in_category(17) instead.

    Thread Starter Lorelle

    (@lorelle)

    Okay, I’ve been playing around with this, and the first one didn’t work so I am trying to add in_category(17) and having trouble. The codex says to put apostrophes around the category ID, but I’ve tried with and without. With double parenths per codex example and without. This is the last attempted version:

    <?php
    $thisdir = dirname(__FILE__).'/';
    if (($post->in_category('17')))
    include($thisdir.'single2.php');
    else
    include($thisdir.'single1.php');
    ?>

    <?php
    if ( in_category('17') ) {
    include(TEMPLATEPATH . '/single2.php');
    } else {
    include(TEMPLATEPATH . '/single1.php');
    }
    ?>

    http://codex.wordpress.org/Template_Tags/in_category
    http://codex.wordpress.org/Include_Tags#Including_Any_Template

    EDIT: I put in some curly braces for clarity.
    EDIT2: Arg – can’t follow my own docs – I think it will work now.

    Thread Starter Lorelle

    (@lorelle)

    How much clarity? Totally borked. errors in template-functions-category.php and in the single.php.

    Try it again? I just re-edited (I put in those slashes). Sorry.

    Thread Starter Lorelle

    (@lorelle)

    I grabbed each of your edits….Okay, it shows but here are the errors:


    Warning: Invalid argument supplied for foreach() in ...wp-includes/template-functions-category.php on line 400
    Warning: in_array(): Wrong datatype for second argument in ...wp-includes/template-functions-category.php on line 404

    Huh – ok let me go poke at something.

    Thread Starter Lorelle

    (@lorelle)

    My hero! You know I only ask the very most challenging of you.

    Ok: The problem. in_category() cannot be used outside the loop, which is where you want to use it.

    The solution: trick it.

    <?php
    $post = $wp_query->post;
    if ( in_category('17') ) {
    include(TEMPLATEPATH . '/single2.php');
    } else {
    include(TEMPLATEPATH . '/single1.php');
    }
    ?>

    It’s $post that in_category() needs to work. Normally, the loop takes care of $post, but since there’s no loop, we have to do it manually.

    Does that work?

    Thread Starter Lorelle

    (@lorelle)

    Michael, don’t give up on me, you brilliant one! It almost works.

    No more errors but every post returns single1.php and I can’t get single2.php to come up.

    I wrote diff text at the top to let me know which was which.

    Thread Starter Lorelle

    (@lorelle)

    FORGET WHAT I JUST SAID! The post I was checking SHOULD have been in cat 17 but it wasn’t. I had clicked on the wrong one. Works like a total charm. PUT this example in the Codex!!! It’s amazingly brilliant, like the man!

    Whew – you had me worried for a second there. Glad it works.

    Thread Starter Lorelle

    (@lorelle)

    Honest, I ain’t going to be the only crash test dummy on this. Get this code in the codex before it fades into forum dust. Brilliant trick!

    THANK YOU. I can go to bed now and sleep in peace. This has been annoying me for weeks and I just couldn’t figure out a solution on my own.

Viewing 15 replies - 1 through 15 (of 28 total)
  • The topic ‘Have a second single.php file for different single post look?’ is closed to new replies.