简体   繁体   English

WordPress-在“ have_posts”循环中的“ If”首页

[英]Wordpress - Frontpage “If” within “have_posts” while loop

On Wordpress, I have a small block displaying the most recent posts within the common footer of my site. 在Wordpress上,我有一个小块显示我网站通用页脚中的最新帖子。 I want to be able to only show the excepts on the front page, and hide these excepts (showing only the post title) on any other page that uses this common footer. 我希望只能在首页上显示例外,并在使用此公共页脚的任何其他页面上隐藏这些例外(仅显示帖子标题)。

Here is my working code: 这是我的工作代码:

<?php while ( have_posts() ) : the_post(); ?>
    <div class="large-6 columns footer-news-title">
        <article <?php post_class(); ?> id="post-<?php the_ID(); ?>">
                <section class="entry-header">
                        <h5><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h5>
                </section>

                <section class="entry-content">
                        <?php the_excerpt(); ?>
                </section>

        </article>
    </div>
<?php endwhile; ?>

I've used an "If Frontpage" loop in the header, so I thought I should wrap the entry-content section with the "If" code I used before: 我在标头中使用了“ If Frontpage”循环,因此我认为我应该用之前使用的“ If”代码包装入口内容部分:

        <?php if( is_front_page() ) : ?>
            <section class="entry-content">
                    <?php the_excerpt(); ?>
            </section>
        <?php endif;?>

However, this didn't work. 但是,这没有用。 When I wrap the code like above, I get no excerpts on any page. 当我像上面那样包装代码时,在任何页面上都没有摘录。 When I change "is_front_page" to "is_home" or "is_home_page" I get excerpts on all pages. 当我将“ is_front_page”更改为“ is_home”或“ is_home_page”时,我会在所有页面上都摘录。

I've spent time this morning searching around but all I can find are instructions on the use of "If" statements or "While" statement, but nothing on using an "If" inside of a "while" on Wordpress. 今天早上我花了很多时间在搜索中,但是我所能找到的只是有关“ If”语句或“ While”语句使用的说明,但是没有关于在Wordpress的“ while”内部使用“ If”的说明。

Any ideas? 有任何想法吗? Thanks for the help beforehand. 预先感谢您的帮助。

What is the name you gave to your front page when creating the page in WordPress admin? 在WordPress管理员中创建首页时,您给首页指定的名称是什么?

Try: 尝试:

if ( is_page( 'page-name-here' ) ) {
    // your content
}

Combine both home and front page conditions like so: 结合首页和首页条件,如下所示:

if (is_home() || is_front_page()) {
   // your content
}

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

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