简体   繁体   中英

Display menu if site is homepage (wordpress)

On site I have left menu, which appears on almost every subpage

<div class="category_menu">
    <nav class="cat_menu">
        <div class="menu_title parent_cat_name">
            <h6><?php echo $category->name; ?></h6>
        </div>
        <?php rs_left_menu_subcats($subcategories); ?>
    </nav>
</div>

I would like do display the another menu if site is homepage. In other cases it should be menu from code I pasted.

What code may I use?

You can combine is_front_page() and is_home() Wordpress function.

<?php if( is_front_page() && is_home() ) { ?>
    // you are on homepage, show your another menu
<?php } else { ?>
<div class="category_menu">
    <nav class="cat_menu">
        <div class="menu_title parent_cat_name">
            <h6><?php echo $category->name; ?></h6>
        </div>
        <?php rs_left_menu_subcats($subcategories); ?>
    </nav>
</div>
<?php } ?>

If you want to display specific content on wordpress homepage than you can do it using is_front_page()

if ( is_front_page() ) {    
    // your menu code goes here for home page
}

Variant with php -- answer by @hardik solanki

Variant with CSS:

.home .category_menu { display: block; } 

Since is_front_page() and is_home() for me doesn't work (WP 5.2.3), I write this condition:

global $wp;  
$current_url = home_url(add_query_arg(array($_GET), $wp->request));
if ($current_url==get_site_url()) { 
    // code for homepage 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM