【问题标题】:Custom Homepage with 1 blog post only仅包含 1 篇博文的自定义主页
【发布时间】:2021-08-05 12:57:40
【问题描述】:

我想将博客设置为主页。我用下面的代码创建了一个front-page.php。

代码将最近的帖子显示为主页。我想使用 1 个特定博客作为主页,而不是最近的博客或置顶帖子。

这是示例网站。博客用作主页。如果发布了新博客,主页不会更改。

https://height-comparison.com/

原帖: How to make Wordpress show only one post on the front page with comments, and comment form?

<?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(); ?>

提前谢谢你。

【问题讨论】:

  • 根本不清楚你在问什么。请参阅How to Ask 并接听tour

标签: wordpress blogs website-homepage


【解决方案1】:

您可以使用置顶帖子 例如wpbeginner sticky posts

<?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(); ?>

【讨论】:

  • 我正在努力实现这一目标height-comparison.com。博客设置为首页,如果有新博客发布,它不会改变。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-06
相关资源
最近更新 更多