亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我創建的 Wordpress 自定義主題顯示類別和檔案中的所有帖子

我創建的 Wordpress 自定義主題顯示類別和檔案中的所有帖子

PHP
海綿寶寶撒 2022-07-22 09:25:16
我剛開始玩 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>我在查詢中缺少什么以及如何引用我剛剛單擊的鏈接?
查看完整描述

1 回答

?
慕森卡

TA貢獻1806條經驗 獲得超8個贊

如果您已將代碼放入archive.php其中,則不需要使用 WP_Query(自定義查詢),而只需使用 bog 標準 WordPress 循環,如下所示:


<div class="recentBlogsWrapper">

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

    <div class="blogPostWrapper">

        <?php

        while ( have_posts() ) {

            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 } ?>

    </div>

</div>

說明:通過在自定義循環中使用以下代碼作為參數


$args = array(

  'post_type' => 'post',

  'posts_per_page' => 4,

);

您要求 WordPress 獲取所有(任何)帖子。但實際上,當您在類別存檔中時,您只想抓取某個類別中的帖子。幸運的是,WordPress 為您完成了所有這些工作,因此通過刪除您的自定義查詢,您應該可以開始使用了。


查看完整回答
反對 回復 2022-07-22
  • 1 回答
  • 0 關注
  • 102 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號