gpt4 book ai didi

php - 如何在 wordpress query_post 函数中排除没有图像的帖子?

转载 作者:行者123 更新时间:2023-11-29 03:11:10 24 4
gpt4 key购买 nike

下面的查询在我的网站上运行一个特色轮播。我希望轮播仅显示包含图像的帖子。我到处搜索,找不到解决方案。请帮忙!

query_posts(array('post_type' => 'ad_listing', 'post_status' => 'publish', 'orderby' => 'rand'));

最佳答案

我在开发自己的主题时遇到了同样的事情。我就是这样解决的。

首先在您的 functions.php 中添加特色图片功能。

if (function_exists('add_theme_support')) {
add_theme_support('post-thumbnails');
}

这允许您为每个帖子选择特色图片。

有了特色图片功能,您可以使用以下功能来检测帖子是否有特色图片...以循环形式:

$args = array(
'post_type' => 'ad_listing'
);
query_posts($args);
if ( have_posts() ) :
while ( have_posts() ) : the_post();
if ( has_post_thumbnail() ) { //For featured images
//For post images/attachments
ob_start();
the_id();
$postID = ob_get_clean();
$args = array(
'numberposts' => 1,
'order'=> 'ASC',
'post_mime_type' => 'image',
'post_parent' => $postID,
'post_status' => null,
'post_type' => 'attachment'
);
$images =& get_children($args);

if ( empty($images) ) {
//What to do without images
}
else {
//What to do with images
}
}
endwhile;
else :
//What happens when no posts are found
endif;

希望这对您有所帮助。

关于php - 如何在 wordpress query_post 函数中排除没有图像的帖子?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8159393/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com