The template has the following code that displays the latest 6 news:

<div class="index-container2 mob-none"> <? $pages = get_posts(array( 'numberposts' => 6, 'category_name' => 'glavnaya', 'order' => 'DESC', 'post_type' => 'post', )); ?> <? $isPush = false; foreach ($pages as $post) { setup_postdata($post); ?> <? $dd = get_the_date('d.m'); $image = get_field('изображение'); $size = get_field('размер_блока_в_новостях'); $bottom_img = '' . get_template_directory_uri() . '/images/proc-grey.png'; //$bg_color = '#bba68f'; $border_grey = ''; if ($size == 'Большая ширина') $size = 'news-body_big'; else $size = 'news-body_standart'; //Определяем цвет фона $temp_color = get_field('шаблон_цвета'); $bg_color = get_field('цвет_фона'); if (!$bg_color) { if ($temp_color == "Серый") $bg_color = "#f1f0f0"; else if ($temp_color == "Белый") $bg_color = "#ffffff"; else if ($temp_color == "Коричневый") $bg_color = "#bba68f"; else $bg_color = "#ffffff"; } //определяем изображение $image_big = get_field('изображение_новости'); $icon = get_field('иконка'); $temp_img = get_field('шаблон_для_изображения'); if ($image_big) { $bottom_img = ''; } else if ($icon) { $bottom_img = $icon['url']; } else if ($temp_img) { if ($temp_img == "Процент") { if ($temp_color == "Серый") $bottom_img = '' . get_template_directory_uri() . '/images/proc-braun.png'; else if ($temp_color == "Белый") $bottom_img = '' . get_template_directory_uri() . '/images/proc-grey.png'; else if ($temp_color == "Коричневый") $bottom_img = '' . get_template_directory_uri() . '/images/proc-white.png'; } else if ($temp_img == "Подарок") { if ($temp_color == "Серый") $bottom_img = '' . get_template_directory_uri() . '/images/present-braun.png'; else if ($temp_color == "Белый") $bottom_img = '' . get_template_directory_uri() . '/images/present-grey.png'; else if ($temp_color == "Коричневый") $bottom_img = '' . get_template_directory_uri() . '/images/present-white.png'; } else if ($temp_img == "Подарок") { if ($temp_color == "Серый") $bottom_img = '' . get_template_directory_uri() . '/images/pack-braun.png'; else if ($temp_color == "Белый") $bottom_img = '' . get_template_directory_uri() . '/images/pack-grey.png'; else if ($temp_color == "Коричневый") $bottom_img = '' . get_template_directory_uri() . '/images/pack-white.png'; } } //делаем рамку, если фон белый и нет картинки if ($bg_color == '#ffffff' && $bottom_img !== '') { $border_grey = ';border: 1.5px solid #f1f0f0;'; } ?> <a href="<?php the_permalink(); ?>" class="news-body <?php echo $size ?>" style="background-color: <?php echo $bg_color; echo $border_grey; ?>;"> <? /*<div class="name"><?php the_field('заголовок'); ?></div> <div class="desc"><?php the_field('описание'); ?></div> <div class="img"><?php if ($bottom_img != '') { ?><img src="<?php echo $bottom_img ?>" alt=""><?php } ?></div>*/ ?> <div class="img2"><?php if ($image_big) { ?><img src="<?php echo $image_big['url']; ?>" alt=""><?php } ?></div> </a> <? $isPush = !$isPush; } ?> <? wp_reset_query(); ?> <div class="button-block"> <button class="load-news" type="button" data-page="1">Ещё новости</button> </div> 

Now I’m trying to load the next 6 news via AJAX, for this I registered a handler function and duplicated the above code in it:

  function load_more_news() { $args = $_POST; $args['page']++; $args['numberposts'] = 6; $args['order'] = 'DESC'; $args['post_type'] = 'post'; $pages = get_posts($args); $isPush = false; foreach ($pages as $post) { setup_postdata($post); $dd = get_the_date('d.m'); $image = get_field('изображение'); var_dump($post); $size = get_field('размер_блока_в_новостях'); $bottom_img = '' . get_template_directory_uri() . '/images/proc-grey.png'; //$bg_color = '#bba68f'; $border_grey = ''; if ($size == 'Большая ширина') $size = 'news-body_big'; else $size = 'news-body_standart'; //Определяем цвет фона $temp_color = get_field('шаблон_цвета'); $bg_color = get_field('цвет_фона'); if (!$bg_color) { if ($temp_color == "Серый") $bg_color = "#f1f0f0"; else if ($temp_color == "Белый") $bg_color = "#ffffff"; else if ($temp_color == "Коричневый") $bg_color = "#bba68f"; else $bg_color = "#ffffff"; } //определяем изображение $image_big = get_field('изображение_новости'); $icon = get_field('иконка'); $temp_img = get_field('шаблон_для_изображения'); if ($image_big) { $bottom_img = ''; } else if ($icon) { $bottom_img = $icon['url']; } else if ($temp_img) { if ($temp_img == "Процент") { if ($temp_color == "Серый") $bottom_img = '' . get_template_directory_uri() . '/images/proc-braun.png'; else if ($temp_color == "Белый") $bottom_img = '' . get_template_directory_uri() . '/images/proc-grey.png'; else if ($temp_color == "Коричневый") $bottom_img = '' . get_template_directory_uri() . '/images/proc-white.png'; } else if ($temp_img == "Подарок") { if ($temp_color == "Серый") $bottom_img = '' . get_template_directory_uri() . '/images/present-braun.png'; else if ($temp_color == "Белый") $bottom_img = '' . get_template_directory_uri() . '/images/present-grey.png'; else if ($temp_color == "Коричневый") $bottom_img = '' . get_template_directory_uri() . '/images/present-white.png'; } else if ($temp_img == "Подарок") { if ($temp_color == "Серый") $bottom_img = '' . get_template_directory_uri() . '/images/pack-braun.png'; else if ($temp_color == "Белый") $bottom_img = '' . get_template_directory_uri() . '/images/pack-grey.png'; else if ($temp_color == "Коричневый") $bottom_img = '' . get_template_directory_uri() . '/images/pack-white.png'; } } //делаем рамку, если фон белый и нет картинки if ($bg_color == '#ffffff' && $bottom_img !== '') { $border_grey = ';border: 1.5px solid #f1f0f0;'; } ?> <a href="<?php the_permalink(); ?>" class="news-body <?php echo $size ?>" style="background-color: <?php echo $bg_color; echo $border_grey; ?>;"> <? /*<div class="name"><?php the_field('заголовок'); ?></div> <div class="desc"><?php the_field('описание'); ?></div> <div class="img"><?php if ($bottom_img != '') { ?><img src="<?php echo $bottom_img ?>" alt=""><?php } ?></div>*/ ?> <div class="img2"><?php if ($image_big) { ?><img src="<?php echo $image_big['url']; ?>" alt=""><?php } ?></div> </a> <? $isPush = !$isPush; } wp_reset_query(); wp_die(); } 

But when you press the upload button, only 6 wrappers are displayed on the screen, neither pictures nor text appear in them.

Attempts to find a ready-made solution gave nothing - everywhere they require wrapping get_field() into a loop, which I have already done.

Apparently, in functions.php you need to connect something for ACF to work. But what?

  • What will var_dump($image_big) in front of <a ... ? - KAGG Design
  • @KAGGDesign, bool(false) - Byulent
  • Read about the difference between page and paged : wp-kama.ru/function/wp_query#parametry-paginatsii - KAGG Design
  • @KAGGDesign, corrected, but it did not help, besides, it is through paged is transmitted via AJAX - Byulent

1 answer 1

The explicit indication of the post ID helped:

 $image_big = get_field('изображение_новости', $post->ID); 

Why he did not decide himself, however, remains incomprehensible.