Forums

How to mark first and last post while only getting one category? (3 posts)

  1. bernk
    Member
    Posted 3 years ago #

    Right now I have two categories, news and projects. I want the news posts to show up on the home page and the projects (posts) to show up only on the portfolio page.

    The first and last news post shown on the home page have to be styled differently, I have achieved this. However, I only want to show the news posts on the home page, so just one category. I've also achieved this. The problem is putting the two together.

    The way I've been able to mark the first and last post is by counting $posts and incrementing an index variable ($i) with each iteration of the loop, then just checking to see if $i=0 or if $i=count($posts)-1. The problem is that when I introduce posts from another category into the equation this method no longer works. Because even if they are not shown they get counted, so my way of checking fails : (

    Any ideas?

  2. MichaelH
    Volunteer
    Posted 3 years ago #

    You can use the in_category conditional...but I see a bigger problem. The first post of a category is easy, but getting the 'last' post for a category would require know the total number of posts in a category, then checking your counter against that total number.

    <?php
    //get category 8 total post count
    $categories=get_categories('include=8');
    echo 'count is' .$categories[0]->count;
    ?>
  3. bernk
    Member
    Posted 3 years ago #

    Thanks Michael.
    Do you have a better way to achieve this?

    I'll try to describe what I'm trying to achieve a bit better, and hopefully clearer. I really need help with this as this will be my first WP theme.

    The website I'm converting to use WP is http://balsi.be/home
    The idea is for the WP theme to look and behave almost identical to what you see there.

    As you can see, there is a large header image which pertains to the most recent post, "vom Ball zum Pögg". Each post has one associated header image with it. If I click on an older post, e.g. "Balsi gewinnt Krebsliga-Pitch" it should open that post in the single view, with that post's header image displayed, and no other posts displayed.

    The way I've set this up so far is to have a "news" category for posts where you write new posts and attach one image to them, not actually embed into the post but just attach. Then on the home page I get the attachment of just the most recent post and show it in the header. Make sense so far? I hope so.
    This part I have achieved fully.

    The problem: I also need to have a category for projects. If you go to http://balsi.be/portfolio you will see what this will look like.
    So, the idea is to have a page which shows a thumbnail of each project there is. For simplicities sake let's say each project also only has one image attached, and so has a thumbnail available to us.
    Then when I click on a project thumb I should be taken to a special single view for projects where I can read about the project, see the image, and jump to other projects.

    Please keep in mind the site I'm showing you here is NOT WordPress, it is just custom PHP/HTML.

    So in summary, I need to find a way to do all of the following:

    1. Display the image attached to the most recent news post on the home page.
    2. Show only the posts belonging to the news category on the home page.
    3. Style the last shown news post differently then the others.
    4. Show only the posts belonging to the projects category on the portfolio page (or category or whatever I should be calling that)

    I've been going over the documentation a ton but I'm new to this and could really use some help.

    Thank you for reading and trying to help!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.