Forum Replies Created

Viewing 15 replies - 1 through 15 (of 65 total)
  • Well I’m actually surprised that isn’t causing your page to error out. If the plugin is disabled then that function shouldn’t be defined anymore. You could try commenting it out and just see if it will change anything, but at this point I just don’t know.

    There has to be some variable that’s missing here. Something to do with your theme perhaps. As a last resort, could you try switching to a different theme (like WordPress’s default Twenty Eleven) and then creating a category template there? If you want to try that, you’ll need to paste your category.php template for that theme so I can make the necessary modifications.

    At least then (if it works) we’ll know the issue was with the theme’s integration with the code and not necessarily the code itself.

    Alrighty, let’s try this. Can you disable all of your plugins (temporarily) and then visit one of the parent category pages to see if it’s working now?

    If so, one of your plugins is causing this. You can find out which one by enabling them one at a time and checking the category page again after each new plugin is enabled. Eventually, you’ll find the plugin that’s responsible for the break and we can go from there.

    If disabling the plugins doesn’t fix it, then I’m afraid I don’t really know anything else to do. :/

    Well the thing is the WordPress template hierarchy doesn’t really work that way. You can create a category template for a specific category (using its slug or id), but there’s no direct way to specify a template for just parent categories and a different one for child categories. That’s generally not a problem though, because one can create a generic template and use conditional statements and custom queries to achieve the desired result.

    You can view a diagram of the template hierarchy here.

    You can see in that image that category pages go in this order:
    category-$slug.php > category-$id.php > category.php > archive.php

    In your case, you don’t have any specific category templates and you don’t have a generic category template, so your theme is falling back to the archive.php (which should be fine because we used conditionals).

    That’s the thing that has me puzzled is I don’t understand why your archive page isn’t filtering your posts with the new code you added.

    One thing you can try is to create a new file called category.php so that it will be used explicitly for categories. I’ve attempted to modify your archive.php code to work for this new category.php page:

    http://pastebin.com/N3u0rQk2

    One line in your archive.php has me quite puzzled as well. Would you mind posting your header.php, footer.php, and functions.php as well?

    It’s my pleasure to try and help out!

    As far as I can tell, both of those plugins have nothing to do with what posts are displayed in which categories. All they seem to do is change which categories get shown in your sidebar (or wherever you list categories at). You have to actually click on one of the listed categories to view what posts are in it (on your category/archive page).

    This being the case, I don’t really see how either of those plugins would interfere with the code I gave you.

    If you did want to try FoCaL again, you first need to make sure your theme supports widgets because that’s how FoCaL works — you add a widget to your sidebar that lists categories but limits them to your specifications.

    I’m not really sure I have anything else I can tell you. Without actually working on your site myself, it’s hard to debug and come up with solutions.

    You could try posting some screenshots of different page outputs you’re getting or things you’re trying.

    Hmm, I went and created some categories and posts to test the code on and it seems to work for me. The only thing I can assume is that you either have a plugin that’s altering the category query for some reason or we’re not on the same page about what the code actually does.

    In WordPress when you visit a category index page (usually something like yoursite.com/category/category-name-here you will see all posts that are filed under that category and all child categories of it. The code I gave you limits that query to only show posts from the parent category (it excludes posts from all child categories of whatever category is currently being viewed).

    I know you had mentioned the Fold Category List plugin in your first reply, so I went and downloaded it to see what it was all about. Are you trying to edit the output of the plugin’s category list? because if so there are tons of options in the Settings > FoCal menu in your WordPress admin.

    No worries 🙂

    I’ve modified the code as necessary and re-posted it here:
    http://pastebin.com/D1JEPmjb

    I haven’t tested it, but I don’t see why it shouldn’t work. I used the is_category() conditional to ensure that my changes only affect category pages (since this is in the global archive.php file).

    Let me know how it goes!

    Edit: Make a copy of your previous archive.php file before making any changes so that it’s easy to restore if something goes wrong!

    I can see now why you must have been confused before! That’s not actually the right category.php file. The one I need is the template file that goes to whatever theme you’re using.

    It should be located in the following folder:
    /wp-content/themes/YOUR_THEME_NAME/category.php

    If it’s not there, then look for archive.php in the same folder. Lastly if that’s not there, look for index.php.

    This is of course assuming that this isn’t a child theme.

    Can you tell me what theme you’re using?

    In fact, why don’t you post your category.php template to pastebin for me to take a look at (if you don’t mind).

    Well you don’t copy/paste it verbatim. Your category.php template should already have a loop section setup that looks something like this:

    if ( have_posts() ) : while ( have_posts() ) : the_post();
    // stuff here
    endwhile;
    endif;

    You need to modify the query just before starting the loop (that’s most of the code I gave you).

    No problem; hope it works out for you!

    Remember, that code goes in the category.php template 🙂

    Here you go 🙂
    I tested this on my test blog; it’s fully functional code.

    http://pastebin.com/MfUbMwti

    There probably are plugins, but that’s what I was saying about gathering the category from wp_query.

    Whenever you go to a category detail page like this:
    yoursite.com/category/category-name

    WP_Query automatically knows some things about that page (that’s how it displays the categories to begin with). You can use that slug that’s passed in to get the category id of whatever category is currently being viewed. Because of that, the code will work dynamically for all categories.

    If you’d like, I can write up a working prototype for you. 🙂

    You only need to know the parent category’s ID in order for that method to work (which should be one of wp_query’s default options on the category view page). Try globalizing wp_query and then printing it and see what’s in there:

    On category template:

    global $wp_query;
    echo '<pre>' . print_r( $wp_query, true ) . '</pre>';

    You can also try doing that with the $query_string global. If you can find the category slug you can get the ID from that through one of WordPress’s functions.

    Sure, you could do that. You’d just need first get all the child categories, organize them into an array of id’s, then pass that to your query.

    You could use get_categories and use its child_of parameter (passing the parent category’s id to there).

    After that, there are several ways to get just the id’s into an array. You might try looping the categories array and creating a new array from just the ids like this:

    $categories = get_categories( $your_args_here );
    $exclude_these = array();
    foreach ( $categories as $category ) {
    	$exclude_these[] = $category->cat_ID;
    }
    
    // at this point, $exclude_these is a proper array  of
    // child categories that can be passed to your query

    I can only show you the WP_Query argument because that’s what I use. You can exclude posts from a particular category. In this case, you’d want to exclude all posts from that child category.

    If Parent Cat 1 has a category ID of 1 and
    Child Cat 1 has a category ID of 2 then:

    'category__not_in' => array( 2 )

    would be the parameter for WP_Query. You can view the category parameters for WP_Query here.

    You could pass additional arguments to that array if you wanted to exclude other categories as well.

Viewing 15 replies - 1 through 15 (of 65 total)