仅显示特定类别的帖子

时间:2016-10-28 15:50:46

标签: php wordpress

在我的page-home.php中,上面的部分会收到所有最新的帖子。

elseif($key == 'latest_post_block'){ ?>
    <?php if(of_get_option('latest_post_block_visibility') != '0'): ?>
    <section id="blog" class="clearfix">
        <div class="ak-container">
        <?php if(of_get_option('latest_post_title') || of_get_option('latest_post_desc')): ?>
            <div class="section-title-wrap">
                <?php if(of_get_option('latest_post_title')): ?>
                    <h1 class="main-title"><?php echo esc_attr(of_get_option('latest_post_title'));?></h1>
                <?php endif; ?>

                <?php if(of_get_option('latest_post_desc')): ?>
                    <div class="sub-desc">
                        <?php echo esc_textarea(of_get_option('latest_post_desc'));?>
                    </div>
                <?php endif; ?>
            </div>
        <?php endif; ?>
        </div>

        <?php 
        if(of_get_option('latest_post_count') > 0) :
        $args = array(
            'posts_per_page' => of_get_option('latest_post_count')
            );
        $query = new WP_Query($args);                   
        if($query->have_posts()): 
        ?>
        <div class="blog-block-wrapper clearfix">
            <div class="ak-container">
            <div class="block-block-wrap">
                <?php
                $count = 0;
                while($query->have_posts()):
                    $query->the_post(); 
                    $count++;
                    $class = ($count % 2 == 0) ? "right-blog" : "left-blog";
                ?>
                    <div class="blog-block <?php echo $class; ?>">
                        <div class="blog-image">
                            <?php if(has_post_thumbnail()):
                                $big_image = wp_get_attachment_image_src( get_post_thumbnail_id(), 'full' );
                                $image = wp_get_attachment_image_src( get_post_thumbnail_id(), 'blog-thumbnail' );
                            ?>
                            <a class="blog-img-wrap" href="<?php the_permalink(); ?>">
                            <img src="<?php echo $image[0]; ?>" alt="<?php the_title(); ?>">
                            </a>
                            <div class="blog-overlay">
                                <div class="blog-anchor-wrap">
                                    <a class="search fancybox-gallery" data-lightbox-gallery="gallery" href="<?php echo $big_image[0]; ?>"> <i class="fa fa-search"></i></a>
                                    <a class="link" href="<?php the_permalink(); ?>"> <i class="fa fa-link"> </i> </a>
                                </div>
                            </div>
                            <?php 
                            endif; ?>
                        </div>

                        <div class="blog-content-wrapper clearfix"> 
                            <div class="blog-date-wrap">
                                <div class="blog-date">
                                    <?php echo get_the_date('d'); ?><br/>
                                    <?php echo get_the_date('M'); ?>
                                </div>
                            </div>
                            <div class="blog-content">
                                <h3 class="blog-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?> </a></h3>
                                <div class="blog-desc"><?php echo accesspress_letter_count(get_the_content(),'200'); ?></div>
                            </div>
                            <div class="clearfix"> </div>
                            <div class="blog-comments-wrap clearfix">
                                <div class="blog-comments">
                                    <span><a class="author" href="<?php echo get_author_posts_url( get_the_author_meta( 'ID' ) ); ?>"> <i class="fa fa-user"> </i><?php echo get_the_author(); ?></a></span>
                                    <span><a class="comment" href="<?php echo get_comments_link( $post->ID ); ?>"> <i class="fa fa-comments"> </i><?php echo get_comments_number(); ?></a></span>
                                    <?php if(has_category()): ?>
                                    <span>
                                    <i class="fa fa-folder"></i><?php echo get_the_category_list(', '); ?>
                                    </span>
                                    <?php endif; ?>
                                    <?php if(has_tag()): ?>
                                    <span>
                                    <i class="fa fa-tags"></i><?php echo get_the_tag_list('' , ', '); ?>
                                    </span>
                                    <?php endif; ?>
                                </div>
                            </div>
                        </div>
                    </div>
                    <?php 
                        if($count % 2 == 0){
                            echo '<div class="clearfix"></div>';
                        }
                    ?>
                    <?php 
                    endwhile; ?>
                </div>
            </div>
        </div> <!-- blog-block-wrapper end -->
        <?php 
        endif; 
        wp_reset_postdata(); 
        endif; ?>
    </section> <!-- blog -->
    <?php endif; ?> 

但是,我只希望显示某类帖子,而不是所有帖子。这会发生吗?

1 个答案:

答案 0 :(得分:1)

是的,您可以像这样修改$args数组:

$args = array(
    'posts_per_page' => of_get_option('latest_post_count'),
    'cat' => 4 // Category ID
);

或者,按类别slug查询:

$args = array(
    'posts_per_page' => of_get_option('latest_post_count'),
    'category_name' => 'cat' // Category slug
);