【发布时间】:2016-07-05 17:53:17
【问题描述】:
我正在尝试创建一个自定义 wordpress 查询,该查询基于使用 Elliot Condon 出色的高级自定义字段设置的 3 个不同变量提取 3 个特定 ID。
我正在使用它来设置我的变量:
<?php
$post1 = get_field('post_1');
$post2 = get_field('post_2');
$post3 = get_field('post_3');
?>
我想将这些变量传递给这样的自定义查询:
<?php
$post_list = array($post1, $post2, $post3);
foreach( $post_list as $post_id ) :
query_posts('p='.$post_id);
while (have_posts()) : the_post();
// echo the post
endwhile;
wp_reset_query();
endforeach;
?>
但是,上述方法似乎不起作用并导致页面损坏。有人对如何解决有任何想法吗?我显然将变量传递到查询中是错误的,但我不知道如何解决它。
编辑 - 解决方案
这是工作更新块。非常感谢 DACrosby!我正在运行自定义帖子类型的查询,因此我需要在 $args 中指定哪种类型。
<div class="row">
<?php
$post1 = get_field('related_1');
$post2 = get_field('related_2');
$post3 = get_field('related_3');
$args = array(
'post__in' => array( $post1, $post2, $post3 ),
'post_type' => 'work'
);
$the_query = new WP_Query( $args );
?>
<?php if ( $the_query->have_posts() ): ?>
<ul class="case-studies cf fade-in fade-in-3">
<!-- Basic Projects -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php
//Get Featured Image URL
$feat_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
//Get Project Categories
$terms = get_the_terms( $post->ID, 'type' );
?>
<li class="case-study">
<img src="<?php echo $feat_image; ?>" alt="<?php the_title(); ?>">
<a href="<?php the_permalink(); ?>" class="cs-hover">
<div class="col-table cs-text">
<div class="col-tcell">
<span><?php the_title(); ?></span>
<span class="divider"></span>
<span><?php the_field('description'); ?></span>
<?php if(get_field('services')): ?>
<ul class="tags">
<?php while(has_sub_field('services')): ?>
<li><?php the_sub_field('service'); ?></li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
<span class="text-link">View Project</span>
</div>
</div>
</a>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
<?php wp_reset_query(); ?>
</div>
【问题讨论】:
标签: php mysql arrays wordpress