Help needed: Exclude Category in Posts Index (home)
I've searched the forums, but I can't seem to find what I need. I was hoping to exclude a single category from the list of displayed posts on the homepage. Ideally, also want to display posts of that excluded category in a different section, but not necessary, since I can link out to that category's own page.
Is there a way to do this in code?
What I've seen is only separating the recent posts from the rest of the list. But not by category.
Is there a way to do this in code?
What I've seen is only separating the recent posts from the rest of the list. But not by category.
The home.php has all the displayed posts regardless of category, yes? I was hoping to exclude a certain category from this list. For example, I have the categories: general, design, asides, and travel. I want to exclude posts in the asides category from this display. Is there a way to do that?Edi wrote: Sat Jan 17, 2026 9:32 pm What do you mean with "the list of displayed posts on the homepage"? You can also give a link to your website.
Oh! Sorry, I am working on my custom theme for personal use.
My index.php calls home.php this way:
And so my home.php has this:
This list displays all my latest posts on the front page, from any category. I was hoping to exclude posts one specific category from this list.
My index.php calls home.php this way:
Code: Select all
<?php
// Bludit content are pages
// But if you order the content by date
// These pages works as posts
// $WHERE_AM_I variable detect where the user is browsing
// If the user is watching a particular page/post the variable takes the value "page"
// If the user is watching the frontpage the variable takes the value "home"
if ($WHERE_AM_I == 'page') {
if ($page->template() == 'custom') {
include(THEME_DIR_PHP.'custom.php');
}
elseif ($page->template() == 'static') {
include(THEME_DIR_PHP.'page-static.php');
}
elseif ($page->template() == 'micro') {
include(THEME_DIR_PHP.'page-micro.php');
}
elseif ($page->template() == 'now') {
include(THEME_DIR_PHP.'page-now.php');
}
else {
include(THEME_DIR_PHP.'page.php');
}
} elseif ( $WHERE_AM_I === 'category' ) {
include( THEME_DIR_PHP.'category.php' );
} elseif ( $WHERE_AM_I === 'tag' ) {
include( THEME_DIR_PHP.'tag.php' );
} else {
include(THEME_DIR_PHP.'home.php');
}
?>Code: Select all
<?php
foreach ($content as $page) : ?>
<?php $category = getCategory($categoryKey); ?>
<li class="list-group-item d-flex justify-content-between align-items-center px-4 py-3<?php if ( $page->tags( true ) ): ?><?php foreach( $page->tags( true ) as $tagKey=>$tagSlug ) :?> <?php echo $tagSlug ?><?php endforeach; ?><?php endif; ?><?php if ($page->sticky()) : ?> sticky<?php endif ?>">
<!-- Post -->
<div class="postItem me-auto">
<?php if ($page->sticky()) : ?><i class="fa-solid fa-thumbtack text-muted"></i><?php endif ?>
<a class="titleLink" href="<?php echo $page->permalink(); ?>"><h4 class="title"><?php echo $page->title(); ?> <?php if ($page->coverImage()) : ?><?php endif ?></h2></a>
<div class="blurb"><p><?php echo $page->custom('microblogging'); ?></p></div>
<p class="mb-0 readingTime"><small class="text-body-secondary"><?php echo $L->get('Reading time') . ': ' . $page->readingTime(); ?></small></p>
</div>
<p class="postDate mb-0 text-end"><a href="<?php echo $page->permalink(); ?>"><?php echo $page->date('j M Y'); ?></a></p>
</li>
<?php endforeach ?>
</ul>- Edi
- Site Admin
- Posts: 3108
- Joined: Sun Aug 09, 2015 5:01 pm
- Location: Zurich
- Has thanked: 74 times
- Been thanked: 114 times
- Contact:
You can exclude a category with an if condition.
For example, if you want exclude the category "Exclude", you can use the following:
For example, if you want exclude the category "Exclude", you can use the following:
Code: Select all
<?php foreach ($content as $page) : ?>
<?php if ($page->category() !== 'Excluded') : ?>
...
<?php endif; ?>
<?php endforeach; ?>
- Edi
- Site Admin
- Posts: 3108
- Joined: Sun Aug 09, 2015 5:01 pm
- Location: Zurich
- Has thanked: 74 times
- Been thanked: 114 times
- Contact:
You can use the inverse condition (=== instead of !==):
Code: Select all
<?php if ($page->category() === 'Excluded') : ?>

