Php 返回存储在foreach循环外部的var中的所有值
所以我假设某些内容正在被覆盖,但我不确定如何停止此操作并检索循环外的所有值。有什么想法吗Php 返回存储在foreach循环外部的var中的所有值,php,arrays,variables,loops,foreach,Php,Arrays,Variables,Loops,Foreach,所以我假设某些内容正在被覆盖,但我不确定如何停止此操作并检索循环外的所有值。有什么想法吗 foreach($gallids as $gallterm) { $postterms = wp_get_post_terms($gallterm, 'type', array("fields" => "slugs")); $checkmissing = $postterms[0]; print_r($checkmissing); // C
foreach($gallids as $gallterm)
{
$postterms = wp_get_post_terms($gallterm, 'type', array("fields" => "slugs"));
$checkmissing = $postterms[0];
print_r($checkmissing); // Check Terms for missing images - works here.
}
print_r($checkmissing); // Check Terms for missing images - not working here
// - seems to be getting last or first val only.
首先,初始化稍后要使用的变量:
$checkmissing = array();
$cat_terms = array();
然后在foreach
中,将post术语的第一个条目附加到该数组中:
foreach($gallids as $gallterm)
{
list($checkmissing[]) = wp_get_post_terms($gallterm, 'type', array("fields" => "slugs"));
}
请参阅$checkmissing[]
,这将有效防止您覆盖它。它会将每个附加到数组中
最后,您可以在循环后输出结果:
print_r($checkmissing);
注意:如果wp\u get\u post\u terms
返回空数组,则应执行一些额外的处理:
foreach($gallids as $gallterm)
{
$terms = wp_get_post_terms($gallterm, 'type', array("fields" => "slugs"))
AND list($checkmissing[]) = $terms
;
}
我尝试了上面的几个例子,但它们并没有按照我想要的方式工作。所以我四处摸索,做了一些研究,下面是我如何让它为我工作的 在我的特殊情况下,我需要获取特定帖子的所有类别ID,然后将该变量返回到WP_查询参数数组中 首先,你需要抓住后条款
$terms = get_the_terms( $post->ID, 'category' );
接下来,您需要初始化稍后要使用的变量:
$checkmissing = array();
$cat_terms = array();
接下来,您将声明foreach以获取每个单独的术语ID
foreach ( $terms as $term ) {
$cat_terms[] = $term->term_id;
}
现在让我们假设您想要为这个$cat_terms变量返回一个逗号分隔的列表。我们将使用“join”函数
$comma_separated_terms = join( ", ", $cat_terms );
现在让我们假设您想使用这个变量将“category\uu in”参数放入WP\u查询循环中。我们将使用“数组值”
$values = array_values($cat_terms);
这样做的好处是,现在我们可以将$values变量插入到WP_查询参数中:
<?php global $post;
$query = new WP_Query(array(
'post_type' => 'post_type_name',
'category__in' => $values));
?>
在我的特定案例中,客户机希望根据博客文章类别在侧栏中显示一些自定义的文章类型。因此,我需要获取所有博客文章术语,并将它们与自定义文章类型类别的术语相匹配
最终代码如下所示:
<?php
$terms = get_the_terms( $post->ID, 'category' );
$cat_terms = array();
foreach ( $terms as $term ) {
$cat_terms[] = $term->term_id;
}
$values = array_values($cat_terms);
?>
<h3><?php echo $title; ?></h3>
<?php global $post;
$query = new WP_Query(array(
'post_type' => 'custom_post_type',
'category__in' => $values));
if ( $query->have_posts() ) : ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
// Code for loop goes here
<?php endwhile; endif; ?>
<?php wp_reset_postdata(); ?>
//循环代码在这里
要么使$checkmissing数组,要么使用$checkmissing.=$posterms[0];要将每个postterms值连接到字符串。。。或$checkmissing.=','$期后[0];如果需要逗号分隔的字符串
<?php
$terms = get_the_terms( $post->ID, 'category' );
$cat_terms = array();
foreach ( $terms as $term ) {
$cat_terms[] = $term->term_id;
}
$values = array_values($cat_terms);
?>
<h3><?php echo $title; ?></h3>
<?php global $post;
$query = new WP_Query(array(
'post_type' => 'custom_post_type',
'category__in' => $values));
if ( $query->have_posts() ) : ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
// Code for loop goes here
<?php endwhile; endif; ?>
<?php wp_reset_postdata(); ?>