如何将帖子保存为临时内容?

问题描述 投票:0回答:1

我的代码:

<?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输出从此循环保存为瞬态?

wordpress transient
1个回答
0
投票

虽然我不确定您的包含内容是什么,但是您遇到的第一个问题是您无法将输出回显到想要保存为瞬态的字符串。您必须将$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;
© www.soinside.com 2019 - 2024. All rights reserved.