0

Im trying to echo the content from inside shortcodes into a conditional. I have two shortcodes like this:

function fb_fans( $atts, $content = null ) { return $content; }
add_shortcode( 'fans', 'fb_fans' );

function fb_nonfans( $atts, $content = null ) { return $content; }
add_shortcode( 'nonfans', 'fb_nonfans' );

then inside the loop I want echo content that the user puts inside...
[fans] echo this content [/fans] and also
[nonfans] else echo this [/nonfans]

heres what I have that obviously doesnt work

if ($like_status) { echo $fb_fans; } else {  echo $fb_nonfans; } 

anyone know how to write this correctly??? Thanks!

2 Answers 2

1

You need to use do_shortcode built in WordPress function as shown below:

if ($like_status) { 
  echo do_shortcode('[fb_fans]'); 
} else {  
  echo do_shortcode('[fb_nonfans]'); 
} 

Please refer do_shortcode official documentation.

1

Use the actual function if you want to programatically call the shortcode.

echo fb_fans($atts, $content);

UPDATE:

Well, I think you need to go back to learning how shortcodes work. You simply need to use the_content() to display shortcodes properly.

Within your content area, you the user would type:

[fans]FOOBAR[/fans]

This would output FOOBAR inside of the content. A shortcode is basically a way to use PHP functions within the content area safely.

7
  • unfortunately its coming up blank. scratching my head because two of you had the same answer. Maybe i need to add something to my loop to get content? heres the whole loop....... if (have_posts()) : while (have_posts()) : the_post(); if ($like_status) { echo fb_fans($atts, $content); } else { echo fb_nonfans($atts, $content); } endwhile; endif;
    – user12571
    Jan 30, 2012 at 2:22
  • You have to declare the variables $atts and $content to contain some sort of data. Jan 30, 2012 at 3:02
  • any idea of how to do that, when I want to only echo what the user puts inside of each shortcode?
    – user12571
    Jan 30, 2012 at 3:39
  • youre definitely right about learning shortcodes. this is a very simple shortcode. instead of return $content i tried return do_shortcode($content); I think it made a difference. Where Im getting tripped us is how to write a conditional, where one shortcode content is the if, and other is the else.
    – user12571
    Jan 30, 2012 at 23:06
  • Why not use a FB plugin to do all of this? There are plenty out there. :) Jan 31, 2012 at 2:25

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.