Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Various output/exclude category fixes on a theme WordPress

  • SOLVED

I need to do the following with this code, but can't seem to manage - it's a modified version of the Toolbox theme.

<strong>In index.php:</strong>

http://codepad.org/EFyPiPvX

1) On line 132 - don't show categories 331, 332 or 333 - this works from line 193 onwards, but doesn't for the larger, featured post

2) Limit the number of posts output on the frontpage in the 'output other posts' section to 5 (currently displaying 6)

3) Limit the number of posts output on the second page and onwards in the 'Output content for page 2 onwards (non-frontpage)' section to 6 (currently displaying 7)

FIXED by AdamGold: 4) In sidebar.php Limit the output here of posts in cat 330 (they're actually in 331, 332 or 333 - 330 is the parent cat, which none of the posts are assigned to directly) to 3 posts (currently showing all of them, so far).


I also need to put the functions at the top of index.php into my theme's functions.php, but copying and pasting them in doesn't work (it stops output after the first featured post title and image) - any ideas?

Answers (1)

2011-05-10

AdamGold answers:

1) After line 128, add this code:
if(in_category('331')) continue; if(in_category('332')) continue; if(in_category('333')) continue;


AdamGold comments:

2) Before line 185 which is:
if (have_posts()) : while (have_posts()) : the_post();
Add this code:
query_posts('posts_per_page=5');
3) Is it the part after the 'else'? If so, you will need a new loop.
4) Replace:
<?php query_posts($query_string . '&cat=330'); ?>
With
<?php query_posts($query_string . '&cat=330&posts_per_page=3'); ?>


Jon comments:

Thanks Adam - OK:

1) Doesn't work - doesn't output anything for that code block now - I guess because the count has gone over 1?

2) Doesn't work - nothing output at all after the first post

3) OK, I'll try that afterwards

4) Works - thanks. Removing this from my post to make things simpler.


AdamGold comments:

Can I see the modified file? (index.php)


Jon comments:

Sure: http://codepad.org/v30pJU3z


AdamGold comments:

1) Try replacing
$my_query->the_post();
with
the_post();

2. Instead of
query_posts('posts_per_page=5');
Try
$new_query = new WP_Query('showposts=5');

And then, instead of
if (have_posts()) : while (have_posts()) : the_post();

Try
if ($new_query->have_posts()) : while ($new_query->have_posts()) : $new_query->the_post();


AdamGold comments:

The ; got away from the code block :D
if ($new_query->have_posts()) : while ($new_query->have_posts()) : $new_query->the_post();


Jon comments:

That's trying to dump every post onto the homepage (the first, featured post bit doesn't seem to be limited to 1)

Updated code: http://codepad.org/1YJULvQW


AdamGold comments:

Okay, so turn it back to $my_query->the_post(); and remove all the in_category I posted.
Replace:
$my_query = new WP_Query('showposts=1');

With
$my_query = new WP_Query('showposts=1&ategory__not_in=331,332,333');


AdamGold comments:

Sorry, should be:
$my_query = new WP_Query('showposts=1&category__not_in=331,332,333');


Jon comments:

Thanks Adam, you're losing me here though!

Here's what I have - it may be easier for you to just edit the code directly? Here's a jsfiddle link (codepad was down): http://jsfiddle.net/angym/


Jon comments:

Oh, and this code doesn't seem to be doing anything much - a post from one of 331/332/333 is showing in the top featured area.


AdamGold comments:

Yes, I think editing it directly would be the best solution. I just updated the code at JSfiddle, please take a look.


Jon comments:

Doesn't seem to be any difference - do you want to save it back to codepad? Don't think jsfiddle has updated it.


AdamGold comments:

http://pastebin.com/q32yK87D


Jon comments:

Got it! Thanks Adam. So I'll just create a new loop for the second page onwatds - any reason why taking those functions at the top of the page out would stop the output right at the point where it tries to pull wcs_build_category_list, and I'll give that another go?


AdamGold comments:

Glad it worked :) I didn't understand the problem with the functions? Can't see the reason why they wouldn't work in functions.php. Please clarify the problem and post your functions.php (after the functions paste) here.


Jon comments:

Here you go: http://pastebin.com/PnH2mzxw


Jon comments:

Oh and I should have clarified - they work fine at the top of index.php, but as soon as try and put them into functions.php (so I can use them on other pages), the template code stops being output as soon as WP hits one of the custom functions. I'm guessing that's because it can't find them in my theme's functions.php, but I don't know why that would be?


AdamGold comments:

What do you mean by stops being output? I need to see how you call the functions when the problem occurs.


Jon comments:

Maybe I'm doing it wrong - I copied those functions out of the first php tag in index.php and dropped them into the linked functions.php

I figured I wouldn't need to change the function calls in index.php, as they would look for the function in functions.php and run it.

Now, even if I just move 1 function to functions.php, as soon as WP gets to that function call - like:

<?php if ($counter2 == 3) {
wcs_display_ad_block_page_1_item_3();
$counter2++;
} ?>

...as soon as it hits this, no more code is output - when I view the HTML source, that's the end of the document.

Possibly I'm just missing something simple? I've not done this before.


AdamGold comments:

Well there's probably an error that prevents the output to be displayed. Please turn WP_DEBUG to 1 in wp_config.php and tell me if you can see any error. (with the functions in functions.php)


AdamGold comments:

Any update?


Jon comments:

Hey Adam - sorry about that - just tried again now and the debug=true option is great - I didn't realise you could do that. Looks like I was just trying to define the function twice.

Thanks for your help!