Wordpress - 设置默认的特色图像'对于每个类别

时间:2014-11-13 13:32:53

标签: wordpress

我想为每个类别设置默认的“特色图片”。

在我的functions.php文件中,我有以下代码: -

/* ---------------FEATURED POST IMAGE------------------------*/
function default_category_featured_image() {
global $post;
$featured_image_exists = has_post_thumbnail($post->ID);
if (!$featured_image_exists) {
$attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" );

if ($attached_image) {
foreach ($attached_image as $attachment_id => $attachment) {
set_post_thumbnail($post->ID, $attachment);
}}
else if ( in_category('2') ) {
set_post_thumbnail($post->ID, '112');
}
else if ( in_category('3') ) {
set_post_thumbnail($post->ID, '115');
}
else if ( in_category('4') ) {
set_post_thumbnail($post->ID, '113');
}
else if ( in_category('8') ) {
set_post_thumbnail($post->ID, '114');
}
else {
set_post_thumbnail($post->ID, '0');
}
}
}
add_action('the_post', 'default_category_featured_image');

除Wordpress外,其他大多数类别都适用。一分钟有6个帖子以“Wordpress”作为类别,他们都使用类别ID“3”,但由于某些未知原因,一些以“Wordpress”作为类别的帖子都有默认的特色图片设置,哪里有几个没有任何默认图像集?

知道为什么会发生这种情况: -

你可以在这里看到问题: -

http://www.web-tricks.co.uk

在主页上,您可以看到“如何增加WordPress内存限制”标题,以及“Web-Tricks WordPress十大最佳插件”现在,但它们具有相同的类别ID - 任何想法?

1 个答案:

答案 0 :(得分:0)

您可以为每个类别创建一个新模板,并执行分配该类别的WP_Query。

然后在循环中,你可以说是否有特色图像,显示,否则显示我的图像。

如果这是有道理的。如果没有,我会尽快发布一个例子。我正忙着:P

相关问题