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

JohnG
  • 486
  • 3
  • 22

4 Answers4

4

You can just concatenate your strings like this:

$category = '2';
echo do_shortcode("[rssfeed cat='" . $category . "']");
Rizier123
  • 58,877
  • 16
  • 101
  • 156
  • It's always best to break out of the echo function to include variables this way. +1 – Lee Dec 19 '14 at 10:34
  • @Lee: What do you mean "break out of the echo function"? – Lightness Races in Orbit Dec 19 '14 at 10:37
  • @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";` – Lee Dec 19 '14 at 10:39
  • Awesome - thanks very much. And thanks to all who suggested alternatives that work. – JohnG Dec 19 '14 at 10:42
  • @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 – Rizier123 Dec 19 '14 at 10:42
  • Being picky but you should put the string inside single quotes as it will be processed (marginally) quicker than double quotes due to php not checking for variables `echo do_shortcode('[rssfeed cat="' . $category . '"]');` – Tim Dec 19 '14 at 10:43
  • @Tim They don't make much difference if you don't have like 1000 of them (http://stackoverflow.com/q/13620/3933332) – Rizier123 Dec 19 '14 at 10:46
  • @Rizier123 So I thought I could work my problem out with that bit of information, but apparently not! I actually need to do: $category = the_field(news_cat); instead of '2'. If I do echo $category it produces the correct number, but in the function above it processes $category as the actual text the_field(news_cat). Is there a way I can pass the number through to the actual function?! Apologies... – JohnG Dec 19 '14 at 12:01
  • @JohnG This should still work, if you said the the output is 2 of `$category`, otherwise it's a `wordpress` problem! You can also try: `echo do_shortcode("[rssfeed cat='" . the_field(news_cat) . "']");` This should also work – Rizier123 Dec 19 '14 at 12:10
  • @Riziee123 Haha - that was more or less what I first tried before creating the category variable :) Unfortunately it doesn't work. I guess maybe it is a wordpress problem... Is there a way I can get an echo inside a variable? So something like: $category = the_field(news_cat); $var = echo $category; echo do_shortcode("[rssfeed cat='" . $var . "']"); ... This doesn't work of course, but is there another way I can echo a variable within a variable?? – JohnG Dec 19 '14 at 12:17
  • @JohnG The 'echo in echo' is a concatenation as in my answer shown! (See this: http://php.net/manual/en/language.operators.string.php) – Rizier123 Dec 19 '14 at 12:19
  • @Rizier123 Thanks - didn't seem to work for some reason though - might be my bad! I've figured out a way to do it, and will put a full answer in a comment below for anyone else who's got my specific problem. Thanks very much though - couldn't have done it without your initial explanation! – JohnG Dec 19 '14 at 12:32
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']");
?>
Tim
  • 316
  • 1
  • 7
  • @rnevius yeah I didn't see that until after I posted – Tim Dec 19 '14 at 10:38
  • @LightnessRacesinOrbit ... how? – rnevius Dec 19 '14 at 10:39
  • 2
    @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. – Lightness Races in Orbit Dec 19 '14 at 10:42
  • 2
    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. – Matt Gibson Dec 19 '14 at 12:01
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.

Matt Gibson
  • 37,886
  • 9
  • 99
  • 128
-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 . "']");
?>
JohnG
  • 486
  • 3
  • 22