在数组中将ACF字段指定为变量(Assigning an ACF field as variable inside an array)

我试图将变量的int值获取到数组但它不起作用。 下面的代码首先打印myvariable,然后在不获取类别的情况下执行它打算执行的操作。

我正在使用ACF。 my_acf_field是一个包含类别ID作为其值的字段。

<?php $myvariable = the_field('my_acf_field'); $args=array( 'cat' => $myvariable, 'orderby' => 'rand', 'numberposts' => 6, ); $posts = get_posts( $args ); foreach($posts as $post) { ?> <div class="related-posts"> <p>Some stuff</p> </div> <?php } ?>

I'm trying to get the int value of a variable to an array but it doesn't work. The code below first prints the myvariable then do what it intends to do without getting the category.

I'm using ACF. my_acf_field is a field that holds a category id as its value.

<?php $myvariable = the_field('my_acf_field'); $args=array( 'cat' => $myvariable, 'orderby' => 'rand', 'numberposts' => 6, ); $posts = get_posts( $args ); foreach($posts as $post) { ?> <div class="related-posts"> <p>Some stuff</p> </div> <?php } ?>

最满意答案

我只是想通了我需要使用get_field('my_acf_field')而不是the_field('my_acf_field')。

<?php $myvariable = get_field('my_acf_field'); $args=array( 'cat' => $myvariable, 'orderby' => 'rand', 'numberposts' => 6, ); $posts = get_posts( $args ); foreach($posts as $post) { ?> <div class="related-posts"> <p>Some stuff</p> </div> <?php } ?>

I just figured out that I need to use get_field('my_acf_field') instead of the_field('my_acf_field').

<?php $myvariable = get_field('my_acf_field'); $args=array( 'cat' => $myvariable, 'orderby' => 'rand', 'numberposts' => 6, ); $posts = get_posts( $args ); foreach($posts as $post) { ?> <div class="related-posts"> <p>Some stuff</p> </div> <?php } ?>

更多推荐

php,myvariable,电脑培训,计算机培训,IT培训"/> <meta name="description&qu