
![]() |
jp29997
There is, literally, something wrong with my brain, so bear with me on this.
I am modifying a theme for my honeys blog. The section I'm about to quote is for an individual post's page,it iterates through every category assigned to the post and displays recent posts from those categories. She wants to exclude 2 categories from this list. the current code looks like <? php// this is where 10 headlines from the current category get printed if ( is_single() ) : global $post; $categories = get_the_category(); foreach ($categories as $category): ?> <li><h2>More from this category</h2> <ul class="bullets"> <? php$posts = get_posts('numberposts=10&category='. $category->term_id); foreach($posts as $post) : ?> <li><a href="<? phpthe_permalink(); ?>"><?phpthe_title(); ?></a></li><? phpendforeach; ?>OK,so I tried to do this <? php// this is where 10 headlines from the current category get printed if ( is_single() ) : global $post; $categories = get_the_category(); foreach ($categories as $category) $exclude = array("Spotlight","Featured"); if(!in_array($categories->cat_name,$exclude)): ?> <li><h2>More from this category</h2> <ul class="bullets"> <? php$posts = get_posts('numberposts=10&category='. $category->term_id); foreach($posts as $post) : ?> <li><a href="<? phpthe_permalink(); ?>"><?phpthe_title(); ?></a></li><? phpendif; ?><? phpendforeach; ?>But, I get a blank sidebar. I would convert everything to curly braces--not sure I'm using the colons right with an if inside a foreach and do my dumbass CS 131 debug steps, but if I do that she'll eventually give a big sigh and say forget it, basically afraid I'm going to destroy her sidebar, and rightfully so. So could somebody help a brother out? The get_the_category function is specified at http://codex.wordpress.org/Template_Tags/get_the_category DangerMouse
Maybe a small
typo? Wouldnt it be $category->cat_name rather than: "if(!in_array($categories->cat_name,$exclude))" ?DM jp29997
calling it a
typois the kindest thing anyone has said to me all day![]() anyway, it didn't work but i think you are correct that i was stepping through the wrong array i think i also had the endif in the wrong position i switched to cat_ID instead this is what i have now still no go: <? php// this is where 10 headlines from the current category get printed if ( is_single() ) : global $post; $categories = get_the_category(); foreach ($categories as $category) $exclude = array('135','14'); if(!in_array($category->cat_ID,$exclude)): ?> <li><h2>More from this category</h2> <ul class="bullets"> <? php$posts = get_posts('numberposts=10&category='. $category->term_id); foreach($posts as $post) : ?> <li><a href="<? phpthe_permalink(); ?>"><?phpthe_title(); ?></a></li><? phpendforeach; ?><? phpendif; ?>perkiset
I just hate that coding style... the WP people really screwed up when they chose to stay with that rather than moving on.
To debug, I'd echo the test array and every iteration to see what PHPis evaluating... I think it's simply a problem with the not-in-array and you'll see it as soon as you dump each iteration out.<edit>also, you might consider putting error_reporting(E_ALL) at the top to make sure there's no other issues that are causing the problem</edit> |

Thread Categories

![]() |
![]() |
Best of The Cache Home |
![]() |
![]() |
Search The Cache |
- Ajax
- Apache & mod_rewrite
- BlackHat SEO & Web Stuff
- C/++/#, Pascal etc.
- Database Stuff
- General & Non-Technical Discussion
- General programming, learning to code
- Javascript Discussions & Code
- Linux Related
- Mac, iPhone & OS-X Stuff
- Miscellaneous
- MS Windows Related
- PERL & Python Related
- PHP: Questions & Discussion
- PHP: Techniques, Classes & Examples
- Regular Expressions
- Uncategorized Threads