Php 如何在短代码WordPress的循环中使用模板或部分?
为了更好地组织我的代码,我想在模板中拆分一些HTML。 我在短代码中有一个循环来获取帖子:Php 如何在短代码WordPress的循环中使用模板或部分?,php,wordpress,shortcode,Php,Wordpress,Shortcode,为了更好地组织我的代码,我想在模板中拆分一些HTML。 我在短代码中有一个循环来获取帖子: function nh_shortcode_func($atts) { $posts = get_posts_by_category(.......); $html .= ''; foreach ($posts as $post) { $post_id = $post->ID; $post_title = $post->post_title;
function nh_shortcode_func($atts) {
$posts = get_posts_by_category(.......);
$html .= '';
foreach ($posts as $post) {
$post_id = $post->ID;
$post_title = $post->post_title;
$post_image = get_field('image', $post_id);
$html .= **HERE THE CODE OF HTML WITH DIVS AND CLASSES**
}
return $html
}
add_shortcode('nh-content', 'nh_shortcode_func');
相反,将我的HTML代码放在一个变量中,我想使用另一个具有HTML结构的文件来构建帖子。另外,我想将动态数据传递给它
有什么想法吗
谢谢。只要使用包含文件,它就可以访问变量。比如:
function nh_shortcode_func($atts) {
ob_start();
include dirname( __FILE__ ) . '/templates/nh_shortcode.php';
return ob_get_clean();
}
add_shortcode('nh-content', 'nh_shortcode_func');
嘿谢谢但是如何将其包含在我的foreach循环中呢?我需要在$HTML变量中添加外部HTML以返回shortcode函数。Chris,我阅读了更多关于输出缓冲区的内容,这似乎是最好的选择。谢谢很高兴我能帮忙!是的,您的foreach循环将进入包含的文件中