首页的类别和帖子

时间:2017-09-23 作者:Phaidonas Gialis

我想做一个与类别名称和类别下的每一个职位首页。我尝试过的代码是:

$categories_list = get_the_category_list( esc_html__( \', \', \' \' ) );
if ( $categories_list ) {

    /* translators: 1: list of categories. */
    printf( \'<span class="cat-links">\' . esc_html__( \'Posted in %1$s\', \'plavou_2017\' ) . \'</span>\', $categories_list ); // WPCS: XSS OK.
}

            /* Start the Loop */
            while ( have_posts() ) : the_post();

            $args = array(
  \'exclude\' => \'\',
    \'feed\' => \'\',
);

wp_list_categories( $args );


                /*
                 * Include the Post-Format-specific template for the content.
                 * If you want to override this in a child theme, then include a file
                 * called content-___.php (where ___ is the Post Format name) and that will be used instead.
                 */
                get_template_part( \'template-parts/content\', get_post_format() );

            endwhile;

            the_posts_navigation();

        else :

            get_template_part( \'template-parts/content\', \'none\' );

        endif; ?>
它在某一点上是有效的,但我更喜欢为每个类别再次打印类别,最好是有类别和它的帖子。现在,它会为每个帖子再次打印所有类别。

1 个回复
SO网友:Pranav Bhatt

首先,作为wordpress的初学者,不要犹豫。其次,对于带有相应帖子的类别列表,您需要执行以下操作:

     <ul class="et_pb_tabs_controls clearfix tab-ul">
                            <?php 
                            dynamic_sidebar(\'sidebar\');    
                            $cate_id=get_query_var(\'cat\');

                            global $sitepress;

                            $paged = ( get_query_var(\'page\') ) ? get_query_var(\'page\') : 1;

                            $sitepress->switch_lang( $sitepress->get_default_language() );

                            $args = array(
                                \'type\'                     => \'post\',
                                \'child_of\'                 => 0,
                                \'parent\'                   => \'\',
                                \'orderby\'                  => \'name\',
                                \'order\'                    => \'DESC\',
                                \'hide_empty\'               => 1,
                                \'hierarchical\'             => 1,
                                \'exclude\'                  => 1,
                                \'include\'                  => \'\',
                                \'number\'                   => \'\',
                                \'taxonomy\'                 => \'category\',
                                \'pad_counts\'               => false 
                                );
                            $flag=0;        
                            $inc=0;

                            foreach (get_categories( $args ) as $category):
                               $flag=($inc==0)?0:1;   
                           if( !is_array($cate_id) && $cate_id=="")
                               $category_arr[]= $category->term_id;   

                           ?>
                           <li class="et_pb_tab_0 <?php echo ($flag==0)? \'et_pb_tab_active\':\'\';?>"><a href="#<?php echo $category->name; ?>"><?php echo $category->name; ?></a></li>
                           <?php 
                           $inc++; 
                           endforeach; ?>
                       </ul>
<?php foreach ($category_arr as $category_id):
                            $arr=array(
                                \'post_type\' => \'post\',
                                \'order\'=>"ASC",
                                \'cat\'=>$category_id,
                                \'posts_per_page\' => -1,
                                \'paged\' => $current_page,
                                \'suppress_filters\' => false
                                );
                        $wpb_all_query = new WP_Query($arr); ?>
<div class=" et_pb_row et_pb_row_1 full-width">
    <?php if ( $wpb_all_query->have_posts() ) : 
    $increment=0;
    while ( $wpb_all_query->have_posts() ) : 

        if(($increment%3)==0){
            echo \'</div><div class=" et_pb_row et_pb_row_1 full-width">\';
        }
        $wpb_all_query->the_post(); 

        ?>
        <div class="et_pb_column et_pb_column_1_3 et_pb_column_1">
            <div class="et_pb_posts et_pb_module et_pb_bg_layout_light  et_pb_blog_0">
                <div class="entry box">
                    <!--If no results are found-->
                    <?php $post_link=  get_permalink(); ?>
                    <?php $img_path=get_the_post_thumbnail(); ?>
                    <?php $url = wp_get_attachment_url( get_post_thumbnail_id()); ?>
                    <a href="<?php echo $post_link;?>">

                      <?php  if($url !=""){
                        ?>
                        <img src="<?php echo $url; ?>" alt="">
                        <?php } ?>
                    </a>
                    <a href="<?php echo $post_link;?>"><h1><?php the_title(); ?></h1></a>
                    <p class="box-content"><?php echo(get_the_excerpt()); ?></p>
                    <div class="profile-box"> 
                        <img src="<?php bloginfo(\'template_url\'); ?>/img/profile-icon.png" alt="" id="" data-height-percentage="54" data-actual-width="18" data-actual-height="28">  
                        <div class="pro-name"><h4><?php echo get_the_author(); ?></h4>                                   
                            <span><?php echo get_the_date(); ?></span>
                        </div>
                    </div>
                </div><!--End if no results are found-->
            </div> <!-- .et_pb_posts -->
        </div><!-- .et_pb_column -->
        <?php 
        $increment++;
        endwhile; 
        wp_reset_postdata();
        endif; 
        ?>
</div> 

结束

相关推荐

Ignore latest two posts

我试图从一个页面中排除最近的两篇博客文章。我知道这是可能的offset 然而,这样做会导致一个bug,其中一些博客帖子会在第二页上重复,因此并不理想。目前,我正在使用post id手动执行此操作,如下所示:$paged = ( get_query_var( \'paged\' ) ) ? get_query_var( \'paged\' ) : \'1\'; $args = array( \'posts_per_page\' => 5, \'post__not