Wordpress Main Menu - Hide 'Home' link on homepage only - php

I need to hide the "Home" link that's generated from Wordpress main menu on the homepage, and show it on the rest of the site.
I tried creating my own menu with no "Home" link and adding the "Home" link manually on the header.php file but it goes to the end of the menu and does not look like a pretty solution.
Any ideas? Using latest Wordpress 3.2

If you only want to hide it to the users, i suggest using the following CSS:
body.home a[title="Home"] {
display: none;
}
Explanation: Wordpress generates several classes for the body tag. The home class is used to hide all links with the title Home on the homepage.
Working Example (code taken from the default theme): http://jsfiddle.net/yJVyK/1/
Note: The attribute selector does not work in IE6

There is an another solution with PHP which more correct way in my opinion.
add_filter( 'wp_nav_menu_objects', 'amc_filter_menu', 10, 2 );
/**
* Filters to remove Home Link on Front Page
*/
function amc_filter_menu( $objects, $args ) {
// Return Default Value if the Menu isn't Main Menu
// Replace "Navigation_location" with your target location
if ( 'Navigation_location' !== $args->theme_location ) {
return $objects;
}
// Detect the Menu which equeal site URL
foreach ( $objects as $key => $object ) :
if ( get_site_url( null, '/' ) === $object->url && is_front_page() || get_site_url() === $object->url && is_front_page() ) :
unset( $objects[ $key ] );
endif;
endforeach;
// Return the menu objects
return $objects;
}
Source

Related

Adding a dynamic logo to Genesis primary nav

I would like to add a logo ( get_custom_logo() ) in the Genesis navigation bar but when it's blank, there's to be text. I've achieved this in a similar fashion for the header but cannot for the primary nav. The end result DOES work but when the user goes to the "customize" part of "site identity" the change is not visible, until you hit "publish" then refresh the actual page.
I need the change to be seen in "customize" mode as the logo is being changed. Here's the code the semi-functions...
add_filter( 'wp_nav_menu_items', 'prefix_add_menu_item', 10, 2 );
/**
* Add Menu Item to end of menu
*/
function prefix_add_menu_item ( $items, $args ) {
if( $args->theme_location == 'primary' ) {
if (( has_custom_logo())) {
$thelogo = get_custom_logo();
$items .= '<li class="menu-item">' . $thelogo . '</li>';
} else {
$items .= '<li class="menu-item">Theme Name</li>';
return $items;
}
return $items;
}
}
Ideally, I'd like to apply this method throughout the site for other hooks. If anyone can point to what I'm doing wrong, that would help.
Cheers.
It won't refresh in preview unless you add a custom setting to $wp_customize for your image or you add a new PHP class for the site logo. Either way, it's not an easy fix.

Add custom css class for the search page filter

