Support » Themes and Templates » Display by Category home.php template

  • Along with my plugin work I’ve been spending a bit of time on template experiments, proof of concepts in organizing content differently in WordPress 1.5.

    A method that’s requested often, and one that caught my interest, is to display recent posts organized within their respective categories, instead of just by date (descending). I’ve come up with one solution through a (very) modified post loop in a home.php template, and would like feedback on what I have so far, such as improvements to structure, streamlining code, etc.

    download template | view template | demo

    The modifications to The Loop I’ve made still only handle the last N posts, but it first retrieves the categories they’ve been posted to, and then organizes posts under each. By default categories are organized by latest post first, but uncomment the following line (remove the //) to sort/list the categories alphabetically:

    // uasort($cats, strcasecmp);

    I’ve commented throughout the template, but please ask if something isn’t clear.

    Finally, the template is designed around the default (Kubrick) theme, so it’ll work best with that; however the loop framework can be imported into any layout (with appropriate tweaking).

Viewing 6 replies - 46 through 51 (of 51 total)
  • Ah ok, I will have to look into it. I had initially asked about such a way to do it in this thread, and was directed here. The front page will have only 3 categories shown in a specific order. So I thought using some type of conditionals in the loop would help, but I am not quite sure exactly what code to use, and how exactly to format it.

    The goal behind my template here was to provide a solution to a very specific request in how to display blog posts on the home page, and that was by gathering current posts from a typical post loop and then listing them by category. There are ways to modify it to display only certain categories and whatnot (i.e. query_posts()), but in regards to your request, assuming I understand it fully, it lacks in a couple areas, primarily in setting up ‘static’ category sections on a page.

    If you have WP set to display the lastest 5 posts on the main (home) page, the template calls those 5 posts as normal. It doesn’t concern itself as to which categories those posts are in until after it starts displaying them. We could certainly restrict posts to the categories you prefer, but if you want to assure exactly three categories are displayed at all times, the template fails you there.

    So, if you need 3 categories at all times with 1 or more posts under each, the use of conditionals and whatnot with a single standard post loop won’t help you. Not unless you’re bothering to make sure the current N posts are always in those 3 categories… That’s why I believe setting up 3 different post loops with query_posts() initializing each (to define the category and post count for each) is your best option.

    akela

    (@akela)

    Nice work! Is it possible to show one post as excerpt(or the whole post) and for others just titles(permalinks) under the same scenario – last N posts organized by categories?

    petrewp

    (@petrewp)

    Hi,

    Excuse my poor english, but I’ll try to explain my problem and needs.

    This plugin might be the one I need, but I’ve read through the thread but not become any smarter (not that I’m smart anyway), much because of my lack of PHP knowledge, but maybe more lack of knowledge of where goes what.. yep..

    P1. I want to have one post on the frontpage, only.
    P2. When I click a category, I want all the posts within that category to show on the following page

    Q1. How do I use this script for this (is there another that does this maybe, this might not be the one)?
    Q2. Should it be pasted in the index.php in the template folder?
    Q3. Which settings should then be used.

    Big thanks in advance,

    Petter / Norway

    Kafkaesqui

    (@kafkaesqui)

    akela, sorry for hitting your Q so late, but sure, why not? :)

    All you need is to perform a test against a PHP variable that ticks off or iterates for each post within a category, but resets at the start of the next category.

    petrewp,

    First, this is not a plugin. It’s important to keep that in mind. Now…

    P1. Then this template is not for you.
    P2. WordPress nearly provides this, as they say, ‘out of the box,’ though it does require a bit of help if you want one post on the home page but *all* posts in a category on just one page.

    I’d look at this plugin, which deals with both your P’s:

    http://mattread.com/projects/wp-plugins/custom-query-string-plugin/

    Kafkaesqui,

    Big thanks, the plugin from mattread was exactly the thing I needed.

    Cheers,

    Petter / Norway

Viewing 6 replies - 46 through 51 (of 51 total)
  • The topic ‘Display by Category home.php template’ is closed to new replies.