php - 更改 WordPress 循环以显示类别
问题描述
我有一个简单的 WordPress 循环来获取 100 个最新帖子。如何更改此设置以根据我所在的类别动态显示帖子?
<?php
// the query
$wpb_all_query = new WP_Query(array(
'post_type' => 'post',
'post_status' => 'publish',
'post-no' => 100));
?>
<?php if ($wpb_all_query->have_posts()) :
while ($wpb_all_query->have_posts()) : $wpb_all_query->the_post(); ?>
<img class="card-img-top bottom-line mb-2 lozad "
data-src="<?php the_post_thumbnail_url(); ?>"
alt="<?php the_title(); ?>"/>
<?php endwhile; ?>
<?php else : ?>
解决方案
如果您的意思是您在类别存档页面上,那么此代码将起作用:
// gets the category information from the archive page
$cat = get_the_category();
// the query
$wpb_all_query = new WP_Query(array(
'post_type' => 'post',
'post_status' => 'publish',
// queries for posts that have that category
'cat' => $cat[0]->cat_ID,
'post-no' => 100));
推荐阅读
- javascript - 访问嵌套数组属性
- cordova - 文件传输插件在上传视频和从服务器下载时表现异步
- python - Matplotlib 字体常用字体与 LaTeX
- c++ - c++ 稍后保留指令
- macos - Flutter Mac OS Build Error 我该如何解决这个错误?
- r - R:加入两个数据帧会导致来自第二个数据帧的所有匹配列的 NA
- windows-server - 如何从命令/powershell 执行 WindowsServer2019 的 setup.exe 安装程序?
- graphics - ImageMagick -fuzz OR 重心裁剪与适合可扩展纵横比框的东西
- javascript - 与 Axios 反应无法将数组映射到列表
- mongodb - 想知道 MongoDB 与 Firebase 和 Flutter 的安全性