I'm trying to add a custom CSS class for the search page that uses WooCommerce Product Search.
Search pages path is like:
domain.com/shop/ixwpss=3&title=0&excerpt=0&content=0&categories=0&attributes=0&tags=1&sku=0&v=a3420e5a4c03
When I search for the number 3 that represents the tag for products.
I managed to add the class for the whole shop, with the following function but I want to add only for the search page. Can someone help me?
add_filter( 'body_class', 'add_body_classes' );
function add_body_classes( $classes ) {
global $post;
if ( is_shop() )
$classes[] = 'search';
return $classes;
}
Please update the code like below
add_filter( 'body_class', function( $classes ) {
if ( is_shop() ) {return array_merge( $classes, array( 'class-name' ) );}
}
Assuming that the search page is using the archive-product.php
If you are using a WooCommerce compatible theme then you have to go in your themes folder -> woocommerce and find the archive-product.php. If the override file does not exists then go to wp-content/plugins/woocommerce/templates/archive-product.php
And a if statement to check if is search or not like below:
if ( is_search() ) {
<body class="your-class">
} else {
<body class="regular-class">
// the content that is already in that file (archive-product.php)
}
*if the part (body class in your case) is not there you have to locate it and do the same (probably it's in your header.php)

How to repair the top navigation bar sticky position?

I use a WP 5.3 Twenty Seventeen child theme, but I don't want to edit templates, so I added a needed second (in fact it became the first in the displaying order) div wrap section to the top navigation menu with the bellow function. The problem is that I lost after this the fixed position of the menu (it is not sticky anymore now). How to remediate this? I played with the CSS code, but without success. The site in discussion is here.
function my_navigation_top( $slug, $name ) {
if( $name == 'top' ) { ?>
--- some content here ---
</div> <!-- wrap closed -->
<div class="wrap"> <!-- wrap opened -->
<?php }
}
add_action( 'get_template_part_template-parts/navigation/navigation', 'my_navigation_top', 10, 2 );
UPDATE
If instead of a second div wrap section I add a simple div section inside the wrap, the problem doesn't appear.
I am looking at the script file that sets the fixed class (that makes the top nav sticky when scrolling down). (File: twentyseventeen/assets/js/global.js)
And It seems the fixed class is not set if the height of the nav is too tall:
// Set properties of navigation.
function setNavProps() {
navigationHeight = $navigation.height();
navigationOuterHeight = $navigation.outerHeight();
navPadding = parseFloat( $navWrap.css( 'padding-top' ) ) * 2;
navMenuItemHeight = $navMenuItem.outerHeight() * 2;
idealNavHeight = navPadding + navMenuItemHeight;
navIsNotTooTall = navigationHeight <= idealNavHeight;
}
// Make navigation 'stick'.
function adjustScrollClass() {
// Make sure we're not on a mobile screen.
if ( 'none' === $menuToggle.css( 'display' ) ) {
// Make sure the nav isn't taller than two rows.
if ( navIsNotTooTall ) {
// When there's a custom header image or video, the header offset includes the height of the navigation.
if ( isFrontPage && ( $body.hasClass( 'has-header-image' ) || $body.hasClass( 'has-header-video' ) ) ) {
headerOffset = $customHeader.innerHeight() - navigationOuterHeight;
} else {
headerOffset = $customHeader.innerHeight();
}
// If the scroll is more than the custom header, set the fixed class.
if ( $( window ).scrollTop() >= headerOffset ) {
$navigation.addClass( navigationFixedClass );
} else {
$navigation.removeClass( navigationFixedClass );
}
} else {
// Remove 'fixed' class if nav is taller than two rows.
$navigation.removeClass( navigationFixedClass );
}
}
}
I am not entirely certain what the solution would be. One idea (which is not ideal) is to adapt the .js file and put true between the ( ) instead of navIsNotTooTall on this line:
if ( navIsNotTooTall ) {
If you want to add this change to the child theme, instead of the main theme: Put the changed global.js file in your child-theme directory here: twentyseventeen-child/assets/js/global.js
Then add this to functions.php of the child-theme:
add_action( 'wp_enqueue_scripts', 'override_globaljs_script', 100 );
function override_globaljs_script()
{
wp_dequeue_script( 'twentyseventeen-global' );
wp_deregister_script( 'twentyseventeen-global' );
wp_enqueue_script( 'twentyseventeen-global-child', get_stylesheet_directory_uri() . '/assets/js/global.js' );
}
It will unregister the main theme global.js and register it for the child theme. This is the 'proper' Wordpress way to do it as far as I am concerned.

Hide/replace menu item when user is logged in/out

I want to be able to hide or even replace the 'My account' button when users are logged out and I want to be able to hide or replace the 'Registration' button when users are logged in.
How would I go about doing this? I'm still a amateur at WordPress and I'm still learning, this is what I have so far in my nav-menus.php file.
if( is_user_logged_in() ) {
wp_nav_menu( array( 'My Account' => 'logged-users' ) );
} else {
wp_nav_menu( array( 'Registration' => 'not-logged-users' ) );
}
I know this isn't correct.
First, you need to create both menus, go to Appearance » Menus, create the two menus logged-in and logged-out.
After creating the menus, add this code in your theme’s functions.php file or a site-specific plugin:
function my_wp_nav_menu_args( $args = '' ) {
if( is_user_logged_in() ) {
$args['menu'] = 'logged-in'; //This value stands for the actual name you give to the menu when you create it.
} else {
$args['menu'] = 'logged-out';
}
return $args;
}
add_filter( 'wp_nav_menu_args', 'my_wp_nav_menu_args' );
}
That's all.
If you only need to hide them, then I would do this mainly via CSS. WP adds the class logged-in to the body element when there is a logged-in user, so you can use that to format elements inside of body differently.
Add classes to your menu items via the admin backend, like for example hide-when-logged-in and hide-when-not-logged-in.
Then you can use
body.logged-in .hide-when-logged-in,
body:not(logged-in) .hide-when-not-logged-in {
display: none;
}
in your stylesheet to hide those elements under the appropriate condition.

Is there is a function that checks if a menu page is loaded in wordpress?

I created a menu to wordpress dashboard
add_menu_page( __('locations'), __('Locations'), 'manage_options', 'manage-locationss', 'locations_page');
function locations_page() {
require_once(locations.php);
}
I want to add a style sheet to this page only , Is there is a function that cheks if this page is opened then loads a style sheet , Like this function is_page_template().
Or I have to add the css using wp_enqueue_style() without any checks?
If i understand what you try to achieve, you need something like that
if(is_page( 'location' )){ // your name of your page
wp_enqueue_style() // do stuff with you right parameters and files
}
if(is_page( 'location' )){} // Checks if the Page with a post_title of "Location" is being displayed. In this way You can Check whatever page you need.
You can use WP Screen and get_current_screen():
$current_screen = get_current_screen();
if ( $current_screen->id === 'location' ) {
// Your code
}
https://codex.wordpress.org/Class_Reference/WP_Screen
https://codex.wordpress.org/Function_Reference/get_current_screen

Categories