将if语句添加到wordpress中的php中的类

时间:2018-05-23 22:40:27

标签: php wordpress wordpress-theming

我有以下代码:

<?php if ( is_post_extra_title_meta_enabled() ) { ?>
<div class="post-header">
<h2 class="entry-title"><?php the_title(); ?></h2>
</div>
<?php } ?>

我希望将类H1设为在wordpress的首页/主页上。我该如何为此添加if / else命令?所以基本上:

If on front page:
<h1 class="entry-title"><?php the_title(); ?></h1>

Else:
 <h2 class="entry-title"><?php the_title(); ?></h2>

2 个答案:

答案 0 :(得分:0)

要确定您是否在wordpress的主页上,可以使用is_home()功能。

因此,如果您希望在您提供的原始if语句中添加if else,那么您的代码就会这样,

<?php if ( is_post_extra_title_meta_enabled() ) { ?>
    <div class="post-header">
        <?php if ( is_home() ) { ?>
            <h1 class="entry-title"><?php the_title(); ?></h1>
        <?php } ?>
        <?php else { ?>
            <h2 class="entry-title"><?php the_title(); ?></h2>
        <?php } ?>
    </div>
<?php } ?>

我偏向于使用替代语法,所以这里是重写的代码。

<?php if ( is_post_extra_title_meta_enabled() ): ?>
    <div class="post-header">
        <?php if ( is_home() ): ?>
            <h1 class="entry-title"><?php the_title(); ?></h1>
        <?php else: ?>
            <h2 class="entry-title"><?php the_title(); ?></h2>
        <?php endif; ?>
    </div>
<?php } ?>

答案 1 :(得分:0)

取决于您的WP配置,您可以使用is_front_page()或is_home()(如Ryan所述)