Wordpress短代码始终位于内容的顶部



无论它放在页面中的什么位置,这个短代码都始终位于内容的顶部,我如何才能让它显示在它的位置?

如前几篇文章中所述,我曾尝试用return替换echo语句,但它破坏了脚本。

快捷代码从每个产品属性项中提取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');

如果没有输出缓冲,就无法回显短代码输出。您必须RETURN短码输出。

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);
ob_start(); // Use Output Buffering for your shortcode
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
}
}
}
return ob_get_clean(); // You have to return shortcode output.
}
add_shortcode('navprodealimit', 'navigationProdFromEachLimit');

最新更新