php - 短代码出现在内容顶部
问题描述
我目前正在尝试向我的 Wordpress 网站添加一个简码,但目前它显示在页面顶部,而不是我放置的位置。
除了这个问题之外,一切都运行良好。
// function that runs when shortcode is called
function testimonial_short() {
if( get_query_var( 'page' ) ) {
$page = get_query_var( 'page' );
} else {
$page = 1;
}
$args = array(
'post_type' => 'drivers',
'posts_per_page' => 4,
'paged' => $page
);
$products = new WP_Query( $args );
?>
<div class="owl-carousel owl-theme">
<div class="testimonial-outer">
<div class="image-col">
<img class="testimonial-img" src="<?php echo get_field('image');?>">
</div>
<!-- image-col -->
<div class="text-col">
<h4>
<?php echo get_field('name');?>
</h4>
<p>
<?php echo get_field('testimonial');?>
</p>
</div>
<!-- text-col -->
</div>
<!-- testimonial-outer -->
</div>
<?php
}
// register shortcode
add_shortcode('testimonial', 'testimonial_short');
解决方案
您的testimonial_short
函数不是返回 HTML,而是立即输出它。这导致它出现在页面顶部,因为在尚未发送任何内容的情况下评估了短代码。
尝试:
function testimonial_short(){
...
return "<div>my html</div>";
}
推荐阅读
- spark-ar-studio - 使用 Spark Ar 跟踪图像成功后播放视频
- c++ - 函数如何读取指针?
- tensorflow - 在 keras 中创建自定义回调时无法理解部分概念
- python - ImportError:没有名为 yaml 的模块 - 但我有 yaml
- authentication - 为什么没有调用回调标识符?
- javascript - 为 reactjs 中的项目添加唯一键
- css - Zurb Foundation Flexbox 6.6.3 & Flot/Flex Grid 不工作
- istio - Istio 重试不起作用
- python - Tkinter 和 32 位 Unicode 复制——有什么解决办法吗?
- android - 颤振导入'package:flutter_svg/flutter_svg.dart';不适用于 VS 代码和 Android 工作室