Adding a dynamic logo to Genesis primary nav - php

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.

Related

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.

Wordpress custom plugin - Select post status on default

I am creating my own plugin in Wordpress. With register_post_type I can view my own posts. I also created a post status with register_post_status. When I go to the summary page I can see all posts and filter on the status.
The "problem": When I go to the summary page, I can see all posts and the filters. The "All" status is always selected on default, but I want to select my custom status on default. Is this possible? Or change the URL in the menu to post_status=&post_type= ? I am talking about the admin side.
Hope someone can help me, because I can't figure it out.
I have fixed it with this code - it changes the url in the menu:
add_action( 'admin_menu', 'wpse_admin_menu', 100 );
function wpse_admin_menu()
{
global $menu, $submenu;
$parent = 'parent';
if( !isset($submenu[$parent]) )
return;
foreach( $submenu[$parent] as $k => $d ){
if( $d[0] == 'name' )
{
$submenu[$parent][$k][2] = 'edit.php?post_status=status&post_type=type';
break;
}
}
}

How to replace My Account and add login/logout links to menu in Woocommerce?

I am using woocommerce and I add the one menu My Account using woocommerce plugin But I want to show the menu login and logout instead of My Account which I add the into menu. I also added the script into functions.php result is same.
add_filter( 'wp_nav_menu_items', 'add_loginout_link', 10, 2 );
function add_loginout_link( $items, $args ) {
if (is_user_logged_in() && $args->theme_location == 'primary_navigation') {
//echo "hello friend how are";
$items .= '<li>Log Out</li>';
}
elseif (!is_user_logged_in() && $args->theme_location == 'primary_navigation') {
$items .= '<li>Log In</li>';
}
return $items;
}
When I am using jupitor theme and when I see the theme location of My Account Menu I get
Primary Navigation
I have a one doubt is I have to add the My Account menu first then I add the login and logout menu.
Your above code will add login/logout links to menu. You do not have to add My Account menu.
Please check your theme_location. If theme location is correct then login/logout link will add to menu.
For check theme_location you have to search register_nav_menus in functions.php file of your theme. If you find it in functions.php file then you can see theme_location inside register_nav_menus code.

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

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

Categories