views:

123

answers:

6

I'm still somewhat a beginner to PHP, so sorry if this is a dumb question =)

What I'm trying to do is in a WordPress blog, insert a custom field with multiple values ("ingredients") into my RSS feed. (I also have other posts which are not recipes, which is why the headings "Ingredients" and "Instructions" are inside the if statement.) Here is my entire code so far:

    <?php
    function insertIngredients($content) {
     /* get ingredients into variable $recipeStuff */
     $recipeStuff = 
   if ($ingredients = get_post_custom_values('ingredients')) {
    echo '<h3>Ingredients</h3><ul id="ingredients">';
    foreach ( $ingredients as $key => $value ) {
   echo '<li>';
   echo $value;
   echo '</li>';
    } 
    echo '</ul><h3>Instructions</h3>';
   }
     /* add before content */
        $content = $recipeStuff . $content;
        return $content;
    }
    /* Do it! */
    add_filter('the_excerpt_rss', 'insertIngredients');
    add_filter('the_content_rss', 'insertIngredients');
    ?>

But a get an "unexpected IF" error, so I guess I can't put all that inside the $recipeStuff variable =) I just can't think of how else to put it in there.

(If it matters, the IF statement is exactly what I use in the posts on the page itself, and it works perfectly!)

Thanks very much in advance for any help! =D

UPDATE!

Here's what I have in my code now:

function insertIngredients($content) {
/* test for presence of ingredients & set variables */
if ($ingredients = get_post_custom_values('ingredients')) {
    $heading1 = '<h3>Ingredients</h3><ul id="ingredients">';
    foreach ( $ingredients as $key => $value ) {
        $ings = '<li>' . $value . '</li>';
    }
    $heading2 = '</ul><h3>Instructions</h3>';
}
/* if no ingredients, variables are empty */
else { $heading1=''; $ings=''; $heading2=''; }

$recipeStuff = $heading1 . $ings . $heading2 ;

/* add before content */
    $content = $recipeStuff . $content;
    return $content;
}
/* Do it! */
add_filter('the_excerpt_rss', 'insertIngredients');
add_filter('the_content_rss', 'insertIngredients');

I no longer get an error message, but the ingredients are not showing up in the rss feed. I'm not sure if it's because something is still wrong with the code, or if it takes a while to have an effect (though I don't know why that would be the case)? I use FeedBurner, if that makes a difference.

Thanks very much for your answers, everyone. I'm going to try a few different things and update again in a bit. Thanks! =)

+3  A: 

Why not turn it arround?

if (condition) {
$recipeStuff = '';
}
else
{
$recipeStuff = '';
}
Rhapsody
DUDE! Why didn't I think of that?! Thanks =))))
KeriLynn
Out of curiousity, does PHP support $variable = (condition)? true : false ; ?
joslinm
Yes, inline if, however - in the above code there is some loops, which would not really be done well.
Aurel300
+1  A: 

Obviously, this is not correct PHP syntax. What you need is a variable that concatenates the strings (li, etc.) and return it.

You should move if outside the $recipeStuff variable.

Vasileios Lourdas
+1  A: 

I think these are the problem lines

$recipeStuff = 
   if ($ingredients = get_post_custom_values('ingredients')) {

what does this do? or what value should it have?

$recipeStuff = 

try this instead

if ($ingredients == get_post_custom_values('ingredients')) {

When using the equal sign to compare values it should be a double == or triple === using one equal sign is how you set the value

$one = 1; // assigns the value 1 to the variable $one

// Compares the values
if($one == 1) {
   echo "True<br />";
} else {
   echo "False<br />";
}

Read the Comparison operators page

Phill Pafford
+1  A: 

No, that does not really work that way. It should be (ignoring the function header and whatever):

<?php
if (  HERE CHECK FOR INGREDIENTS  ){
    $recipeStuff = SOMETHING ;
} else {
    $recipeStuff = SOMETHING ELSE ;
}
?>

So, you can't use if (in this form) like an inline function. There IS an inline alternative, but it is hard to use loops inside of it. Echo will ALWAYS output something to the output buffer. Id est, you can't use echo like "$myVar = echo 'something';". Just do an assignment inside the condition.

Aurel300
There is no IF scope.. What do you want to say with "This must be defined outside the if statement, so it will be accessible even afterwards (scope)"?
Dennis Haarbrink
Sorry, my bad...
Aurel300
+3  A: 

What you want is called "functions":

function receipeStuff()
{
    $content = '';
    if ($ingredients = get_post_custom_values('ingredients')) {
        $content .= '<h3>Ingredients</h3><ul id="ingredients">';
        foreach ( $ingredients as $key => $value ) {
           $content .= '<li>';
           $content .= $value;
           $content .= '</li>';
        }
    } 
    $content .= '</ul><h3>Instructions</h3>';
    return $content;
}

 $content = recipeStuff() . $content;
cweiske
This doesn't look correct: if ($ingredients = get_post_custom_values('ingredients')) { should be if ($ingredients == get_post_custom_values('ingredients')) {
Phill Pafford
no, we just assign it AND check it if there is content in it. That may (and should) be split in two statements, but it works.
cweiske
A: 

The most easiest way to do it, and keeping it simple is by

if (!yourcondition) {//if its not true
$recipeStuff = '';
}
else{//else if it is true
$recipeStuff = '';
}
hart1994