我对PHP还是有些初学者,如果这是一个愚蠢的问题,请原谅 =)
我的目标是在WordPress博客中,在我的RSS订阅中插入具有多个值(“成分”)的自定义字段。(我还有其他不是食谱的文章,这就是为什么“成分”和“说明”标题位于if语句内部的原因。)以下是到目前为止我的全部代码:
<?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');
?>
但是我遇到了“意外的IF”错误,所以我猜我不能把所有内容放在$recipeStuff变量里=)我想不出其他方法把它放进去。
(如果有影响的话,IF语句正是我在页面上使用的帖子,它完美地工作!)
非常感谢您提前的任何帮助!=D
更新!
这是我现在代码中的内容:
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');
我不再收到错误信息,但是配料没有显示在rss源中。我不确定是否代码还有问题,或者需要一段时间才能生效(尽管我不知道为什么会这样)。如果使用FeedBurner,可能会有所不同。
非常感谢大家的回答。我会尝试几种不同的方法,并稍后更新。谢谢!=)