I’m in the process of building this site for a sports club.
The categories are available on the menu (via ‘Club Archives’), but when selected, the category filter does not work. ‘News’ posts show, no mater what category is selected.
I haven’t spotted any coding errors, and have had no luck searching for clues on Google/Forums. I wonder whether there is a problem with the database. Your advice on a fix would be hugely appreciated. It’ll probably be something simple/missing. Let me know if you want me to quote any code (not sure what will be helpful).
Thanks in advance,
I take it you’ve checked on the Post Edit page in the Dashboard to ensure that each post has been allocated to just one category, including de-selecting the default “uncategorized” [sic] category?
thanks for the quick reply.
All posts now have a single associated category (just followed your advice, and made sure), but the problem persists. No published posts are listed as uncategorized (just a single draft post).
I’ve been playing with the Index.php file (wanting just ‘news’ on the home page) and wonder whether this is causing the problem somehow.
<div id="mainColumn"> <?php query_posts('category_name=news'); ?> <?php if (have_posts()) : ?> <?php while (have_posts()) : the_post(); ?> <div id="post-<?php the_ID(); ?>" class="post"> <div class="postMeta"> <p class="container"> </p> </div>
When news is removed, all posts show on the home page, and when selecting a category filter.
I don’t actually understand why you’re altering the PHP. Does your theme not support custom menus? Because if it does, you just create a custom menu with the categories you want, in Dashboard –> Appearance –> Menus.
If you do need to alter the PHP, then as far as I know,
query_posts()is used like this:
query_posts( array ( 'category_name' => 'news', 'posts_per_page' => -1 ) );
This will show all posts in a category.
I could be wrong, though, and there could be another way.
The theme does not support Custom Menus, but is hard coded to allow categories to be selected on the nav bar. The links work! They land on the correct page ‘/category/resources’ for example – but the content is incorrect.
Looking back at your previous posts, I think you’re right that the entry in index.php might be your problem. It looks to me as though this template is being called every time.
What category templates do you have in your theme?
Thanks for the tip.
Just reading through the codex page on category templates
The same theme has been used here and works fine for Categories. Viewing their source code, I see marginal differences on the nav bar coding, but cannot tell if they have a category template installed.
Looking through the theme files again, I notice now that there isn’t even an archive.php – which means (guessing) that there is no ‘category template’ installed?
This seems odd. I guess I need to create some new PHP to get the nav bar working. Your thoughts on this/tips?
Well, I think you have to remember that you don’t have to do anything special to output a category list. If I understand things correctly, WP works from the query string in the URL to work out what query to send to the database. So if you point to a page called:
… WP knows to get the posts in the ‘news’ category.
So if you look in the twentyten theme, there is a template file called category.php which in turn calls loop.php, which decides how to output content depending on the format.
On the other hand, twentyeleven uses category.php, which uses content.php.
In both cases, category.php is used because
is_category()is true and so WP calls the first appropriate template it finds on the WordPress Template Hierarchy
However, other themes can adopt other strategies. For example, a theme might put everything in index.php in a big
switchstatement or something. The template index.php gets used if no other template is available.
I may be wrong—it has been known—but I think if your permalinks are correct, WP should output the correct category lists without any intervention from you. In other words, I don’t think you need to use wp_query() or anything else. I think the correct behaviour is built in. The loop always has the proper data because WP parses the URL query string.
Does that help at all?
I’m still not sure what to do to get the categories working. I can work around it, as you suggested with a custom menu in the sidebar, but would rather solve the problem.
The permalinks are working fine. The URL is correct when a category is selected from the nav bar. There is content in each category. But ‘News’ shows up every time.
So WP is not getting the right posts from the database.
Actually, just realised that I can’t use a Categories menu in the sidebar – same problem. Categories selected don’t show up. Just ‘news’.
I could created pages of all the posts (exclude those pages from the header, and have them on a sidebar menu) – but this still means that the categories will not work, which is not good for the club.
I think my point was that, as long as the URL is correct and as long as no other code intervenes, WP should do what you want automatically. It doesn’t matter whether the code is in a custom menu, a link on a page or anywhere else. If the URL is correct, then the correct category list should appear without any coding on your part.
I don’t recall suggesting that you should use, or need, a custom menu in a sidebar. All you need AFAIK is for your URLs to be correct: and they seem to be to me.
If, however, you add any code like this:
<?php query_posts('category_name=news'); ?>
… then you’ll just get the posts in the ‘news’ category if the template containing that line is used.
The URLs from your menu look correct to me, as I say, so I’m trying to work out why they aren’t displaying the right list. The choices are between the following, as far as I can see:
- You have some custom code that is overriding the default behaviour
- Your theme does something strange with the loop that means the default behaviour is not working
So, I’m not sure what custom code you have in your templates, or what strategy your theme uses for making sure the correct template is used, but I think my starting point would be to get rid of any custom code that tries to list a category and then work out from the source files how your theme decides what template to call. Once you know what template is being called, you can examine the code to see why the correct category list is not being displayed, if that’s still the case once all custom code has been removed.
My apologies if I’ve misunderstood either what you are saying or what you are trying to achieve.
Hi, and thanks for all your advice. I think I’ve mapped out a new way forwards, and am hugely grateful for your help.
You’re right; best to take a few steps backwards. The problem was definitely
<?php query_posts('category_name=news'); ?>in the index.php template, which was being referenced more than needed. I’ve stripped it out. My changes to the original code did not go far enough, evidently, to get the job done as I wanted it. The best way forwards is the simplest.
Instead of a news feed on the home page, I’ve dropped the query code altogether, and dropped the news feed as a landing page – now using a dedicated ‘home’ page in its place, with a welcome message. News will appear through the ‘menu’ (categories) drop down, should anyone want it.
- The topic ‘Categories not filtering. Code problem? Database Problem?’ is closed to new replies.