douluozhan4370 2017-04-07 22: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 01: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 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部