Hi to the community,
i try to build a shortcode that prints the Creative Commons logo and some text,
everything is working fine except that i put the shortcode at the end of the post but the shortcode goes to the top of the post content,
i use the code bellow:
<?php // Creative Commons License Shortcode
function creativecommons( $atts, $content = null )
{
extract( shortcode_atts( array(
'type' => '',
'code' => '',
), $atts ) );
{ ?>
<div class="license">
<div class="cc-img">
<img src="<?php bloginfo('template_directory'); ?>/images/cc.png" width="32" height="32" alt="Creative Commons License" />
</div>
<div class="cc-content">
<h4><?php echo $type ?></h4>
<small><?php echo $code ?></small>
</div>
</div><!-- end of license -->
<div class="clear"></div><!-- clear div -->
<?php }
return;
}
add_shortcode('cc1', 'creativecommons');
?>
I’m missing something here or something else have to do with my problem?
Thanks a lot,
Philip
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Couple things: I think your shortcode function should return the value rather than print it, and you shouldn’t have the output wrapped in braces like that (the one just before the first div, and the one just before the return). Unless that’s some obscure php syntax I’ve never seen (which is entirely possible)?
As far as I know, whenever you close a php tag–even inside a function–the parser will just immediately output the results, not return them.
Method 2
The only thing wrong with the braces in the function definition is that the closing brace is the same as the opening brace. The return statement is out of place. It should be within the function definition braces, and should look something like this (from W3 Schools):
function add($x,$y)
{
$total=$x+$y;
return $total;
}
echo "1 + 16 = " . add(1,16);
I am not sure what value you want returned.
Plus, the previous comment is correct. You cannot echo the values of variables defined in the function outside that function. I believe that variables defined in a function are local to that function. That is where the return statement comes into play.
Apparently, you want to return multiple values from your function. Since a function cannot return more than one value, you can do this with an array. You can then retrieve your values outside the function.
See following example I picked up on the web.
function small_numbers()
{
return array (0, 1, 2);
}
list ($zero, $one, $two) = small_numbers();
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0