gpt4 book ai didi

php - 具有多个可变帖子 ID 的 WordPress 查询

转载 作者:行者123 更新时间:2023-11-29 11:39:11 24 4
gpt4 key购买 nike

我正在尝试创建一个自定义 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;
?>

但是,上述方法似乎不起作用并导致页面损坏。有人知道如何修复吗?显然,我将变量传递到查询中是错误的,但我不知道如何修复它。

编辑 - 解决方案

这是工作更新 block 。非常感谢 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>

最佳答案

From the documentation

Caveats query_posts() is only one way amongst many to query the database and generate a list of posts. Before deciding to use query_posts(), be sure to understand the drawbacks.

Alters Main Loop query_posts() is meant for altering the main loop. ...

Secondary Loops To create secondary listings (for example, a list of related posts at the bottom of the page, or a list of links in a sidebar widget), try making a new instance of WP_Query or use get_posts().

An example of WP_Query usage:

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();

从那里您只需修改发送到 WP_Query 的 $args 即可。例如:

$args = array(
'post__in' => array( $post1, $post2, $post3 ),
'post_type' => 'desired_post_type'
);

关于php - 具有多个可变帖子 ID 的 WordPress 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36093488/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com