如何使页眉只显示在主页上?Wordpress


How to make header show only on home page? Wordpress

这是我用wordpress:编辑的网站

www.thestudentbubble.com

正如你所看到的,有一个"标题",这3行解释了网站的内容。如何使其仅显示在主页上?

我找到了Header.php文件,当前代码如下:

<?php if ( get_header_image() ) : ?>
    <a href="<?php echo esc_url( home_url( '/' ) ); ?>">
              <img src="<?php header_image(); ?>" class="header-image" width="<?php echo get_custom_header()->width; ?>" height="<?php echo get_custom_header()->height; ?>" alt="" /></a>
    <?php endif; ?>

试试这个:

<?php
if ( is_home() ) {
    // This is a homepage
} else {
    // This is not a homepage
}
?>

Wordpress is_home()函数。您也可以使用is_frontpage()

<?php
if ( is_front_page() ) {
    // This is a homepage
} else {
    // This is not a homepage
}
?>

关于这些之间的区别的更多信息:

if ( is_front_page() && is_home() ) {
  // Default homepage
} elseif ( is_front_page() ) {
  // static homepage
} elseif ( is_home() ) {
  // blog page
} else {
  //everyting else
}

使用is_home函数检查您是否在主页中。

<?php if (is_home()): ?>
    <!-- ... -->
<?php endif; ?>

将代码放入这样的if条件中--

if( is_front_page())
{
   ///put code here to show only on home page header 
}

一个更整洁的解决方案是将现有条件(如果失败,可能会很好地回退)更改为:

<?php if ( get_header_image() && is_home() ) : ?>

我希望Wordpress模板的标题图像只显示在主页上,而在任何其他页面上显示。

我不得不编辑模板的header.php文件并添加:

<!-- Custom Header - Start -->
<?php if ( is_front_page() ) :   ?>
   $header_image = get_header_image();
   [...]
<?php endif; // end check for featured image or standard header ?>