首页 > 解决方案 > 将多个 ACF 变量合并到一个数组中

问题描述

我正在使用 ACF 关系字段。我正在显示多个手动选择的帖子块。有一个最后一个帖子块,我想在其中排除所有之前手动选择的帖子。

如何制作所有 ACF 的数组以选择它们以将它们从循环中排除?

这是我到目前为止的代码(不工作,如果我只使用一个变量,它就可以工作)

<?php   
$excluir = get_field('bloque_6_posts');
$excluir2 = get_field('bloque_2_posts');
$excluir3 = get_field('post_destacado');
$excluir4 = get_field('posts_destacados');
$excluir5 = get_field('bloque_4_posts');
$excluirtodo = array (
  $excluir,
  $excluir2,
  $excluir3,
  $excluir4,
  $excluir5
);
$paged = (get_query_var('page')) ? get_query_var('page') : 1;
$args = array(
  'posts_per_page' => 9,
  'paged'          => $paged,
  'post__not_in' => $excluirtodo
);

$the_query = new WP_Query( $args ); 
?>

编辑[已解决]:正如@disinfor 在评论中指出的那样,解决方案是 array_merge 而不是 array

标签: arrayswordpressvariablesadvanced-custom-fields

解决方案


从评论中添加我的答案以帮助未来的访问者

您当前正在将数组数组传递给post__not_in. 您需要使用array_merge将数组组合成一个数组。

<?php   
$excluir = get_field('bloque_6_posts');
$excluir2 = get_field('bloque_2_posts');
$excluir3 = get_field('post_destacado');
$excluir4 = get_field('posts_destacados');
$excluir5 = get_field('bloque_4_posts');

// NEW CODE HERE
$excluirtodo = array_merge(
  $excluir,
  $excluir2,
  $excluir3,
  $excluir4,
  $excluir5
);
// END ARRAY_MERGE
$paged = (get_query_var('page')) ? get_query_var('page') : 1;
$args = array(
  'posts_per_page' => 9,
  'paged'          => $paged,
  'post__not_in' => $excluirtodo
);

$the_query = new WP_Query( $args ); 
?>

推荐阅读