首页 > 解决方案 > php代码有问题按类别过滤帖子

问题描述

我是 php 新手并尝试制作一个 wordpress 主题,帮助我转换此代码,而不是按主题过滤最新帖子以按类别过滤帖子,我一直在尝试更改它几个小时,但它似乎没有工作

代码如下,我基于另一个主题非常好,但是此代码仅获取从所有类别聚合的最新帖子,如果我想获取特定类别的最新帖子,我应该如何更改它?

<?php if ( ! is_paged() ) {

            $latest_posts_used = false;
            if ( !empty( $mts_options['mts_featured_categories'] ) ) {
                foreach ( $mts_options['mts_featured_categories'] as $section ) {
                    $category_id = $section['mts_featured_category'];
                    $featured_category_layout = $section['mts_featured_category_layout'];
                    $posts_num = $section['mts_featured_category_postsnum'];
                    if ( $category_id === 'latest' && ! $latest_posts_used ) {
                        $latest_posts_used = true;
                        $fc_section_class  = ( in_array( $featured_category_layout, array( 'horizontal', 'vertical' ) ) ) ? '' : ' '.$featured_category_layout;
                        $fc_section_no_gap = ( $featured_category_layout === 'dark' ) ? ' no-gap' : '';
                        ?>
                        <section id="latest-posts" class="clearfix<?php echo $fc_section_class ?><?php echo $fc_section_no_gap ?>">
                            <h4 class="featured-section-title"><?php _e( "Latest Articles", "mythemeshop" ); ?></h4>
                            <?php $j = 1; if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
                            <?php newstimes_the_homepage_article( $featured_category_layout, $j, true );?>
                            <?php $j++; endwhile; endif; ?>
                            <!--Start Pagination-->
                            <?php if (isset($mts_options['mts_pagenavigation_type']) && $mts_options['mts_pagenavigation_type'] == '1' ) { ?>
                                <?php $additional_loop = 0; mts_pagination($additional_loop['max_num_pages']); ?>
                            <?php } else { ?>
                                <div class="pagination pagination-previous-next">
                                    <ul>
                                        <li class="nav-previous"><?php next_posts_link( '<i class="fa fa-chevron-left"></i> '. __( 'Previous', 'mythemeshop' ) ); ?></li>
                                        <li class="nav-next"><?php previous_posts_link( __( 'Next', 'mythemeshop' ).' <i class="fa fa-chevron-right"></i>' ); ?></li>
                                    </ul>
                                </div>
                            <?php } ?>
                            <!--End Pagination-->
                        </section><!--#latest-posts-->
                    <?php } elseif ( $category_id !== 'latest' ) {

                        $fc_section_class  = ( in_array( $featured_category_layout, array( 'horizontal', 'vertical' ) ) ) ? '' : ' '.$featured_category_layout;

                        ?>
                        <section class="featured-section clearfix<?php echo $fc_section_class ?>">
                            <h4 class="featured-section-title"><a href="<?php echo esc_url( get_category_link($category_id) ); ?>" title="<?php echo esc_attr( get_cat_name($category_id) ); ?>"><?php echo get_cat_name($category_id); ?></a></h4>
                            <?php $cat_query = new WP_Query('cat='.$category_id.'&posts_per_page='.$posts_num); ?>
                            <?php $j = 1; if ($cat_query->have_posts()) : while ($cat_query->have_posts()) : $cat_query->the_post(); ?>
                            <?php newstimes_the_homepage_article( $featured_category_layout, $j );?>
                            <?php $j++; endwhile; endif; wp_reset_postdata();?>
                        </section>
                    <?php } ?>
                <?php } ?>
            <?php } ?>
        <?php } else {

            // Paged

            $latest_section_layout = 'vertical'; // default layout if latest posts section does not exists
            if ( !empty( $mts_options['mts_featured_categories'] ) ) {
                foreach ( $mts_options['mts_featured_categories'] as $section ) {
                    if ( $section['mts_featured_category'] === 'latest' ) {
                        $latest_section_layout = $section['mts_featured_category_layout'];
                        break;
                    }
                }
            }

            $fc_section_class  = ( in_array( $latest_section_layout, array( 'horizontal', 'vertical', 'mixed' ) ) ) ? '' : ' '.$latest_section_layout;
            $fc_section_no_gap = ( $latest_section_layout === 'dark' ) ? ' no-gap' : '';
            ?>
            <section id="latest-posts" class="clearfix<?php echo $fc_section_class ?><?php echo $fc_section_no_gap ?>">
            <?php $j = 1; if (have_posts()) : while (have_posts()) : the_post(); ?>
            <?php newstimes_the_homepage_article( $latest_section_layout, $j, true );?>
            <?php $j++; endwhile; endif; ?>
            <!--Start Pagination-->
            <?php if (isset($mts_options['mts_pagenavigation_type']) && $mts_options['mts_pagenavigation_type'] == '1' ) { ?>
                <?php $additional_loop = 0; mts_pagination($additional_loop['max_num_pages']); ?>
            <?php } else { ?>
                <div class="pagination pagination-previous-next">
                    <ul>
                        <li class="nav-previous"><?php next_posts_link( '<i class="fa fa-chevron-left"></i> '. __( 'Previous', 'mythemeshop' ) ); ?></li>
                        <li class="nav-next"><?php previous_posts_link( __( 'Next', 'mythemeshop' ).' <i class="fa fa-chevron-right"></i>' ); ?></li>
                    </ul>
                </div>
            <?php } ?>
            <!--End Pagination-->
            </section><!--#latest-posts-->
        <?php } ?>

标签: phpwordpress

解决方案


推荐阅读