【发布时间】:2019-02-02 19:15:29
【问题描述】:
我正在开发我的第一个 Wordpress 插件,这很有趣。
我使用 echo 显示 HTML,但它显示在页面顶部。做了一些挖掘,我发现我应该在函数中使用“return”。切换到“返回”会导致其他问题。看起来我试图输出的字符串 () 被“return”破坏了。我可以使用 echo 输出字符串并且它可以工作(只是在错误的地方)。
有什么想法吗?
function v3grid_func($atts) {
// Get the parameters from the shortcode
$atts = shortcode_atts(
array(
'count' => 3,
'link_to_all' => 'yes',
'css_class' => 'v3grid',
'categories' => '1'
), $atts
);
// Get list of recent posts
$args = array(
'numberposts' => $atts['count'],
'offset' => 0,
'category' => 0,
'orderby' => 'post_date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' =>'',
//'post_type' => array('post', 'podcast', 'recipe', 'roundup'),
'post_type' => array('roundup'),
'post_status' => 'publish',
'suppress_filters' => true
);
$recent_posts = wp_get_recent_posts( $args, ARRAY_A );
// Figure out width requirement for each grid item
$width = 100/$atts[count];
$output = '';
// Container for row start
$output .= 'Start -> <div style="margin:0 -10px 0 -10px; box-sizing: border-box; position:relative; display:block;">';
// Loop through posts
foreach( $recent_posts as $recent ){
// Get the URL of the feature image for the post
$thumbnail = htmlentities(get_the_post_thumbnail_url( $recent["ID"], 'full' ));
$url = htmlentities(get_permalink($recent["ID"]));
$title = htmlentities($recent["post_title"]);
$output .= ' 1 - <div style="width:'.$width.'%; float:left; padding:10px; display:block;">';
$output .= ' 2 - <div><img src="'.$thumbnail.'" style="display:block; width:100%;"></div>'; // <-- Problem likely here
$output .= ' 3 - <div><a href="'.$url.'">'.$title.'</a></div>';
$output .= ' 4 - </div>';
}
// Container for row end
$output .= 'End -> </div>';
echo $output; // Shows everything as expected
return $output; // Does not show everything as expected (need to use return)
wp_reset_query();
}
add_shortcode('v3grid', 'v3grid_func');
这里是 echo 和 return 的输出。我添加了开始、结束和数字,以便跟踪输出。认为这是我的foreach的问题。
在函数中使用“echo”的输出 - 输出很好。
Start -> <div style="margin:0 -10px 0 -10px; box-sizing: border-box; position:relative; display:block;">
1 - <div style="width:25%; float:left; padding:10px; display:block;">
2 - <div><img src="/wp-content/uploads/THUMBNAIL-150x150.jpg" style="display:block; width:100%;"></div>
3 - <div><a href="/roundup/post1/">Post Title A</a></div>
4 - </div>
1 - <div style="width:25%; float:left; padding:10px; display:block;">
2 - <div><img src="/wp-content/uploads/THUMBNAIL-1-150x150.jpg" style="display:block; width:100%;"></div>
3 - <div><a href="/roundup/post2/">Post Title B</a></div>
4 - </div>
1 - <div style="width:25%; float:left; padding:10px; display:block;">
2 - <div><img src="/wp-content/uploads/Thumbnail-Image-150x150.gif" style="display:block; width:100%;"></div>
3 - <div><a href="/roundup/post3/">Post Title C</a></div>
4 - </div>
1 - <div style="width:25%; float:left; padding:10px; display:block;">
2 - <div><img src="/wp-content/uploads/SnacksTHUMBNAIL-150x150.jpg" style="display:block; width:100%;"></div>
3 - <div><a href="/roundup/post4/">Post Title D</a></div>
4 - </div>
End -> </div>
如果在函数中使用“return”,则输出 - 输出错误/损坏
开始 ->
1 - <div style="width:25%; float:left; padding:10px; display:block;">
2 - <div><img src="/wp-content/uploads/THUMBNAIL-150x150.jpg" style="display:block; width:100%;"></div>
3 - <div></div>
3 - <div></div>
3 - <div><a href="/roundup/post3/">Post Title C</div>
4 - </div>
1 - <div style="width:25%; float:left; padding:10px; display:block;">
2 - <div><img src="/wp-content/uploads/SnacksTHUMBNAIL-150x150.jpg" style="display:block; width:100%;"></div>
3 - <div><a href="/roundup/post4/">Post Title D</div>
4 - </div>
End -> </div>
【问题讨论】:
-
方法
wp_reset_query();有必要吗?因为 return 会阻止/停止执行,而 echo 不会。 -
嗨@GabrielHeming 感谢您的回复。我看起来不需要 wp_query_reset -- 使用 WP_Query 或 get_posts 后不需要调用 wp_reset_query,因为它们不会修改主查询对象。而是使用 wp_reset_postdata - 我将其替换为 wp_reset_postdata,但它并没有“解决”问题。
-
get_the_post_thumbnail() 可能是罪魁祸首,因为如果无法获得正确的缩略图 ID,它可能会返回 false。在连接之前,您应该先检查它是否返回了一个字符串。