【问题标题】:how to Wordpress slider code in header show only in homepage如何将标题中的 Wordpress 滑块代码仅显示在主页中
【发布时间】:2017-12-26 23:05:54
【问题描述】:

嗨,我是 wordpress 定制的新手,这是滑块代码,我想将此代码插入到标题中并仅显示在主页上,请救救我

 <?php

$slider_option = get_theme_mod('wp_store_homepage_setting_slider_option',0);
if ($slider_option == '1'):
    do_action('wp_store_slider_section'); // Slider section- this function is in wp-store-function.php
endif;
?>

【问题讨论】:

  • 我已经用 css .home .myslide { display:block;} .myslide { display:none;}

标签: php jquery wordpress slider nivo-slider


【解决方案1】:

创建(如果您没有)“主页”页面模板会更简单,这是一个相当简单的 PHP 页面,带有特定的 HEADING,因此您可以在创建/编辑页面时选择该模板.

然后在该代码中添加您希望滑块显示的部分,您可以轻松绕过这些“is_home”或“is_frontpage”子句。

例子:

<?php
/*
Template Name: NAME-OF-TEMPLATE
Author: NAME OF AUTHOR
Web Site: author url
Contact: author email
*/
get_header(); ?>
<!-- Get nav bar -->
<?php get_template_part( 'navigation', 'default' ); ?>
<!-- Start of page content -->
<div id="primary" class="site-content">
    <div id="content" role="main">
        <article id="post-0" class="post">
            <header class="entry-header">
<!-- Page Title/head if needed -->
            <!-- <h1 class="entry-title"><?php echo get_the_title(); ?></h1> -->
<!-- Your Code snippet -->
<?php
$slider_option = get_theme_mod('wp_store_homepage_setting_slider_option',0);
if ($slider_option == '1'):
    do_action('wp_store_slider_section'); // Slider section- this function is in wp-store-function.php
endif;
?>
            </header>
<!-- Main content -->
            <div class="entry-content">
<!-- Rest of your content and page structure -->
            </div><!-- .entry-content -->
        </article><!-- #post-0 -->
    </div><!-- #content -->
</div><!-- #primary -->
<?php get_footer(); ?>

请参考这个简单的教程来帮助您熟悉自己:https://www.cloudways.com/blog/creating-custom-page-template-in-wordpress/

和 WP 参考: https://developer.wordpress.org/themes/template-files-section/page-template-files/#creating-page-templates-for-specific-post-types

【讨论】:

    【解决方案2】:

    // 使用以下代码,is_front_page() 在查看站点首页时返回 true。

     <?php if(is_front_page()) { 
            $slider_option = get_theme_mod('wp_store_homepage_setting_slider_option',0);
            if ($slider_option == '1'):
            do_action('wp_store_slider_section'); // Slider section- this function is in wp-store-function.php
            endif;
        } ?>
    

    【讨论】:

      【解决方案3】:

      你可以试试is_home()is_front_page() 来判断是否是首页

      if ( is_home() || is_front_page() ) {
          $slider_option = get_theme_mod('wp_store_homepage_setting_slider_option',0);
           if ($slider_option == '1'):
           do_action('wp_store_slider_section'); // Slider section- this function is in wp-store-function.php
           endif;
      } else {
          // Display what you want if not home
      }
      

      在这里寻找reference: is_home
      在这里寻找referenceis_front_page

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-01-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多