WordPress.org

Ready to get started?Download WordPress

Forums

Have a second single.php file for different single post look? (29 posts)

  1. Lorelle
    Member
    Posted 9 years ago #

    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.

  2. davidchait
    Member
    Posted 9 years ago #

    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

  3. Lorelle
    Member
    Posted 9 years ago #

    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!

  4. Ryan Boren
    WordPress Dev
    Posted 9 years ago #

    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.

  5. Lorelle
    Member
    Posted 9 years ago #

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

  6. Michael Adams (mdawaffe)
    Member
    Posted 9 years ago #

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

  7. Lorelle
    Member
    Posted 9 years ago #

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

  8. Michael Adams (mdawaffe)
    Member
    Posted 9 years ago #

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

  9. Lorelle
    Member
    Posted 9 years ago #

    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

  10. Michael Adams (mdawaffe)
    Member
    Posted 9 years ago #

    Huh - ok let me go poke at something.

  11. Lorelle
    Member
    Posted 9 years ago #

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

  12. Michael Adams (mdawaffe)
    Member
    Posted 9 years ago #

    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?

  13. Lorelle
    Member
    Posted 9 years ago #

    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.

  14. Lorelle
    Member
    Posted 9 years ago #

    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!

  15. Michael Adams (mdawaffe)
    Member
    Posted 9 years ago #

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

  16. Lorelle
    Member
    Posted 9 years ago #

    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.

  17. Ryan Boren
    WordPress Dev
    Posted 9 years ago #

    I created a Custom post template plugin to make this easier.

  18. Lorelle
    Member
    Posted 9 years ago #

    So it's customizable by post ID and not just category. That's really customization. I love it. Most brilliant.

    Thanks!

  19. nden
    Member
    Posted 9 years ago #

    OK. I think this is a good idea.

    So, tell me steps by steps how Lorelle figured it out. Would you? I am confusing all the sayings. Thanks

  20. Michael Adams (mdawaffe)
    Member
    Posted 9 years ago #

    Just Use ryan's plugin referenced above.

  21. Ryan Boren
    WordPress Dev
    Posted 9 years ago #

    Someone's also working on a Post Templates Plugin that provides post templates in the same fashion that core WP provides page templates.

  22. Lorelle
    Member
    Posted 9 years ago #

    nden, did you figure it out?

  23. neoflox
    Member
    Posted 9 years ago #

    Hello! I am very glad having found this helpful thread!

    I had the same problem as Lorelle had and follow this here curiously. The way of putting this "template switch" in the single.php manually works very well. But unfortunately I can't bring ryan's plugin to work.

    I get this error message 4 times and the custom templates don't show up after:
    Warning: Invalid argument supplied for foreach() in /home/www/htdocs/neoflox.net/page/wp-includes/template-functions-category.php on line 410

    Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/www/htdocs/neoflox.net/page/wp-includes/template-functions-category.php on line 414

    I have the right files in the template directory and I believe I configered the plugin properly:

    if ( in_category('1') && file_exists(TEMPLATEPATH . '/single-1.php') )
    return TEMPLATEPATH . '/single-1.php';
    if ( in_category('2') && file_exists(TEMPLATEPATH . '/single-2.php') )
    return TEMPLATEPATH . '/single-2.php';

    PLEASE be so kind and help me out you wonderful people! :P

  24. Lorelle
    Member
    Posted 9 years ago #

    I got this to work without the plugin. The code ended up being like this inside of a file called single.php with NOTHING else in that file:

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

    I then created single1.php and a single2.php with different style sheets and headers and even sidebars, since I can put whatever I wanted in each one. This is brilliant and no stress at all. No plugin no nothing.

    I'm sure you can make a series of these in the single.php that asks if X then single4.php or single6.php and so on.

    I hope this helps.

  25. oriecat
    Member
    Posted 9 years ago #

    Oh this is a great thread. I am helping a friend set up her blog so that one category is totally different, and we figured that out, but I hadn't yet thought about how they would appear when viewed as single. Looks all too easy now. :)

  26. Kafkaesqui

    Posted 9 years ago #

    Just to complicate the issue, I also put together a plugin for this:

    http://wordpress.org/support/topic.php?id=27821#post-156289

  27. neoflox
    Member
    Posted 9 years ago #

    Well, Lorelle, thanks, but that was the way it always worked for me ;) Now I asked how to make the plugin work ;)

  28. soyuz
    Member
    Posted 8 years ago #

    hi, i'm new in WP. I've installed WP and works fine. My blog has two authors, and when a single page is requested, I want the loop to action: if the author is admin, use single1.php and if the author is Tedy, use single2.php. So I have similar problem except I want to sort it by author. I tried to change the above code into:

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

    It didn't work. It always shows the single1.php

    Any suggestion?

    Thanks.

  29. thepete
    Member
    Posted 8 years ago #

    I'd love to use Ryan's plugin but instead of customizing the template by category and post_id I'd like to use it for author_id. Is that possible? I've tried noodling around with it, but I'm afraid I just don't know enough. Any help would be appreciated. Thanks!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.