1

{This question is marked as duplicate but i think my question and answer to it is much simpler then the original one}

Hi i have this following code that is showing before the widget title. For what i understand from my research is that i have to use return, but its not working. Please help.

            <?php 
            function get_review_code(){  
            echo '<div class="review-slider-wrap">';  
                    echo '<ul class="review-slider">';
                    global $post;
                    $args = array( 'numberposts' => 10, 'category_name' => 'review' );
                    $posts = get_posts( $args );
                       foreach( $posts as $post ): setup_postdata($post);
                        echo '<li>';   
                            echo '<div class="white-curve">';         
                                echo the_excerpt();                                                
                                $author_id=$post->post_author;                                                    
                                echo '<div class="author_full_name">';
                                    echo '- ';
                                    echo the_author_meta('first_name'); echo     '&nbsp;';
                                    echo the_author_meta('last_name');
                                echo '</div>';
                            echo '</div>';        
                            echo get_avatar( $post->post_author, 230 );
                        echo '</li>';
                    endforeach;
                    echo '</ul>';
            echo '</div>';
            }      
            add_shortcode('review_review', 'get_review_code');    
1
  • add_shortcode should return a value not echo it Commented Sep 8, 2015 at 20:12

2 Answers 2

1

Some of the calls in your function will echo rather than return. Instead of the_author_meta, use get_the_author_meta. Instead of the_excerpt, use get_the_excerpt. And use jycr753's approach; return the whole string rather than echo it.

0
0

shortcut should return a value, if you echo it, this will place it before anything else. try this: (not tested)

<?php
function get_review_code(){ 
    global $post;
    $content = "";
    $content .= '<div class="review-slider-wrap">';  
            $content .= '<ul class="review-slider">';  
            $args = array( 'numberposts' => 10, 'category_name' => 'review' );
            $posts = get_posts( $args );
               foreach( $posts as $post ): setup_postdata($post);
                $content .= '<li>';   
                    $content .= '<div class="white-curve">';         
                        $content .= get_the_excerpt();                                                
                        $author_id = $post->post_author;                                                    
                        $content .= '<div class="author_full_name">';
                            $content .= '- ';
                            $content .= get_the_author_meta('first_name');
                            $content .=     '&nbsp;';
                            $content .= get_the_author_meta('last_name');
                        $content .= '</div>';
                    $content .= '</div>';        
                    $content .= get_avatar( $post->post_author, 230 );
                $content .= '</li>';
            endforeach;
            $content .= '</ul>';
    $content .= '</div>';
    return $content;
}
add_shortcode('review_review', 'get_review_code');    

?>
4
  • This should work but some content is still above title. only the image is outputting right. Commented Sep 8, 2015 at 20:28
  • what do you mean? do you use the shortcut inside the content or do you call it with do_shortcut? Commented Sep 8, 2015 at 20:41
  • Yeah i am pasting this in the widget content [review_review]. its not showing all the content in the content after title. Commented Sep 9, 2015 at 4:14
  • @mysticalghoul np, but edit my answer so people can use it in the future as reference :) Commented Sep 9, 2015 at 7:37

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