Как вложить оператор else if в PHP или есть лучший способ справиться с этим? - PullRequest
0 голосов
/ 09 марта 2020

Так что мой код частично работает. Я могу позвонить по правильному идентификатору категории и вытащить правильное изображение, НО, когда я go перехожу на следующую страницу в нумерации страниц, изображения категории не отображаются, пока я не нажму refre sh. Я не думаю, что правильно делаю свои вложенные операторы 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');

Это мой обновленный код, который теперь получает синтаксические ошибки перед вторым, третьим оператором 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 Ответ

0 голосов
/ 10 марта 2020

Просто исправление синтаксиса:

 // 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');
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...