简体   繁体   English

仅包含 1 篇博文的自定义主页

[英]Custom Homepage with 1 blog post only

I want to set a blog as homepage.我想将博客设置为主页。 I created a front-page.php with the code below.我用下面的代码创建了一个front-page.php。

The code shows the recent post as homepage.该代码将最近的帖子显示为主页。 I want to use 1 specific blog as homepage instead of the recent blogs or sticky posts.我想使用 1 个特定博客作为主页,而不是最近的博客或粘性帖子。

Here's the sample site.这是示例站点。 The blog is used as homepage.博客用作主页。 The homepage doesn't change if new blogs are published.如果发布了新博客,主页不会更改。

https://height-comparison.com/ https://height-comparison.com/

Original post: How to make Wordpress show only one post on the front page with comments, and comment form?原帖: 如何让Wordpress在首页只显示一篇带有评论和评论表格的帖子?

<?php get_header(); ?>
<div class="blog">
    <section>
        <div class="container">
            <div class="row">
                <div class="blog-sidebar">
                    <?php get_sidebar(); ?>
                </div>
                <div class="span8">
                <?php $articles = new WP_Query(array(
                                    'post_type' => 'post',
                                    'post_status' => 'publish',
                                    'posts_per_page' => 1
                                ));?>
                    <?php  while ($articles->have_posts()): $articles->the_post(); ?>
                        <article id="post-<?php the_ID(); ?>" role="article">
                            <header>
                                <a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>"><?php the_post_thumbnail( 'wpbs-featured' ); ?></a> 
                            <section class="post_content clearfix" >
                                 <?php the_content(); ?>
                            </section> 
                        </article>
                    <?php endwhile; ?> 
                  <?php comments_template('',true); ?>
                </div>
            </div>
        </div>
    </section>
</div>

<?php get_footer(); ?>

you can use sticky posts example wpbeginner sticky posts您可以使用置顶帖子示例wpbeginner 置顶帖子

<?php get_header(); ?>
<div class="blog">
    <section>
        <div class="container">
            <div class="row">
                <div class="blog-sidebar">
                    <?php get_sidebar(); ?>
                </div>
                <div class="span8">
                <?php 

                /* Get all sticky posts */
                $sticky = get_option( 'sticky_posts' );
                 
                /* Sort the stickies with the newest ones at the top */
                rsort( $sticky );
                 
                /* Get the 5 newest stickies (change 5 for a different number) */
                $sticky = array_slice( $sticky, 0, 5 );
                 
                /* Query sticky posts */



                $articles = new WP_Query(array(
                                    'post_type' => 'post',
                                    'post_status' => 'publish',
                                    'post__in' => $sticky,
                                    'ignore_sticky_posts' => 1,
                                    'posts_per_page' => 1
                                ));?>
                    <?php  while ($articles->have_posts()): $articles->the_post(); ?>
                        <article id="post-<?php the_ID(); ?>" role="article">
                            <header>
                                <a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>"><?php the_post_thumbnail( 'wpbs-featured' ); ?></a> 
                            <section class="post_content clearfix" >
                                 <?php the_content(); ?>
                            </section> 
                        </article>
                    <?php endwhile; ?> 
                  <?php comments_template('',true); ?>
                </div>
            </div>
        </div>
    </section>
</div>

<?php get_footer(); ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM