[英]List each custom post type separately in a wp_query without repeating the loop
我正在使用AJAX搜索從wordpress中提取3種自定義帖子類型(帖子,指南,建議)。 正如您在我的if while
循環中看到的那樣,結果顯示了哪些結果,但是我正嘗試將它們分開,以便顯示如下內容:
第1節
博客文章
第2節
導游
問題似乎是我需要編輯if while循環,因為在該循環中添加任何內容只會使它進入該循環。 有誰知道修改if while循環實現此目標的最佳方法?
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_Query
結果進行排序或重新排序。
但是,如果您對單個查詢進行設置-由於這些字符串太短了,我可能只想通過Output Buffer
控制Output Buffer
。
基本上,您可以創建一個關聯數組,並將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>';
}
}
如果我理解您的擔心,建議通過創建一個通用類來划分您的代碼。 然后您撥打不同的Post Type:
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();
您最初處於正確的軌道。 您可以只使用標准的發布對象來檢查發布類型。 您可以調整以適合自己的需求,但可以嘗試以下操作:
<?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
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.