Php 在wp_查询中单独列出每个自定义帖子类型,而不重复循环
我正在使用AJAX搜索从wordpress post、guides和advice中提取3种自定义帖子类型。正如您在我的if-while循环中所看到的,结果显示了哪些结果是哪些,但我正试图将它们单独分开,这样它将显示如下内容: 第一节 博客帖子 第二节 向导 问题似乎是我需要编辑if while循环,因为在该循环中添加任何内容都会导致它位于该循环中。有人知道修改if-while循环以实现此目的的最佳方法吗Php 在wp_查询中单独列出每个自定义帖子类型,而不重复循环,php,jquery,wordpress,Php,Jquery,Wordpress,我正在使用AJAX搜索从wordpress post、guides和advice中提取3种自定义帖子类型。正如您在我的if-while循环中所看到的,结果显示了哪些结果是哪些,但我正试图将它们单独分开,这样它将显示如下内容: 第一节 博客帖子 第二节 向导 问题似乎是我需要编辑if while循环,因为在该循环中添加任何内容都会导致它位于该循环中。有人知道修改if-while循环以实现此目的的最佳方法吗 function data_fetch(){ $the_query = new WP_Que
function data_fetch(){
$the_query = new WP_Query(
array(
'posts_per_page' => 6,
's' => esc_attr( $_POST['keyword'] ),
'post_type' => array('post' , 'guides', 'advice'),
'post_status' => 'publish'
)
);
global $post;
if( $the_query->have_posts()) :
while( $the_query->have_posts() ): $the_query->the_post(); ?>
<?php $type = get_post_type();
$term = $_POST['keyword'];
$i++;
$total = $the_query->found_posts;
?>
<span class="search-title">
<?php if ($type == 'post'):?>
<?php echo 'Article: ';?>
<?php elseif($type == 'guide' ):?>
<?php echo 'Guide: ';?>
<?php elseif($type == 'advice' ):?>
<?php echo 'advice: ';?>
<?php endif;?>
<a href="<?php echo esc_url( post_permalink() ); ?>"><?php the_title();?></a><br>
</span>
<?php endwhile; ?>
<?php
wp_reset_postdata();
else:
echo '<h3>No Results Found</h3>';
endif;
die();
}
如果我是你,我可能只做三个独立的查询。它们看起来很简单,根本不应该引起任何问题。否则,您必须以某种方式对WP_查询结果进行排序或重新排序 但是,如果您设置为单个查询,因为这些是如此短的HTML字符串,我可能只需要使用 基本上,您可以创建一个关联数组,并将HTML字符串添加到相应的键中。您可以做得更详细一点,并将它们设置为嵌套数组,但是由于您的输出很轻,您可能只需要将HTML字符串作为键的值就可以了 我冒昧地清理了一下你的代码,删除了一些未使用的变量,等等
function data_fetch(){
// Build the Query Arguments
$the_query = new WP_Query( array(
's' => esc_attr($_POST['keyword']),
'posts_per_page' => 6,
'post_type' => array('post' , 'guides', 'advice'),
'post_status' => 'publish'
) );
// Do we have posts?
if( $the_query->have_posts()){
// We do. Start an array that will fill with HTML from the output buffer
$output = array(
'post' => '',
'guides' => '',
'advice' => '',
);
// Loop through the posts
while( $the_query->have_posts() ){
$the_query->the_post();
ob_start(); // Turn on the output buffer
$type = get_post_type(); ?>
<span class="search-title">
<?php echo ($type == 'post') ? 'Article: ' : ucwords($type).': '; // Output "Articles: " for posts, or just capitalize the other post type names ?>
<a href="<?php echo esc_url( post_permalink() ); ?>"><?php the_title();?></a><br>
</span>
<?php $output[$type] .= ob_get_clean(); // Add the HTML output from the buffer to our array
}
wp_reset_postdata();
// Here we have an array with 3 HTML strings that we can output wherever
echo $output['post'];
echo $output['guides'];
echo $output['advice'];
} else {
echo '<h3>No Results Found</h3>';
}
}
如果我理解您的担忧,我建议通过创建泛型类来划分代码。 您可以调用不同的帖子类型:
class Custom_Query {
public $post_type;
public $key_word;
public function __construct($post_type, $keyword) {
$this->post_type = $post_type;
$this->key_word = $keyword;
}
public function getResult() {
echo '<h1>Article : ' . $this->post_type . '</h1>';
$the_query = new WP_Query(
array(
'posts_per_page' => 6,
's' => esc_attr($this->key_word),
'post_type' => array($this->post_type),
'post_status' => 'publish'
)
);
if ($the_query->have_posts()):
while ($the_query->have_posts()): $the_query->the_post();
echo '<span class="search-title">';
echo '<a href=" ' . esc_url(post_permalink()) . '">' . the_title() . '</a><br>';
echo '</span>';
endwhile;
else:
echo '<h3>No Results Found</h3>';
endif;
wp_reset_postdata();
}
}
global $post;
$type = get_post_type();
$term = $_POST['keyword'];
$article = new Custom_Query($type, $term);
$article->getResult();
一开始你的思路是对的。您可以只使用标准post对象来检查post类型。您可以根据需要进行调整,但可以尝试以下方法:
<?php
query = new WP_Query( array(
's' => esc_attr($_POST['keyword']),
'posts_per_page' => 6,
'post_type' => array('post' , 'guides', 'advice'),
'post_status' => 'publish'
) );
if ($query->have_posts()) {
while ($query->have_posts()):
$query->the_post();
?>
<div class="posts">
<?php
if ($query->post->post_type === 'post') {
// add your content
} else if ($query->post->post_type === 'guides' {
// add your content
} else {
// add your content
}
?>
</div>
<?php endwhile;
} else {
// no posts found
}
这是最好的答案,谢谢,但它只从每个输出中提取一个条目。我如何将它们全部提取出来,或者至少增加每个结果的数量?实际上echo$output['xxxx']不起作用,它只吐出0,还有其他提示吗?谢谢抱歉,我是想使用连接运算符!更改$output[$type]=ob\u get\u clean;到$output[$type]。=ob_get_clean;注意等号之前的周期。如果您现在在输出中没有得到任何内容,可能是因为您的查询没有得到任何该post类型的内容。例如,如果您每个都有一个,然后发布6个新的指南,您将只收到指南,而没有发布或建议项目,因为查询获取的6个最新项目只是指南。啊,我看到它现在可以工作了,谢谢!有没有办法限制每个输出的数量?就像你说的,如果我发布6个新的指南,它将只显示指南。我只是想知道是否有一种方法可以限制像3个这样的总数。是的,如果你需要保证每一个的最大值,你需要回到我最初提到的,并做3个单独的查询-每一个帖子类型一个