在第一个简码 div 内生成第二个简码 div

Second shortcode generated div inside the first shortcode div

我正在尝试将多个短代码生成博客 post 循环添加到一页

问题:当我将包括博客 post 循环在内的两个简码添加到一个页面时,简码将自身植入到第一个简码外部 div 中,这发生在我的一些 php代码也是如此,也许您可​​以通过发现错误来挽救一天?

在这里你会看到,第二行(这是短代码的第二个循环开始的地方,行更宽,这基本上就是问题所在):

https://ibb.co/jB352w

如果我要添加另一个具有相同短代码构建的循环,它将比第二个更宽,依此类推..

html 输出,在这里你可以看到第二个循环已经将自己植入到第一个循环 "row" 中。 https://ibb.co/h7pOpb

我的shortcode.php:

<?php

$city = the_terms( $post->ID , "'.$city.'");

/*** HOME ***/
function blog_loop_mtl( $atts ) {
    extract( shortcode_atts( array(
                                    'type' => 'post',
                                    'perpage' => 20,
                                    'city'   => 'Montreal'
                                  ), $atts ) );
      echo '<div class="clear"></div>';// Outter Container open
                      $args = array(
                                    'post_type' => $type,
                                    'posts_per_page' => $perpage,
                                    'city'   => $city
                                    );
                      $splendid_query = new  WP_Query( $args );

    echo '<div class="row">';// Row Open

    while ( $splendid_query->have_posts() ) : $splendid_query->the_post();
      $category = get_the_category();
      echo           '<div class="col-xs-6 col-sm-4 grid-entry-wrapper"> <!-- grid-entry-wrapper open -->



                            <div class="post_grid_entry">

                                <div id="grid_entry_meta">

                                      <div class="boujee">

                                          <a href="' . get_category_link($cats[0]->cat_ID) . '" style="color: white">' . $category[0]->cat_name . '</a>

                                      </div>
                                      <div>
                                        ' . $city . ' <i class="fa fa-map-marker" aria-hidden="true"></i>
                                      </div>

                                </div>
                                <a href="' . get_permalink() . '">
                                  <div class="grid_thumbnail" >
                                  <div class="grid_thumbnail" style="background-image: url('.get_the_post_thumbnail_url().')" alt="">
                                  </div>
                                </a>
                                <a href="'.get_author_posts_url( get_current_user_id()). '">'. get_avatar(get_the_author_meta( 'id' )) . '</a>
                            </div>
                            <div>
                              <a href="' . get_permalink() . '"></a>
                                <h3 class="post_grid_title">
                                  <a href="' . get_permalink() . '">'. get_the_title(). '</a>
                                </h3>
                            </div>
                            <div id="grid_entry_meta_publ">
                              <div>
                                Published ' . time_elapsed_string(get_the_date()). '
                              </div>
                              <div>
                                by <a href="'.get_author_posts_url(get_the_author_meta( 'id' )). '">'. get_the_author_meta( 'display_name' ) . '</a>
                              </div>
                            </div>

                      </div><!-- grid-entry-wrapper close -->
          </div>'; // Row Close

    endwhile;
    wp_reset_query();



}
add_shortcode('blog_loop_mtl', 'blog_loop_mtl');

简码仅在您 return 输出时有效。

<?php

$city = the_terms( $post->ID , "'.$city.'");

/*** HOME ***/
function blog_loop_mtl( $atts ) {
    extract( shortcode_atts( array(
                                    'type' => 'post',
                                    'perpage' => 20,
                                    'city'   => 'Montreal'
                                  ), $atts ) );
      echo '<div class="clear"></div>';// Outter Container open
                      $args = array(
                                    'post_type' => $type,
                                    'posts_per_page' => $perpage,
                                    'city'   => $city
                                    );
                      $splendid_query = new  WP_Query( $args );

    $output = '<div class="row">';// Row Open

    while ( $splendid_query->have_posts() ) : $splendid_query->the_post();
      $category = get_the_category();
      $output .=           '<div class="col-xs-6 col-sm-4 grid-entry-wrapper"> <!-- grid-entry-wrapper open -->



                            <div class="post_grid_entry">

                                <div id="grid_entry_meta">

                                      <div class="boujee">

                                          <a href="' . get_category_link($cats[0]->cat_ID) . '" style="color: white">' . $category[0]->cat_name . '</a>

                                      </div>
                                      <div>
                                        ' . $city . ' <i class="fa fa-map-marker" aria-hidden="true"></i>
                                      </div>

                                </div>
                                <a href="' . get_permalink() . '">
                                  <div class="grid_thumbnail" >
                                    <div class="grid_thumbnail" style="background-image: url('.get_the_post_thumbnail_url().')" alt=""></div>
                                  </div>
                                </a>
                                <a href="'.get_author_posts_url( get_current_user_id()). '">'. get_avatar(get_the_author_meta( 'id' )) . '</a>
                            </div>
                            <div>
                              <a href="' . get_permalink() . '"></a>
                                <h3 class="post_grid_title">
                                  <a href="' . get_permalink() . '">'. get_the_title(). '</a>
                                </h3>
                            </div>
                            <div id="grid_entry_meta_publ">
                              <div>
                                Published ' . time_elapsed_string(get_the_date()). '
                              </div>
                              <div>
                                by <a href="'.get_author_posts_url(get_the_author_meta( 'id' )). '">'. get_the_author_meta( 'display_name' ) . '</a>
                              </div>
                            </div>

                      </div><!-- grid-entry-wrapper close -->';


    endwhile;
    wp_reset_query();

    $output .= '</div>'; // Row Close

    return $output;
}
add_shortcode('blog_loop_mtl', 'blog_loop_mtl');

您执行的代码中的第二个错误是 "Row div close" 在 while 循环内,而您在 while 循环外开始行 div。