我的代码:
<?php if (!empty($packs = get_post_meta(get_the_ID(), 'pack', true))):?>
<?php
if ( false === ( $q = get_transient( 'packs_list' ) ) ) {
$params = array(
'post_type' => 'product',
'posts_per_page' => '7',
'meta_query' => array(
array(
'key' => 'package_pack',
'value' => $packs,
'compare' => 'IN'
)
)
);
$wp_query = new WP_Query($params);
echo '<div class="products list_">';
while ($wp_query->have_posts()) : $wp_query->the_post();
$q = include(rh_locate_template('inc/parts/main.php'));
endwhile;
echo '</div>';
set_transient( 'packs_list', $q, 1 * HOUR_IN_SECONDS );
wp_reset_postdata();
}
endif;?>
我正在尝试将整个生成的帖子列表保存为html瞬态,但是不起作用。如何将HTML输出从此循环保存为瞬态?
虽然我不确定您的包含内容是什么,但是您遇到的第一个问题是您无法将输出回显到想要保存为瞬态的字符串。您必须将$q
连接到包含所有包含的HTML输出的长字符串中。
此外,您可能想使用输出缓冲来获取所包含文件模板的内容。
if (!empty($packs = get_post_meta(get_the_ID(), 'pack', true))):
if ( false === ( $q = get_transient( 'packs_list' ) ) ) {
$params = array(
'post_type' => 'product',
'posts_per_page' => '7',
'meta_query' => array(
array(
'key' => 'package_pack',
'value' => $packs,
'compare' => 'IN'
)
)
);
$wp_query = new WP_Query($params);
$q = '<div class="products list_">';
while ($wp_query->have_posts()) : $wp_query->the_post();
ob_start();
include(rh_locate_template('inc/parts/main.php'));
$q .= ob_get_clean();
endwhile;
$q .= '</div>';
set_transient( 'packs_list', $q, 1 * HOUR_IN_SECONDS );
wp_reset_postdata();
}
endif;