【问题标题】:Second shortcode generated div inside the first shortcode div第二个简码在第一个简码 div 中生成 div
【发布时间】:2017-12-08 16:24:42
【问题描述】:

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

问题:当我将两个简码(包括博客文章循环)添加到一个页面时,简码将自己植入第一个简码外部 div 中,这也发生在我的一些 php 代码中,也许您可​​以通过发现错误?

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

https://ibb.co/jB352w

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

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

我的简码.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');

【问题讨论】:

  • 看起来你缺少结束
    标签

标签: php wordpress shortcode markup genesis


【解决方案1】:

短代码仅在您返回输出时有效。

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

您正在执行的代码中的第二个错误是在 while 循环内“Row div close”,而您在 while 循环外启动 row div。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-04
    • 1970-01-01
    • 2015-06-02
    • 1970-01-01
    • 2022-08-13
    • 2013-09-20
    • 1970-01-01
    相关资源
    最近更新 更多