[Solved] Display all posts within a category

Post Reply
dmorten
Jr. Bludit
Posts: 2
Joined: Sat Nov 04, 2017 6:29 pm

Hi folks,

I'm having trouble displaying all posts within a category.

Most of my file is copied from the theme help and inspiration from the built-in themes.
In my index.php, I have this bit to check where we are:

Code: Select all

  if ($Url->whereAmI()=='home') {
            foreach ($pages as $Page) {
                echo "<h3 class='entry-title'>".$Page->title()."</h3>";
                echo "<div class='entry-content'>".$Page->content()."</div>";
               
            }
        }
        elseif ($Url->whereAmI()=='page') {
            echo "<h3 class='entry-title'>".$Page->title()."</h3>";
            echo "<div class='entry-content'>".$Page->content()."</div>";
        
        }
        elseif ($Url->whereAmI()=='category') {
          foreach ($pages as $page) {
            echo "<h3 class='entry-title'>".$Page->title()."</h3>";
            echo "<div class='entry-content'>".$Page->content()."</div>";
            
        }
The first check for "home" works fine. It displays all posts.

The second check for "page" isn't used at the moment.

The last check doesn't work correctly. I have assigned my post with a category, and when clicking a link to a certain category, I'd like to display all posts within that particular category.

I'm certain it has something to do with the

Code: Select all

foreach ($pages as $page)
bit, as I'm dealing with Categories and not Pages.. but I can't find any documentation stating what variables are available and what they contain.

Any tips as to how I can alter the foreach loop so it displays all posts within a specific category?

I should say I'm a php rookie. (if you are in doubt by now). I've messed about with php and Wordpress some 10 years ago, but the world was a different place then.

Version 2.0.2 by the way - and thanks for any help!
User avatar
Edi
Site Admin
Posts: 3121
Joined: Sun Aug 09, 2015 5:01 pm
Location: Zurich
Has thanked: 54 times
Been thanked: 77 times
Contact:

It depends, what you like to to.

Basically all posts of a category are displayed when using the link with the category name. For example

https://mydomain.com/category/general

This is how the plugin Categories works.
Clickwork - Websites mit Bludit | Planet Bludit - Tipps und Snippets
User avatar
diego
Site Admin
Posts: 773
Joined: Sat May 16, 2015 2:53 pm
Been thanked: 1 time
Contact:

You have an error on the code with the variable $Page and $page, one with uppercase and the another not.

anyway, you don't need to filter by category, because is the same as home.

Code: Select all

if ($Url->whereAmI()=='page')
{
	echo "<h3 class='entry-title'>".$Page->title()."</h3>";
	echo "<div class='entry-content'>".$Page->content()."</div>";
}
// This ELSE incude Categories, Tags, Blog, and Home
else
{
	foreach ($pages as $Page) {
		echo "<h3 class='entry-title'>".$Page->title()."</h3>";
		echo "<div class='entry-content'>".$Page->content()."</div>";
	}
}
dmorten
Jr. Bludit
Posts: 2
Joined: Sat Nov 04, 2017 6:29 pm

Edi wrote: Sat Nov 04, 2017 9:42 pm It depends, what you like to to.

Basically all posts of a category are displayed when using the link with the category name. For example

https://mydomain.com/category/general

This is how the plugin Categories works.
Yes, that's how I would expect it to work - but it didn't :)
diego wrote: Sun Nov 05, 2017 1:14 pm You have an error on the code with the variable $Page and $page, one with uppercase and the another not.

anyway, you don't need to filter by category, because is the same as home.

Code: Select all

if ($Url->whereAmI()=='page')
{
	echo "<h3 class='entry-title'>".$Page->title()."</h3>";
	echo "<div class='entry-content'>".$Page->content()."</div>";
}
// This ELSE incude Categories, Tags, Blog, and Home
else
{
	foreach ($pages as $Page) {
		echo "<h3 class='entry-title'>".$Page->title()."</h3>";
		echo "<div class='entry-content'>".$Page->content()."</div>";
	}
}
Thanks, I managed to correct the lowercase p while my original post was in moderation. Not sure whether that sorted the whole thing out, but it works as a charm now :)
Post Reply