【问题标题】:How to nest an else if statement in PHP or is there a better way to handle this?如何在 PHP 中嵌套 else if 语句,或者有更好的方法来处理这个?
【发布时间】: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();
  • 我想这就是你的意思。我仍然得到相同的结果。

标签: php wordpress function


【解决方案1】:

只是修正语法:

 // 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(); // This is a function so theres a need for ()
     }
    // Identify the category this should apply to.
    else if ( tribe_is_event( $post->ID ) ) { // Adding '{' at the end of condition
         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
         } // End if 'charity-fundraising'

         else if  ( tribe_event_in_category( 'trunk-shows' ) ){ // Removed another if tribe_is_event. instead - if/else for tribe_event_in_cat 
            set_post_thumbnail( $post->ID, '32768' ); // Change '0000' to the ID of whatever image you want as a default
         } // End if 'trunk-shows'

         else 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
         } // End if 'performing-arts'

    } //End if tribe is event

}

add_action('the_post', 'my_default_featured_images');

【讨论】:

  • 语法没有在 Visual Studio 代码中验证,我在 add_action 之前添加了一个 },它在添加到 functions.php 时会破坏网站。
  • @Mike 我刚刚编辑了那个。我错过了结束 !featured_image_exists 条件的 }
猜你喜欢
  • 2016-07-11
  • 1970-01-01
  • 1970-01-01
  • 2018-09-14
  • 2022-06-19
  • 2023-03-27
  • 1970-01-01
  • 2022-11-01
  • 2017-07-15
相关资源
最近更新 更多