如何将搜索结果拆分为前端的帖子类型选项卡? [英] How can I Split Search Results into Post Types Tabs on Frontend?
问题描述
有没有办法根据帖子类型将结果拆分成标签?我想把结果保留为现在的默认结果(所有结果),然后右边的标签按照他们所属的帖子类型筛选出不同的结果。
我在这里创建了一个jpeg来帮助解释我正在尝试做什么。
谢谢
< A HREF = http://f.cl.ly/items/110S2K1A0T3m290Y0C14/Filter_post_Types.jpg 相对= nofollow noreferrer> http://f.cl.ly/items/110S2K1A0T3m290Y0C14/Filter_post_Types.jpg
更新1:
只是想在代码方面添加更多信息
我正在计划使用短代码作为选项卡
[选项卡]
[选项卡]所有搜索结果[/ tab]
[tab]帖子类型1结果[/ tab]
[tab]帖子类型1结果[/ tab]
[tab]帖子类型1结果[bab]
[标签]
或者PHP模板我也可以做它由
<?php
echo do_shortcode('[tabs style =boxedid =searchTabs]
[标题标题=帖子类型1]帖子类型1结果[/ tab]
[标题标题=帖子类型2]帖子类型2结果[/ tab]
[tab title =帖子类型3]帖子类型3结果[/ tab]
[/ tabs]');
?>
我想这里有些东西可以帮助我,但不知道如何实现 http://codex.wordpress.org/Template_Tags/get_posts
更新2:
我在其他论坛上收到的其他建议是尝试这样的代码,但我似乎无法正确实现它到我的搜索模板php http://pastie.org/4248751
建议的代码是
<?php
$ the_slug ='my_slug';
$ args = array(
'name'=> $ the_slug,
'post_type'=>'custom_post_Type_1',
'post_status'=>'publish',
'numberposts'=> 1
);
$ my_posts = get_posts($ args);
$ the_slug2 ='my_slug2';
$ args2 =阵列(
'名称'=> $ the_slug2,
'post_type'=> 'custom_post_Type_2',
'post_status'=> '发布',
'numberposts'=> 1
);
$ my_posts2 = get_posts($ args2);
echo do_shortcode('tab style =boxedid =searchTabs]
[tab title =发布类型1]
<? php //此代码用于第一个帖子类型
foreach($ myposts as $ post):setup_postdata($ post);?>
< li>< a>>> ;<?php the_title();?>< / a>< / li>
<?php endforeach;?>
[/ tab]
[tab title =Post Type 2]
<?php //这段代码用于第二个帖子类型
foreach($ myposts2 as $ post):setup_postdata($ post) ;?>
将立GT;< a取代;>< PHP the_title();?>< / A>< /立GT;
<?php endforeach;?>
[/ tab]
[/ tabs]');
?>
编辑:对不起 -
用户在每个标签中使用不同的循环。
第一个标签:
<?php
全球$ post;
rewind_posts();
$ query = new WP_Query(array(
'posts_per_page'=> -1,
));
while($ query-> have_posts()):$ query-> the_post();
?>
<! - 你的输出码在这里 - >
<?php
endwhile;
wp_reset_postdata();
?>
每个其他选项卡将具有相同的循环,但在查询中添加了一个参数
<?php
global $ post;
rewind_posts();
$ query = new WP_Query(array(
'posts_per_page'=> -1,
'post_type'=>'post_type_name',
));
while($ query-> have_posts()):$ query-> the_post();
?>
<! - 你的输出码在这里 - >
<?php
endwhile;
wp_reset_postdata();
?>
I have a Wordpress Installation. I have about 3 x custom post types on my site. When I do a search, all post type results get displayed on the front end on 1 page. Some post types are totally different from others content wise so the results look a bit funny.
Is there a way to split the results into tabs according to post type? I want to leave the results as default the way they are now (All Results) but then have tabs to the right filtering out the different results according to post type they belong to.
I created a jpeg here to help explain exactly what I am trying to do.
Thanks
http://f.cl.ly/items/110S2K1A0T3m290Y0C14/Filter_post_Types.jpg
Update 1: Just wanted to add a bit more info on the code side
I was planning on using shortcodes as tabs
[tabs]
[tab]All Search Results[/tab]
[tab]Post Type 1 Results[/tab]
[tab]Post Type 1 Results[/tab]
[tab]Post Type 1 Results[/tab]
[tab]
or in PHP Template I can also do it by
<?php
echo do_shortcode('[tabs style="boxed" id="searchTabs"]
[tab title="Post Type 1"]Post Type 1 Results[/tab]
[tab title="Post Type 2"]Post Type 2 Results[/tab]
[tab title="Post Type 3"]Post Type 3 Results[/tab]
[/tabs]');
?>
I am thinking there is something here that can help me, but not sure how to implement it http://codex.wordpress.org/Template_Tags/get_posts
Update 2: Other advice I have received on another forum is to try code like this, but I cannot seem to implement it correctly to my search template php which is here http://pastie.org/4248751
The code suggested was
<?php
$the_slug = 'my_slug';
$args=array(
'name' => $the_slug,
'post_type' => 'custom_post_Type_1',
'post_status' => 'publish',
'numberposts' => 1
);
$my_posts = get_posts($args);
$the_slug2 = 'my_slug2';
$args2=array(
'name' => $the_slug2,
'post_type' => 'custom_post_Type_2',
'post_status' => 'publish',
'numberposts' => 1
);
$my_posts2 = get_posts($args2);
echo do_shortcode('[tabs style="boxed" id="searchTabs"]
[tab title="Post Type 1"]
<?php //This code is for first post type
foreach( $myposts as $post ) : setup_postdata($post); ?>
<li><a>"><?php the_title(); ?></a></li>
<?php endforeach; ?>
[/tab]
[tab title="Post Type 2"]
<?php //This code is for second post type
foreach( $myposts2 as $post ) : setup_postdata($post); ?>
<li><a>"><?php the_title(); ?></a></li>
<?php endforeach; ?>
[/tab]
[/tabs]');
?>
EDIT: Sorry - understand now.
User different loops in each tab.
First tab:
<?php
global $post;
rewind_posts();
$query = new WP_Query(array(
'posts_per_page' => -1,
));
while ($query->have_posts()) : $query->the_post();
?>
<!-- YOUR OUTPUT CODE HERE -->
<?php
endwhile;
wp_reset_postdata();
?>
Each other tab would have the same loop, but with an added parameter in the query
<?php
global $post;
rewind_posts();
$query = new WP_Query(array(
'posts_per_page' => -1,
'post_type' => 'post_type_name',
));
while ($query->have_posts()) : $query->the_post();
?>
<!-- YOUR OUTPUT CODE HERE -->
<?php
endwhile;
wp_reset_postdata();
?>
这篇关于如何将搜索结果拆分为前端的帖子类型选项卡?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!