Wordpress 简码始终位于内容的顶部

这个短代码总是在内容的顶部,无论它放在页面中的什么位置,我怎样才能让它显示在它的位置?


我试图用之前帖子中描述的替换echo语句,但它破坏了脚本。return


简码从每个产品属性术语中提取 1 个产品,然后显示产品图像和属性术语名称。


function navigationProdFromEachLimit() {

$factwpslug = "/charts/?fwp_chart_type=";

$number = '5'; //LIMIT THE NUMBER OF RETURNED CHART TYPES


$args = array(


'number'     => $number,

'orderby'    => 'count',

'order'      => 'DESC',

'hide_empty' => false,

'include'    => $ids,

);

$product_categories = get_terms( 'pa_chart-type', $args );

 $count = count($product_categories);

if ( $count > 0 ){

foreach ( $product_categories as $product_category ) {

    $args = array(

        'posts_per_page' => 1,

        'tax_query' => array(

            'relation' => 'AND',

            array(

                'taxonomy' => 'pa_chart-type',

                'field' => 'slug',

                'terms' => $product_category->slug

            )

        ),

        'post_type' => 'product',

        'orderby' => 'title,'

    );

    $products = new WP_Query( $args );

    while ( $products->have_posts() ) {

        $products->the_post();


        ?>

            <div class ="chart-type-nav <?php echo $product_category->slug ?>">


                <a class="nav-thumb" href="<?php echo $factwpslug  . $product_category->slug ?>">

                   <div class ="img-contain"> <?php the_post_thumbnail(); ?></div>

                </a>

                  <?php

             echo '<span class="nav-title"><a href="'.$factwpslug  . $product_category->slug . '">' . $product_category->name . '</a></span>';

 ?>

            </div>

        <?php


    }

}

}

}

add_shortcode('navprodealimit', 'navigationProdFromEachLimit');


慕婉清6462132
浏览 99回答 1
1回答

慕的地10843

如果没有输出缓冲,您将无法回显短代码输出。您必须RETURN使用简码输出。function navigationProdFromEachLimit() {$factwpslug = "/charts/?fwp_chart_type=";$number = '5'; //LIMIT THE NUMBER OF RETURNED CHART TYPES$args = array('number'&nbsp; &nbsp; &nbsp;=> $number,'orderby'&nbsp; &nbsp; => 'count','order'&nbsp; &nbsp; &nbsp; => 'DESC','hide_empty' => false,'include'&nbsp; &nbsp; => $ids,);$product_categories = get_terms( 'pa_chart-type', $args );$count = count($product_categories);ob_start(); // Use Output Buffering for your shortcodeif ( $count > 0 ){foreach ( $product_categories as $product_category ) {&nbsp; &nbsp; $args = array(&nbsp; &nbsp; &nbsp; &nbsp; 'posts_per_page' => 1,&nbsp; &nbsp; &nbsp; &nbsp; 'tax_query' => array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'relation' => 'AND',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'taxonomy' => 'pa_chart-type',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'field' => 'slug',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'terms' => $product_category->slug&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; ),&nbsp; &nbsp; &nbsp; &nbsp; 'post_type' => 'product',&nbsp; &nbsp; &nbsp; &nbsp; 'orderby' => 'title,'&nbsp; &nbsp; );&nbsp; &nbsp; $products = new WP_Query( $args );&nbsp; &nbsp; while ( $products->have_posts() ) {&nbsp; &nbsp; &nbsp; &nbsp; $products->the_post();&nbsp; &nbsp; &nbsp; &nbsp; ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class ="chart-type-nav <?php echo $product_category->slug ?>">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a class="nav-thumb" href="<?php echo $factwpslug&nbsp; . $product_category->slug ?>">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<div class ="img-contain"> <?php the_post_thumbnail(); ?></div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;echo '<span class="nav-title"><a href="'.$factwpslug&nbsp; . $product_category->slug . '">' . $product_category->name . '</a></span>';&nbsp;?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return ob_get_clean(); // You have to return shortcode output.}add_shortcode('navprodealimit', 'navigationProdFromEachLimit');
打开App,查看更多内容
随时随地看视频慕课网APP