Home › Forums › Front-end Issues › How to Use print instead of echo to stop shortcode output going at top?
i have this code:
function rightsidebarf( $atts, $content = null ) { ?>
<?php $rows = get_field('caci_side_bar_content', 'option'); ?>
<?php if($rows) : ?>
<?php foreach($rows as $row) : ?>
<?php if($row['caci_side_bar_description']) : ?>
<a href="<?php echo $row['caci_side_bar_url']; ?>" alt="<?php echo $row['caci_side_bar_description']; ?> <?php bloginfo('name'); ?>"><img src="<?php echo $row['caci_side_bar_image']; ?>" title="<?php echo $row['caci_side_bar_description']; ?> <?php bloginfo('name'); ?>" /></a>
<?php endif; ?>
<?php endforeach; ?>
<?php endif; ?>
<?php }
add_shortcode('rightsidebar', 'rightsidebarf');
which outputs images at the top of my sidebar.
But i want it underneath something else in my sidebar.
I think its the echo that is pushing it to the top.
so i need to rewrite this code to use print instead. the nearest i have as a test is:
function rightsidebarf( $atts, $content = null ) { ?>
<?php $rows = get_field('caci_side_bar_content', 'option'); $my_sb_image = "<div class='side-slider'>Hello image</div>"; ?>
<?php if($rows) : ?>
<?php foreach($rows as $row) : ?>
<?php if($row['caci_side_bar_description']) : {
print $my_sb_image;
} endif; ?>
<?php endforeach; ?>
<?php endif; ?>
<?php }
add_shortcode('rightsidebar', 'rightsidebarf');
but its still outputting at the top?
Shortcodes should not echo or output anything. Shortcodes need to return the html that they create.
function rightsidebarf( $atts, $content = null ) {
$output = '';
$rows = get_field('caci_side_bar_content', 'option');
if($rows) {
foreach($rows as $row) {
if($row['caci_side_bar_description']) {
$output = '<a href="'.$row['caci_side_bar_url'].
'" alt="'.$row['caci_side_bar_description'].
' '.bloginfo('name').'"><img src="'.
$row['caci_side_bar_image'].'" title="'.
$row['caci_side_bar_description'].' '.
bloginfo('name').'" /></a>';
} // end if row
} // end foreach
} // end if rows
return $output;
}
add_shortcode('rightsidebar', 'rightsidebarf');
HI John
thanks for replying. I tried this code.
It only output one image where it should have done and the lat tags at the top of the sidebar.
I tried moving return $output; to before } // end foreach, but it was the same problem?
tom
I forgot to take into consideration that it is a loop, so it’s only outputting the last one because each one overwrites the last one.
The difference between the below is the change from =
to .=
in this line $output .=
function rightsidebarf( $atts, $content = null ) {
$output = '';
$rows = get_field('caci_side_bar_content', 'option');
if($rows) {
foreach($rows as $row) {
if($row['caci_side_bar_description']) {
$output .= '<a href="'.$row['caci_side_bar_url'].
'" alt="'.$row['caci_side_bar_description'].
' '.bloginfo('name').'"><img src="'.
$row['caci_side_bar_image'].'" title="'.
$row['caci_side_bar_description'].' '.
bloginfo('name').'" /></a>';
} // end if row
} // end foreach
} // end if rows
return $output;
}
add_shortcode('rightsidebar', 'rightsidebarf');
John – thank you – works a treat now.
‘.bloginfo(‘name’).’ was outputting text above everything (loop problem??) but once i got rid of that it all worked fine.
thank you so much for your help.
have a good christmas
tom
I forgot the bloginfo() echos the value. You need to use get_bloginfo() instead https://codex.wordpress.org/Function_Reference/get_bloginfo
thanks john – ‘.get_bloginfo(‘name’).’ has worked a treat
thanks again
tom
The topic ‘How to Use print instead of echo to stop shortcode output going at top?’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.