Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
WordPress-当存在多个帖子类型时,在foreach循环中添加帖子类型标题_Wordpress_Loops - Fatal编程技术网

WordPress-当存在多个帖子类型时,在foreach循环中添加帖子类型标题

WordPress-当存在多个帖子类型时,在foreach循环中添加帖子类型标题,wordpress,loops,Wordpress,Loops,我正在自定义WordPress中的搜索结果循环。我正在显示两种自定义帖子类型的搜索结果:网络研讨会和研究。我想把网络研讨会和研究分成各自的部分,每个部分都有一个标题 电流环 <?php if( have_posts() ){ $types = array('webinars', 'research'); foreach( $types as $type ) {

我正在自定义WordPress中的搜索结果循环。我正在显示两种自定义帖子类型的搜索结果:网络研讨会和研究。我想把网络研讨会和研究分成各自的部分,每个部分都有一个标题

电流环

<?php
            if( have_posts() ){
                $types = array('webinars', 'research');
                foreach( $types as $type ) {                    
                    while( have_posts() ){
                        the_post();
                        if( $type == get_post_type() ){
                            get_template_part('loop-templates/content', 'search');
                        }
                    }
                    rewind_posts();
                }
            }             
        ?>


关于让所有网络研讨会都生活在自己的div容器中,让所有研究都生活在自己的div容器中,有什么建议吗?

您可以直接在搜索模板中为每个研讨会指定帖子类型(不必在循环中)。如果您想要两个不同的模板,一个用于网络研讨会,一个用于研究,您可以这样做:

<?php if( have_posts() ){

if ( 'webinars' === get_post_type() ):
echo '<h2>Webinars</h2>';
while( have_posts() ){
the_post();
//Template for webinars
the_title(); echo '<br/>';
};
endif;

if ( 'research' === get_post_type() ):
echo '<h2>Research</h2>';
while( have_posts() ){
the_post();
//Template for research
the_title(); echo '<br/>';
};
endif;

}; ?>
你几乎可以做任何事情,只要记住,你的搜索页面必须适应任何搜索查询,不要将其锁定为特定的帖子类型

<?php add_action( 'pre_get_posts', function ( $query ) {
if ( ! is_admin() && $query->is_search() && $query->is_main_query() && in_array ( $query->get( 'post_type' ), array( 'webinars', 'research' ) ) ) {
$query->set( 'post_type', array( 'webinars', 'research' ) );
$query->set( 'posts_per_page', 12 );
};
} ); ?>