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 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配