douluozhan4370 2017-04-08 06:48
浏览 46
已采纳

如何在Wordpress的页面部分显示博客?

I'm trying to display blog posts underneath the 'about us' paragraph on an about page by using the code below in a template part. However, it's only returning the title of the actual page and the date info as the date I've edited the page.

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
   <article class="post">
        <header>
            <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
            <div class="post-details">
                <i class="fa fa-user"></i><?php the_author_posts_link(); ?>
                <i class="fa fa-calendar"></i> <?php the_time( 'F jS, Y' ); ?>
                <i class="fa fa-folder-open-o"></i> <a href=""><?php the_category( ', ' ); ?></a>
                <i class="fa fa-comments"></i><a href=""><?php comments_popup_link( 'No Comments »', '1 Comment »', '% Comments »' ); ?></a>

            </div><!-- post details -->
        </header>

        <div class="post-excerpt">
            <p><?php the_excerpt(); ?> <a href="post.html">continue reading</a></p>
        </div><!-- post-excerpt -->

        <hr>

    </article><!-- end article -->
<?php endwhile; else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>

What code do I need to pull my actual blog posts into this section?

  • 写回答

1条回答 默认 最新

  • dongxinche1264 2017-04-08 09:48
    关注

    In your snippet the custom query for your posts is missing. Try something like this:

        // WP_Query arguments
        $args = array(
        'post_type' => 'post',
        'post_status' => 'publish'
        );
        $custom_query = new WP_Query( $args );
        <?php if ( $custom_query->have_posts() ) : while ( $custom_query->have_posts() ) : $custom_query->the_post(); ?>
               <article class="post">
                    <header>
                        <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                        <div class="post-details">
                            <i class="fa fa-user"></i><?php the_author_posts_link(); ?>
                            <i class="fa fa-calendar"></i> <?php the_time( 'F jS, Y' ); ?>
                            <i class="fa fa-folder-open-o"></i> <a href=""><?php the_category( ', ' ); ?></a>
                            <i class="fa fa-comments"></i><a href=""><?php comments_popup_link( 'No Comments »', '1 Comment »', '% Comments »' ); ?></a>
    
                        </div><!-- post details -->
                    </header>
    
                    <div class="post-excerpt">
                        <p><?php the_excerpt(); ?> <a href="post.html">continue reading</a></p>
                    </div><!-- post-excerpt -->
    
                    <hr>
    
                </article><!-- end article -->
            <?php endwhile; else : ?>
            <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
            <?php 
      // Restore original Post Data
        wp_reset_postdata();
    
            endif; 
    
    
        ?>
    

    Here you can find an useful tool to generate a Wordpress Query: https://generatewp.com/wp_query/

    Here you can find permitted arguments for Wordpress Query: https://developer.wordpress.org/reference/classes/wp_query/

    To use your custom query remember to call have_posts() and the_posts() methods with your query object ( $custom_query->have_posts() and $custom_query->the_post() in snippet), furthermore is important wp_reset_postdata() to restore main query.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 删除虚拟显示器驱动 删除所有 Xorg 配置文件 删除显示器缓存文件 重启系统 可是依旧无法退出虚拟显示器
  • ¥15 vscode程序一直报同样的错,如何解决?
  • ¥15 关于使用unity中遇到的问题
  • ¥15 开放世界如何写线性关卡的用例(类似原神)
  • ¥15 关于并联谐振电磁感应加热
  • ¥15 this signal is connected to multiple drivers怎么解决
  • ¥60 请查询全国几个煤炭大省近十年的煤炭铁路及公路的货物周转量
  • ¥15 请帮我看看我这道c语言题到底漏了哪种情况吧!
  • ¥66 如何制作支付宝扫码跳转到发红包界面
  • ¥15 pnpm 下载element-plus