小工具中的快捷码打印在标题之前

时间:2015-09-08 作者:mysticalghoul

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

您好,我有以下代码显示在小部件标题之前。因为我从研究中了解到,我必须使用return,但它不起作用。请帮忙。

            <?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\');    

2 个回复
最合适的回答,由SO网友:shanebp 整理而成

函数中的某些调用将回显而不是返回。而不是the_author_meta, 使用get_the_author_meta.而不是the_excerpt, 使用get_the_excerpt.并使用jycr753的方法;返回整个字符串,而不是回显它。

SO网友:Jorge Y. C. Rodriguez

快捷方式应返回一个值,如果您回显该值,则会将其置于其他值之前。试试这个:(未测试)

<?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\');    

?>