【发布时间】:2020-03-09 17:48:56
【问题描述】:
所以我的代码部分工作。我可以调用正确的类别 ID 并提取正确的图像,但是当我转到分页的下一页时,类别图像在我点击刷新之前不会显示。我认为我没有正确执行嵌套的 if else 语句我尝试了各种组合但无济于事。如果该活动没有设置特色图片,我正在尝试根据活动类别分配特色图片。这是我的代码。任何帮助,将不胜感激。
// Featured Image for Certain Event Categories
function my_default_featured_images() {
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);
}
}
// Identify the category this should apply to.
else if ( tribe_is_event( $post->ID ) ) {
if ( tribe_event_in_category( 'charity-fundraising' ) ) {
set_post_thumbnail( $post->ID, '32766' ); // Change '0000' to the ID of whatever image you want as a default
} else if ( tribe_is_event( $post->ID ) ) {
} if ( tribe_event_in_category( 'trunk-shows' ) ) {
set_post_thumbnail( $post->ID, '32768' ); // Change '0000' to the ID of whatever image you want as a default
} else if ( tribe_is_event( $post->ID ) ) {
} if ( tribe_event_in_category( 'performing-arts' ) ) {
set_post_thumbnail( $post->ID, '32828' ); // Change '0000' to the ID of whatever image you want as a default
wp_reset_postdata();}
}}}
add_action('the_post', 'my_default_featured_images');
这是我更新的代码,现在在第二个、第三个 else if 语句之前出现语法错误。我仍在学习propper语法,所以我不确定如何解决这个问题。
// Featured Image for Certain Event Categories
function my_default_featured_images() {
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);
wp_reset_postdata;
}
}
// Identify the category this should apply to.
else if ( tribe_is_event( $post->ID ) )
if ( tribe_event_in_category( 'charity-fundraising' ) )
set_post_thumbnail( $post->ID, '32766' ); // Change '0000' to the ID of whatever image you want as a default
} else if ( tribe_is_event( $post->ID ) )
if ( tribe_event_in_category( 'trunk-shows' ) )
set_post_thumbnail( $post->ID, '32768' ); // Change '0000' to the ID of whatever image you want as a default
else if ( tribe_is_event( $post->ID ) )
if ( tribe_event_in_category( 'performing-arts' ) )
set_post_thumbnail( $post->ID, '32828' ); // Change '0000' to the ID of whatever image you want as a default
}
add_action('the_post', 'my_default_featured_images');
【问题讨论】:
-
1.
else ifs 关闭您的条件之后的第二个{... 请编辑您的代码。 2.请在第一个foreach循环中尝试wp_reset_postdata();。 -
我想这就是你的意思。我仍然得到相同的结果。