【问题标题】:Why is my category.php in WP showing all posts?为什么我在 WP 中的 category.php 显示所有帖子?
【发布时间】:2016-05-23 19:01:11
【问题描述】:

我正在尝试在 wordpress 中配置我的 category.php 以显示属于特定类别的帖子。

这是我目前使用的 category.php 的代码:

<?php get_header(); ?>   
    
    <div id="site-wrapper">
        
        <main id="main">
            
            
            <h2 id="category_title"><a href="#">Kategorie "<?php single_cat_title( '', true ); ?>"</a></h2>
            
                <?php 
                // the query
                $args = array('posts_per_page' => -1 );
                $the_query = new WP_Query( $args ); 
            
                ?>
            
                <?php if ( $the_query->have_posts() ) { ?>
            
                    <!-- loop -->
            
                    <?php while ( $the_query->have_posts() ) {
                     
                                $the_query->the_post(); ?>
           <article id="post_cat"> 
                  
                        <div id="thumbnail">
                        
                            <?php if ( has_post_thumbnail() ) : ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
    <?php the_post_thumbnail(); ?>
</a>
<?php endif; ?>
                    
                    </div>
                   
                   <h2><a href="<?php the_permalink();?>"><?php the_title(); ?></a></h2>
                   
                   <div class="entry">
                   
                        <?php the_excerpt(); ?>
                        
                   </div>
                 
           
           </article>
           
	
    
                <?php } } else { ?>
                <p><?php _e( 'Die Posts entsprechen nicht den Kriterien.' ); ?></p>
                <?php }  ?>
    			
               <!-- end of the loop -->
	
    
               <?php wp_reset_postdata(); ?>
        </main>
    
    
<?php get_sidebar(); ?>  
<?php get_footer(); ?>

代码看起来不错,但问题是 index.php 中的所有帖子都显示出来了。

我希望有人可以帮助我!提前致谢!

【问题讨论】:

    标签: php html wordpress wordpress-theming


    【解决方案1】:

    删除自定义查询,这是您的问题。你的循环应该是这样的:

    while ( have_posts() ) :
        the_post();
    
            // The rest of your loop code
    
    endwhile;
    

    【讨论】:

    • 妈的,我瞎了。非常感谢!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-22
    • 2021-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-02
    • 2021-02-25
    相关资源
    最近更新 更多