0

My WordPress site needs to have a different RSS feed on every page. In the short-term, I need to find a way to output an RSS feed with category = "2" (which will later become a number from different pages). I'm relatively new to PHP though.

Is there a way to get a variable echo'd WITHIN an echo?

I've tried:

<?php
$category = '2';
echo do_shortcode('[rssfeed cat="$category"]');
?>

and

<?php
$category = '2';
echo do_shortcode('[rssfeed cat='echo "$category"']');
?>

... But obviously they don't work. Can anyone suggest a work-around? Thanks

2
  • @Rohil_PHPBeginner...do_shortcode() needs to be echoed Commented Dec 19, 2014 at 11:26
  • I was talking about other echo Commented Dec 19, 2014 at 11:26

4 Answers 4

4

You can just concatenate your strings like this:

$category = '2';
echo do_shortcode("[rssfeed cat='" . $category . "']");
Sign up to request clarification or add additional context in comments.

12 Comments

It's always best to break out of the echo function to include variables this way. +1
@Lee: What do you mean "break out of the echo function"?
@LightnessRacesinOrbit as in, like the example above rather than including the variable directly inside the echo string like echo "This is my string with $variable";
Awesome - thanks very much. And thanks to all who suggested alternatives that work.
@Lee That's why i made it like that :D I think you can spot it much better if you 'break' the echo statement with a concatenation and put the variable there
|
2

You can adapt your first attempt but swap the " and ' around - variables will be parsed if you use double quotes http://php.net/manual/en/language.types.string.php#language.types.string.parsing

<?php
$category = '2';
echo do_shortcode("[rssfeed cat='$category']");
?>

4 Comments

@rnevius yeah I didn't see that until after I posted
@LightnessRacesinOrbit ... how?
@rnevius: It directly answers the question, it is clear to a newcomer, and it is to-the-point. This is not "a ripoff" and the downvote is completely unwarranted. The PHP tag is odd.
Indeed. In particular, I wouldn't downvote an answer that seemed similar, or even identical, that was posted within a few minutes of another—people very often work on answers at the same time and produce very similar results. As to which is better—that's in the eye of the beholder, and there's usually room for more than one similar answer, as different people think in different ways.
1

Variable interpolation only happens between double quotes. But shortcodes can use either single or double quotes for their attributes, so if you put your quotes the other way around, it should just work:

<?php
$category = '2';
echo do_shortcode("[rssfeed cat='$category']");
?>

The PHP string now has double quotes, so $category will be interpolated to its value, and the attribute has single quotes, which will still work fine ("Shortcode macros may use single or double quotes for attribute values"), and not terminate the enclosing PHP string.

Comments

-1

Rizier123's answer above worked great:

<?php 
$category = '2';
echo do_shortcode("[rssfeed cat='" . $category . "']");
?>

I actually needed to pull a value from each WordPress page through Advanced Custom Fields, using the_field(category). When I tried to do:

<?php
$category = the_field(category);
echo do_shortcode("[rssfeed cat='" . $category . "']");
?> 

It didn't work - even though

echo $category;

produced the right value. Lots of suggested variations didn't work either - possibly this is a problem with Wordpress. I figured I needed to somehow pass a printed value of a variable into another variable, for it to work. For anyone trying to do what I was doing, a solution I found that worked is:

<?php
function abc(){
print the_field(category);
}
ob_start();
abc();
$category = ob_get_clean();
 echo do_shortcode("[rssfeed cat='" . $category . "']");
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.