The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. September 19, 2019, 01:47:54 PM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: Exclude certain values from foreach + Alternate control structure (stupid colon)  (Read 2583 times)
jp29997
Rookie
**
Offline Offline

Posts: 13


View Profile
« on: April 02, 2008, 03:40:25 PM »

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

Code:
<?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="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>

<?php endforeach; ?>


OK,so I tried to do this

Code:
<?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="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endif; ?>
<?php endforeach; ?>

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
Logged

No links in signatures please
DangerMouse
Expert
****
Offline Offline

Posts: 244



View Profile
« Reply #1 on: April 02, 2008, 11:37:46 PM »

Maybe a small typo? Wouldnt it be $category->cat_name rather than: "if(!in_array($categories->cat_name,$exclude))" ?

DM
Logged
jp29997
Rookie
**
Offline Offline

Posts: 13


View Profile
« Reply #2 on: April 03, 2008, 05:21:54 AM »

calling it a typo is the kindest thing anyone has said to me all day  ROFLMAO

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:

Code:
<?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="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>

<?php endforeach; ?>
<?php endif; ?>




Logged

No links in signatures please
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #3 on: April 03, 2008, 08:53:47 AM »

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 PHP is 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>
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!