如何在WordPress的PHP代码中添加if/else实现首页标题标签切换
解决WordPress首页标题标签切换问题
很简单,咱们只需要在输出标题标签的时候加上WordPress的首页判断函数就行,同时保留你原来的启用判断逻辑。直接看修改后的完整代码:
<?php if ( is_post_extra_title_meta_enabled() ) { ?> <div class="post-header"> <?php if ( is_front_page() || is_home() ) : ?> <h1 class="entry-title"><?php the_title(); ?></h1> <?php else : ?> <h2 class="entry-title"><?php the_title(); ?></h2> <?php endif; ?> </div> <?php } ?>
补充说明:
- 这里用
is_front_page() || is_home()是为了覆盖两种常见的首页场景:is_front_page():匹配你在WordPress后台设置为「首页」的静态页面is_home():匹配默认的博客文章列表首页
如果你确定自己的首页是其中某一种,可以只保留对应的判断条件就行。
- 我们把原来固定的
<h2>标签替换成了if/else结构,满足首页条件时输出<h1>,其他页面保持<h2>。 - 你原来的
is_post_extra_title_meta_enabled()判断逻辑完全保留,只有当这个功能启用时才会渲染整个标题容器。
内容的提问来源于stack exchange,提问作者Namelessandroid
相关产品推荐
相关产品推荐

