【问题标题】:PHP to show/hide slideshows depending on page idPHP 根据页面 ID 显示/隐藏幻灯片
【发布时间】:2019-05-19 22:40:09
【问题描述】:

我想在不同的页面上显示不同的幻灯片

我想我需要使用 if elseif else PHP 循环,但语法不正确

这是我的代码:

<?php if( is_front_page() ) : ?>
 <div class="mobile-header"> <img src="/images/mobile-header.jpg" width="1080" height="1000" alt="Sean Sheehan" /> </div>

<?php  if  ( function_exists( 'soliloquy' ) )  { soliloquy( 'homepage-dark', 'slug' ); } ?>

<?php else : ?>
        <header id="masthead" class="<?php echo is_singular() && twentynineteen_can_show_post_thumbnail() ? 'site-header featured-image' : 'site-header'; ?>">

每当我尝试添加 elseif 时,代码就会中断。我尝试这样做以在第 25 页上显示不同的滑块:

 <?php if( is_front_page() ) : ?>
 <div class="mobile-header"> <img src="/images/mobile-header.jpg" width="1080" height="1000" alt="Sean Sheehan" /> </div>

<?php  if  ( function_exists( 'soliloquy' ) )  { soliloquy( 'homepage-dark', 'slug' ); } ?>

 <?php   elseif ( is_page(25) ) { 
?>
 <div class="mobile-header"> <img src="/images/mobile-header.jpg" width="1080" height="1000" alt="Sean Sheehan" /> </div>

<?php  if  ( function_exists( 'soliloquy' ) )  { soliloquy( 'photography-dark', 'slug' ); } ?>

<?php else : ?>

<header id="masthead" class="<?php echo is_singular() && twentynineteen_can_show_post_thumbnail() ? 'site-header featured-image' : 'site-header'; ?>">

但是这段代码不正确。

【问题讨论】:

  • 请提供更多详细信息,例如代码如何中断:您看到了哪些错误消息?
  • 解析错误:语法错误,意外'{',期待':'在/homepages/4/d554888208/htdocs/sean/wp-content/themes/twentynineteen-child/header.php 在线61

标签: php wordpress


【解决方案1】:

主要问题有:

  • 虽然您可以将{}: 结构用于if 语句,但您不能在同一控制块中混合它们(此处为details)。由于您从if(): 开始,因此您不能在&lt;?php elseif ( is_page(25) ) { 中使用elsif() {。您需要使用 elsif(): 格式,因此该行需要像这样读取 - &lt;?php elseif ( is_page(25) ):

  • 你需要用endif;关闭块

所以修正后的代码(见下面的 cmets)应该是这样的:

<?php if( is_front_page() ) : ?>
 <div class="mobile-header"> <img src="/images/mobile-header.jpg" width="1080" height="1000" alt="Sean Sheehan" /> </div>

<?php  if  ( function_exists( 'soliloquy' ) )  { soliloquy( 'homepage-dark', 'slug' ); } ?>

<!-- elseif(): not { -->
<?php   elseif ( is_page(25) ):?>

 <div class="mobile-header"> <img src="/images/mobile-header.jpg" width="1080" height="1000" alt="Sean Sheehan" /> </div>

<?php  if  ( function_exists( 'soliloquy' ) )  { soliloquy( 'photography-dark', 'slug' ); } ?>

<?php else: ?>

<header id="masthead" class="<?php echo is_singular() && twentynineteen_can_show_post_thumbnail() ? 'site-header featured-image' : 'site-header'; ?>">
    <!-- don't forget endif; -->
    <?php endif; ?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-07
    • 1970-01-01
    • 1970-01-01
    • 2012-09-02
    • 2021-03-03
    • 2023-03-22
    • 1970-01-01
    • 2014-01-25
    相关资源
    最近更新 更多