如何在PHP中嵌套else if语句,或者有更好的方法来处理这个问题?
所以我的代码部分工作正常。我能够调用正确的类别ID并拉取正确的图像,但当我转到分页的下一页时,类别图像直到我点击刷新才会显示。我认为我没有正确地使用嵌套的if-else语句,我尝试了各种组合,但都无济于事。我正在尝试根据事件类别分配特征图像,如果该事件没有设置特征图像。这是我的密码。任何帮助都将不胜感激如何在PHP中嵌套else if语句,或者有更好的方法来处理这个问题?,php,wordpress,function,Php,Wordpress,Function,所以我的代码部分工作正常。我能够调用正确的类别ID并拉取正确的图像,但当我转到分页的下一页时,类别图像直到我点击刷新才会显示。我认为我没有正确地使用嵌套的if-else语句,我尝试了各种组合,但都无济于事。我正在尝试根据事件类别分配特征图像,如果该事件没有设置特征图像。这是我的密码。任何帮助都将不胜感激 // Featured Image for Certain Event Categories function my_default_featured_images() { global
// 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');
只是修复语法:
//特定事件类别的特色图像
函数my_default_featured_images(){
全球$员额;
$featured\u image\u exists=具有\u post\u缩略图($post->ID);
如果(!$featured\u image\u存在){
$attached_image=get_children(“post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1”);
}
如果($attached_image){
foreach($attached_image as$attachment_id=>$attachment){
设置发布缩略图($post->ID,$attachment);
}
wp_reset_postdata();//这是一个函数,因此需要()
}
//确定应适用的类别。
else if(tribe_是_事件($post->ID)){//在条件末尾添加“{”
if(部落活动(属于“慈善筹款”)类别){
设置_post_缩略图($post->ID,'32766');//将'0000'更改为您想要的任何图像的ID作为默认值
}//如果“慈善筹款”结束
else if(部落事件在部落类别中('trunk shows'){//删除了另一个部落事件,而不是-if/else作为部落类别中的部落事件
设置_post_缩略图($post->ID,'32768');//将'0000'更改为您想要的任何图像的ID作为默认值
}//如果“中继显示”,则结束
else if(部落活动属于表演艺术类){
设置\u post\u缩略图($post->ID,'32828');//将'0000'更改为您想要的任何图像的ID作为默认值
}//如果“表演艺术”结束
}//如果部落是事件,则结束
}
添加动作(“帖子”、“我的默认图片”特色图片);
1.在之后的第二个{
如果
关闭了你的条件…请编辑你的代码。2.请尝试在第一个foreach
循环中进行wp\u reset\u postdata();
操作。我想这就是你的意思。我仍然得到相同的结果。语法在visual studio代码中没有验证,我添加了一个}在“添加”操作之前,当添加到functions.php时,它会破坏网站。@Mike,我刚刚编辑了它。我在结尾中缺少了一个}!特征图像存在
条件