Exclude categories from page (not home!)
-
I’ve already combed through the Forum, and nothing I’ve found works.
I need to keep posts from a certain category from being displayed on a specific page (not the home page).
This codex link ought to work, but it just doesn’t! Could it be incompatible with the most recent WP version?
I’m dropping:
<?php if (in_category('3') && is_page('10')) continue; ?>
into the right place within my custom page template, but the categories persist.
The plugins out there all work only on keeping categories off the home page.
Any suggestions?
-
A little help here?
More info please.
What page? And (briefly), why?
I’m using WP to create a business website of which a blog will be a subsidiary part.
I’m generating posts to populate a WP page that will act as an easily updatable and categorizable portfolio — and I don’t want those “portfolio” posts showing up on the blog.
The plugins that I’ve found are all organized around excluding posts from the “homepage”, but I need the capability to tell specific page templates which category they are allowed to display. The WP Codex provides guidelines for accomplishing this, but the code is not working for me (see first post above).
Only Posts belong to categories, not Pages, so the code may be:
<?php if (in_category('3') && $post->ID == '10')) continue; ?>
sparkletack, I feel your frustration. Just went through the same issue myself. What you’ll find is that WP Pages are meant to be static.
Most likely you want to use archive.PHP to do what you want. It is possible to exclude (or include) specific categories.
I’m not sure if I was clear, or perhaps I’m misunderstanding your replies. Let me try again, with more detail:
I’ve created a page called “blog”. It uses a custom page template which invokes the Loop to display posts, the same way the home page in vanilla WP does. It’s working great, displaying everything.
Under “Reading Options” in the Admin section, I’ve given another page — “calyx design” — the designation of front page. It uses a different page template than “blog”, which does NOT include the call to display posts:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>"
Now, on yet a third page (“portfolio”), I will call a specific category of posts by using “wp_list_categories” or something of that nature.
All I want to do is keep that “portfolio” category of posts from displaying on the “blog” page — which I am certain that should be able to do, thanks to this Codex article.
In the following code, page 10 is the “Blog” page, and category 3 is the “portfolio” category:
<?php if (in_category('3') && is_page('10')) continue; ?>
I took this right out of the Codex article, and can’t understand why it isn’t working — it seems as though it ought to apply perfectly.
Thanks for your help…
So, you tried putting
<?php if ( in_category('3') ) continue; ?>
where 3 is the cat id, inside the loop on the blog page and it didn’t work? Maybe look here:
https://rhymedcode.net/1001-wordpression-loops/miniblog/
but bear in mind that’s an old article.@azaozz: yes, I put that line of code in the page template that’s used for the “blog”page, like this:
`<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php if (in_category(‘3′) && is_page(’10’)) continue; ?>`
And thanks for the “miniblog” link, but that doesn’t exactly work for me either.
I really just want to find out why this code isn’t working, even though the WP Gods of the Codex say it should.
I’m a little confused. First, let me clarify that there is a difference in WordPress. When you say “page”, we are all thinking of “Pages” (note the capital “P”). Pages are static. You say you set “calyx design” as the front index Page for your site. This means that “calyx design” is a Page – and thus is static, and *will not* display categories of any kind. It will only display the actual content you write within the “Write Page” area of your admin section. The Loop also exists for Page content – otherwise it wouldn’t be able to display the content you write.
Now, if you’ve done something to set the *category* of “calyx design” as the index page of your site (note the lowercase “p” – meaning what *looks* like a page, but displays posts, and is quite dynamic), then that’s something else.
So my question is, when you say I put that line of code in the page template that’s used for the “blog”page, what do you mean? You put that code in page.php? index.php? archive.php? The code you have up there should be in the “index.php” file, because that’s the base for categories. If you put it in “page.php” it won’t matter, because Pages don’t display categories.
Using “wp_list_categories” provides a links list of categories. This is usually placed in the sidebar, but you can put it in the content of a page (in page.php) if you like. However, the code you’re trying to use has no effect on what the list of links displays – it only affects the posts themselves. If you want to exclude a *link*, then you just need to specify that in the call:
wp_list_categories('exclude=10');
Other than that, I guess I’m confused as to exactly what file you’re trying to put this in. if it’s page.php, it’s not going to do anything. If it’s index.php, then you could do a custom query to ecxlude the category you want to leave out.
Thanks for the detailed response… you think you’re confused? ??
I did have that line of code in the wrong place, apparently — I put it into the page.php file, and then into a custom Page template called blogpage.php… and of course it didn’t work.
I have now put that code line into the index.php page, and it STILL doesn’t work…no categories end up being excluded… but now I’m starting to realize that I’ve had the wrong idea about what I’m trying to do all along.
I’d been operating under the assumption that it was possible to have a “flow” of posts appear on two different pages (or Pages), and separate which posts appeared on which pages by excluding post categories. In other words, all posts except for “portfolio”-categorized posts would appear on the blog page, and only the “portfolio”-categorized posts would appear on the portfolio Page.
If I read your comments correctly, this is not possible.
If I have that right, I’m all ears for any ideas you might have about how to accomplish what I’m trying to do….
well, first off, “blogpage.php” has to be called into your theme. If you create that file, it won’t instantly use it for a particular page, like “blog”. they don’t work that way.
Pages can be specified by going into the page.php file and doing something like
if(is_page('2')) { stuff here for Page ID 2 } else { format stuff for all other static Pages }
Now, you *can* have a “flow” of posts actually appear on static Pages, but it requires some serious coding to do so. (I’m actually doing this right now for a site I’m working on – been trying to get the code refined for about a month now, and I’m still not finished yet!) Normally, you don’t even need to do something like this, as you can accomplish what you’re looking for just by using Categories and conditionals. (My case above is something extreme, and I don’t recommend doing it at all!)
But if I’m reading your idea correctly, what you’re *trying* to do is use WordPress as a CMS, where you:
1) have a static Page as your index page on the site.
2) have other static Pages, to emulate a regular static HTML site (Pages are generally for content that will never change)
3) Have a “blog” section on your site.
4) have a “Portfolio” section, where the “blog” would *link* to the “portfolio” section, but the blog would never display actual portfolio posts. (In essence, you’d have a link to the “portfolio” category, but anytime you posted to that category, the only place you’d see that post is if you were *in& that category – no place else would show it)You *can* do this.
I’ll tackle the “portfolio/blog” stuff, since the static pages/index page you should be able to do yourself.
So what you want to do is start by creating a couple of parent categories. One of these parents should be “blog” and the other should be “portfolio”. You should separate all other categories into these sections (unless you want stuff that won’t appear in either category – then create categories for those too).
So say your “blog” are will have a news section, a rants section, a personal section. The “portfolio” will have your work, like illustrations, graphical buttons, and digital art. So you want to set up your categories like so (ID’s in parentheses):
(1)Blog
(3)-News
(4)-Rants
(5)-Personal
(2)Portfolio
(6)-Illustrations
(7)-Graphical Buttons
(8)-Digital ArtSo you see that “Blog” and “Portfolio” are the parents categories, and everything else is subbed based on that.
So, when you want to display your stuff, in the index.php file, you add in your code:
<php if(is_category('1') || in_category('1')) { if(is_category('2') || in_category('2')) continue; //stuff for post display here } else { //start showing the portfolio stuff if(is_category('1') || in_category('1')) continue; //stuff for post display here } ?>
So see, that says “If you’re in the “Blog” section, and if the posts are in the “Portfolio” section, skip them.” So you’re still in the Blog category, but it’s skipping over the portfolio stuff and just displaying the blog stuff – ELSE – if you’re on the Portfolio pages, then don’t display any of the Blog stuff.
Man, I know it’s confusing, but I hope it makes sense (and answers your question!) You might have to edit that to refine it a bit, but it should work. Please note my bit of “frazzledness”, as I’m doing ten things at once (and two of them involve children under the age of four) – so if I’ve skipped over anything..well forgive me LOL
BTW the line
<?php if (in_category('3') && is_page('10')) continue; ?>
doesn’t do anything. It checks if the current post is in category 3 AND is page 10 (which is impossible since all pages are uncategorized), so it always returns false.Perhaps you meant to use
<?php if (in_category('3') || is_page('10')) continue; ?>
That’s why I asked if you tried using just
<?php if ( in_category('3') ) continue; ?>
as this will work nicely.On the other hand, if you make custom template for a page it will only be used on the page(s) that specifically request it (where you set it), so you don’t have to add conditional tags – is_page(‘6’)… is_single()… etc. It always will be the page you assigned to it.
In your case, as doodlebee suggests above, is best to use custom categories templates to show the posts you need.
Thanks to both of you — this gives me quite a bit to chew on, and chew I will.
I’m continually amazed by the generousity of people in this forum…
Okay. For future generations (heh), I’ve managed to do what I need to do — thanks to everyone’s advice — by doing this:
1) arranging all of my portfolio-related posts as children of the parent Portfolio category — which happens to be “4”.
2) putting this line of code into the loop in “index.php”:
<?php if (in_category('4')) continue; ?>
3) using “wp_list_categories” to call for my Portfolio category posts from “header.php”
4) creating a custom template for the Portfolio category posts. The category for these is “4”, so my category template is named “category-4.php”. This file calls for a custom sidebar that shows appropriate links via an “include TEMPLATEPATH” function.
5) All other categories appear as normal on my “blog” Page, which reaches for the normal “category.php” file when necessary to display the various categories.
There are still some odds and ends to clean up, but that was (I hope) the heaviest lifting..
What about a custom category page? I have used that with success on previous sites and I also recall using another plugin to exclude that category from the “front page” or index.php page.
This plugin for custom category pages:
https://guff.szub.net/2005/07/21/post-templates-by-category/and this one to tell the portfolio cat not to appear on the front page:
https://ryowebsite.com/?p=46Now: I have NO IDEA if they work with the later versions of WP. The sites I use them on are all running WP 2.1.10..
- The topic ‘Exclude categories from page (not home!)’ is closed to new replies.