我用它来显示所有类别中的所有帖子。
$args_cat = array(
// order by category name ascending
\'orderby\' => \'name\',
\'order\' => \'ASC\',
// get only top level categories
\'parent\' => 0
);
$categories = get_categories($args_cat);
// Full posts query
// if there are categories filled with posts
if (!empty ($categories) && !is_wp_error( $categories )) {
foreach ($categories as $category) {
// Query all posts by slug inside each category
$args_category_posts = array(
\'post_type\' => \'post\',
// The category slug and category name we get from the foreach over all categories
\'category_name\' => $category->slug
);
$query = new WP_Query($args_category_posts);
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post(); ?>
<article class="<?php echo $category->slug ?>-article">
<h2 class="<?php echo $category->slug ?>-article-title">
<a href="<?php echo get_permalink() ?>"><?php echo get_the_title() ?></a>
</h2>
<p class="<?php echo $category->slug ?>-post-info">
<?php the_time(\'d. m. Y\') ?>
</p>
<div <?php post_class() ?> >
<?php the_content(); ?>
</div>
</article> <?php
}
} // end loop
} // end foreach
wp_reset_postdata() ;
} // end if there are categories filled with posts
已经阅读
this,
this,
this 和
this 我想知道我是否在做下面的事情。
// Transient API all categories and all posts
$query_categories = get_transient(\'cached_categories\');
if ( false === $query_categories){
$args_cat = array(
// order by category name ascending
\'orderby\' => \'name\',
\'order\' => \'ASC\',
// get only top level categories
\'parent\' => 0
);
// Instead of caching a WP Query I cache \'get_categories($args_cat)\', OK to use it like this?
$query_categories = get_categories($args_cat);
set_transient(\'cached_categories\', $query_categories, DAY_IN_SECONDS );
}
// Full posts query
// if there are categories filled with posts
if (!empty ($query_categories) && !is_wp_error( $query_categories )) {
foreach ($query_categories as $category) {
$query_category_posts = get_transient(\'cached_posts\');
if ( false === $query_category_posts ){
// Query all posts by slug inside each category
$args_category_posts = array(
\'post_type\' => \'post\',
// The category slug and category name we get from the foreach over all categories
\'category_name\' => $category->slug
);
// Here I cache the WP_Query, though this runs for all categories.
// So am I storing multiple transients here?
$query_category_posts = new WP_Query($args_category_posts);
set_transient( \'cached_posts\', $query_category_posts, DAY_IN_SECONDS );
}
if ($query_category_posts->have_posts()) {
while ($query_category_posts->have_posts()) {
$query_category_posts->the_post(); ?>
<article class="<?php echo $category->slug ?>-article">
<h2 class="<?php echo $category->slug ?>-article-title">
<a href="<?php echo get_permalink() ?>"><?php echo get_the_title() ?></a>
</h2>
<p class="<?php echo $category->slug ?>-post-info">
<?php the_time(\'d. m. Y\') ?>
</p>
<div <?php post_class() ?> >
<?php the_content(); ?>
</div>
</article> <?php
}
} // end loop
} // end foreach
wp_reset_postdata() ;
} // end if there are categories filled with posts
输出正常,所有类别和帖子都显示良好,但类别和帖子现在是否已正确缓存?
在代码中,我对设置瞬态的两个地方进行了注释,但不确定这是否可以用于get_categories()
或在foreach
适用于该类别中的所有职位。
Edit
瞬态适用于get_categories()
. 这个var_dump
输出显示了保存可用类别的WP\\u Term对象数组。
$query_categories = get_categories($args_cat);
var_dump($query_categories);
set_transient(\'cached_categories\', $query_categories, DAY_IN_SECONDS );
Edit内部瞬态
foreach
仅获取第一个类别及其帖子,并重复该类别的数量。除第一个类别以外的其他类别不存在,这可能是因为在第一轮
foreach
它已满,因此循环仅显示第一类的帖子。
Now how can I make a new transient that holds the posts per category on every cycle of the foreach
for the number of categories?
对瞬态API完全陌生,并试图充分利用它。尝试调整代码并保持最佳实践,如果这一切都是错误的使用潜伏或没有使用,因为它不工作,请让我知道。如果能给我一些指导或者我已经掌握了使用瞬态的“方法”,我将不胜感激。
Solution
每this answer 我必须为每个类别创建的瞬态提供类别名称(slug)。否则,只有第一个类别存储在瞬态中,一旦第二个循环中的类别已满,则不会更改。因此,只显示第一类的员额。
仅使用$category
因为瞬态变量自$category
是一个对象,需要一个字符串,所以我将其更改为$category->slug
现在它开始工作了。
这是在两个地方完成的,获取和设置瞬态。非常感谢。
// Transients API all categories and all posts
$query_categories = get_transient(\'cached_categories\');
if ( false === $query_categories){
$args_cat = array(
// order by category name ascending
\'orderby\' => \'name\',
\'order\' => \'ASC\',
// get only top level categories
\'parent\' => 0
);
// Instead of caching a WP Query I cache \'get_categories()\'.
$query_categories = get_categories($args_cat);
// var_dump($query_categories);
set_transient(\'cached_categories\', $query_categories, DAY_IN_SECONDS );
}
// Full posts query
// if there are categories filled with posts
if (!empty ($query_categories) && !is_wp_error( $query_categories )) {
foreach ($query_categories as $category) {
// var_dump($category);
$query_category_posts = get_transient(\'cached_posts_\' . $category->slug );
if ( false === $query_category_posts ){
// Query all posts by slug inside each category
$args_category_posts = array(
\'post_type\' => \'post\',
// The category slug and category name we get from the foreach over all categories
\'category_name\' => $category->slug
);
// Here I cache the WP_Query, though this runs for all categories.
// Because of that the \'$category->slug\' is used to serve a string and not an object.
$query_category_posts = new WP_Query($args_category_posts);
set_transient( \'cached_posts_\' . $category->slug , $query_category_posts, DAY_IN_SECONDS );
}
if ($query_category_posts->have_posts()) {
while ($query_category_posts->have_posts()) {
$query_category_posts->the_post(); ?>
<article class="<?php echo $category->slug ?>-article">
<h2 class="<?php echo $category->slug ?>-article-title">
<a href="<?php echo get_permalink() ?>"><?php echo get_the_title() ?></a>
</h2>
<p class="<?php echo $category->slug ?>-post-info">
<?php the_time(\'d. m. Y\') ?>
</p>
<div <?php post_class() ?> >
<?php the_content(); ?>
</div>
</article> <?php
}
} // end loop
} // end foreach
wp_reset_postdata() ;
} // end if there are categories filled with posts