我创建的 Wordpress 自定义主题显示类别和档案中的所有帖子

我刚开始玩 WordPress,并创建了一个有趣的主题。


一切正常,除了当我点击档案月份或类别时,我被带到的页面就像主页一样显示所有博客文章,即使 slug 是正确的。


我创建了一个存档页面和类别页面,复制了主页,因为我希望设计相同。


        <div class="recentBlogsWrapper">

            <h3><?php single_cat_title(); ?><?php get_the_archive_title(); ?> Category</h3>

            <div class="blogPostWrapper">

                <?php

                    $args = array(

                        'post_type' => 'post',

                        'posts_per_page' => 4,

                    );

                    $blogposts = new WP_Query($args);

                    while($blogposts->have_posts()) {

                        $blogposts->the_post();

                ?>

                <a href="<?php echo the_permalink(); ?>" class="blogCard card">

                    <div class="blogHomeImgWrap">

                        <img class="blogPostImg" src="<?php echo get_the_post_thumbnail_url('get_the_ID'(),'full') ?>" />

                    </div>

                    <div class="blogPadding">

                            <h3><?php the_title(); ?></h3>

                            <p><?php the_time('F j, Y') ?></p>

                    </div>

                </a>

                <?php } wp_reset_query(); ?>

            </div>

        </div>

我在查询中缺少什么以及如何引用我刚刚单击的链接?


海绵宝宝撒
浏览 71回答 1
1回答

慕森卡

如果您已将代码放入archive.php其中,则不需要使用 WP_Query(自定义查询),而只需使用 bog 标准 WordPress 循环,如下所示:<div class="recentBlogsWrapper">&nbsp; &nbsp; <h3><?php single_cat_title(); ?><?php get_the_archive_title(); ?> Category</h3>&nbsp; &nbsp; <div class="blogPostWrapper">&nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp; &nbsp; &nbsp; &nbsp; while ( have_posts() ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; the_post();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="<?php echo the_permalink(); ?>" class="blogCard card">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="blogHomeImgWrap">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <img class="blogPostImg" src="<?php echo get_the_post_thumbnail_url( get_the_ID(), 'full' ) ?>" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="blogPadding">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <h3><?php the_title(); ?></h3>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p><?php the_time( 'F j, Y' ); ?></p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </a>&nbsp; &nbsp; &nbsp; &nbsp; <?php } ?>&nbsp; &nbsp; </div></div>说明:通过在自定义循环中使用以下代码作为参数$args = array(&nbsp; 'post_type' => 'post',&nbsp; 'posts_per_page' => 4,);您要求 WordPress 获取所有(任何)帖子。但实际上,当您在类别存档中时,您只想抓取某个类别中的帖子。幸运的是,WordPress 为您完成了所有这些工作,因此通过删除您的自定义查询,您应该可以开始使用了。
打开App,查看更多内容
随时随地看视频慕课网APP