最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

functions - Probleme shortcode with list author

programmeradmin5浏览0评论

I have created a list of my contributors for my website it works well. I'm trying to do this with a shortcode in my functions.php. The list appears well. But when I want to place it in my theme in any page, it appears before the content whereas I want it to be at the bottom of the page of its appropriate page template. I can't seem to find my mistake

    function comite_shortcode( $atts ){
        $contributor_member = array(
             'role'    => 'Contributor',
             'orderby' => 'user_nicename',
             'order'   => 'ASC',
             'number'  => 2,
        );
        $authors = get_users($contributor_member);
            foreach ($authors as $users):
                echo '<div class="column-member">';
                    echo '<div class="info-member">';
                        echo '<span class="comite-description-name">'.$users->display_name.'</span>';
                        echo '<span class="comite-description">'.$users->institution_rattachementUser.'</span>';
                        echo '<span class="comite-description">'.$users->fonctionUser.'</span>';
                        echo '<span class="comite-description">'.$users->responsableUser.'</span>';
                    echo'</div>';       
                echo'</div>';           
                 echo'<hr class="separator-member">';
            endforeach;
            $html = ob_get_contents();
            return $html; // On renvoie le contenu }
add_shortcode( 'comite', 'comite_shortcode' );

I have created a list of my contributors for my website it works well. I'm trying to do this with a shortcode in my functions.php. The list appears well. But when I want to place it in my theme in any page, it appears before the content whereas I want it to be at the bottom of the page of its appropriate page template. I can't seem to find my mistake

    function comite_shortcode( $atts ){
        $contributor_member = array(
             'role'    => 'Contributor',
             'orderby' => 'user_nicename',
             'order'   => 'ASC',
             'number'  => 2,
        );
        $authors = get_users($contributor_member);
            foreach ($authors as $users):
                echo '<div class="column-member">';
                    echo '<div class="info-member">';
                        echo '<span class="comite-description-name">'.$users->display_name.'</span>';
                        echo '<span class="comite-description">'.$users->institution_rattachementUser.'</span>';
                        echo '<span class="comite-description">'.$users->fonctionUser.'</span>';
                        echo '<span class="comite-description">'.$users->responsableUser.'</span>';
                    echo'</div>';       
                echo'</div>';           
                 echo'<hr class="separator-member">';
            endforeach;
            $html = ob_get_contents();
            return $html; // On renvoie le contenu }
add_shortcode( 'comite', 'comite_shortcode' );
Share Improve this question edited Oct 5, 2020 at 18:16 Tom J Nowell 61k7 gold badges79 silver badges148 bronze badges asked Oct 5, 2020 at 18:00 DimDim 31 bronze badge 3
  • I noticed you called ob_get_contents to close your output buffer but where did you start it? I can't find an ob_start call in your shortcodes function – Tom J Nowell Commented Oct 5, 2020 at 18:17
  • Ah exactly thank, it works better now. But I don't understand why it appears in two places on the page. at the bottom of the page (normal position) and at the top of the page (not normal). Where could it come from? – Dim Commented Oct 5, 2020 at 18:38
  • because without ob_start it's outputting directly to the browser, which is broken behaviour. But it appears ob_get_contents still works despite this – Tom J Nowell Commented Oct 5, 2020 at 19:01
Add a comment  | 

1 Answer 1

Reset to default 0

This is because output buffers are closed, but they're never opened.

Adding this at the beginning of the shortcode will fix things:

ob_start();
发布评论

评论列表(0)

  1. 暂无评论