Genesis Menu duplicating instead of moving - php

I am working on a genesis site where I need to move the standard nav menu to above the header. I'm using the following code in the functions.php file of my child theme:
remove_action( 'genesis_after_header', 'genesis_do_nav' );
add_action( 'genesis_before_header', 'genesis_do_nav' );
It's adding a nav menu before the header, but not removing the one after the header. The "after header" one is correctly placed in the output, so I know I'm using the correct hook on the "remove_action". There is nothing else in my functions.php file that is dealing with the nav other than specifying a menu for the footer and adding the descriptions. Below is all the code in my functions.php file (skipping a large section dealing with column shortcodes):
add_action('genesis_setup','child_theme_setup', 15);
function child_theme_setup() {
//Add Homepage Sidebar
genesis_register_sidebar( array( 'name' =>
'Home Sidebar', 'id' => 'home-sidebar' ) );
//Adds footer widgets
add_theme_support( 'genesis-footer-widgets', 6 );
//Adds Footer Text Replace
remove_action( 'genesis_footer', 'genesis_do_footer' );
remove_action('genesis_footer', 'genesis_footer_markup_open', 5);
remove_action('genesis_footer', 'genesis_footer_markup_close', 15);
add_action( 'genesis_after', 'be_footer' );
}
//Function to replace the footer text and copyright
function be_footer() {
echo '<div id="footer" class="footer"><div class="footer-wrap"><div class="left"><p>© Copyright ' . date('Y') . ' RC Auto |Watrous Media</p></div>';
echo '<div class="right">';
wp_nav_menu( array( 'menu' => 'footer' ) );
echo '</div></div></div>';
}
//Add Menu Descriptions
function be_add_description( $item_output, $item ) {
$description = $item->post_content;
if (' ' !== $description )
return preg_replace( '/(<a.*?>[^<]*?)</', '$1' . '<span>' . $description . '</span><', $item_output);
else
return $item_output;
}
add_filter( 'walker_nav_menu_start_el', 'be_add_description', 10, 2 );
// Use shortcodes in widgets
add_filter( 'widget_text', 'do_shortcode' );
/** Move primary nav menu to before header for mobile support*/
remove_action( 'genesis_after_header', 'genesis_do_nav' );
add_action( 'genesis_before_header', 'genesis_do_nav' );
Here's a screenshot:
The Main menu is the styled one added in a widget as I needed it in the header. The main menu I am planning on using as the responsive mobile menu. Anyone know why the lower nav is not removing?
Thanks for your help.

I also had this issue, adding the following code at the start of the child theme's functions.php file fixed it:
require_once( get_template_directory() . '/lib/init.php' );
You can also move remove_action( 'genesis_after_header', 'genesis_do_nav' ); inside your child_theme_setup() function.
Requiring init.php will ensure that the genesis framework is initialized first, so everything after it in your functions file will be able to reference genesis functions. If you are using the genesis_setup action, you'll need to make sure functions that require the genesis framework for reference are run inside that function.

Related

Wordpress custom demo sidebar not appearing on posts

Following php file gets a custom demo sidebar to show up in the admin widget menu, but not on actual posts (file located in folder with same name, which is located in the plugin folder in WP file directory) – add a text widget to custom sidebar to test:
<?php
/**
* Plugin Name: Single Post CTA
* Plugin URI: https://github.com/cdils/single-post-cta
* Description: Adds sidebar (widget area) to single posts
* Version: 0.1
* Author: Carrie Dils
* Author URI: https://carriedils.com
* License: GPL v2+
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: spc
*/
// If this file is called directly, abort
if ( !defined( 'ABSPATH' ) ) {
die;
}
/**
* Load stylesheet
*/
function spc_load_stylesheet() {
if ( is_single() ) {
wp_enqueue_style( 'spc_stylesheet', plugin_dir_url(__FILE__) .'spc-styles.css' );
}
}
// Hook stylesheet
add_action( 'wp_enqueue_scripts', 'spc_load_stylesheet' );
// Register a custom sidebar
function spc_register_sidebar() {
register_sidebar( array(
'name' => __( 'Single Post CTA', 'spc' ),
'id' => 'spcsidebar',
'description' => __( 'Displays widget area on single posts', 'spc' ),
'before_widget' => '<div class="widget spc">',
'after_widget' => '</div>',
'before_title' => '<h2 class="widgettitle spc-title">',
'after_title' => '</h2>',
) );
}
// Hook sidebar
add_action( 'widgets_init', 'spc_register_sidebar' );
// Display sidebar on single posts
function spc_display_sidebar( $content ) {
if ( is_single() ) {
dynamic_sidebar( 'spcsidebar' );
}
return $content;
}
// Add dynamic sidebar
add_filter( 'the content', 'spc_display_sidebar' );
Here is the associated style sheet located in the same folder as the file for the custom sidebar:
.spc {
background: gray;
color: blue;
}
The widgets menu under customizer says “Your theme has 1 other widget area, but this particular page doesn’t display it”. This WordPress guide https://developer.wordpress.org/themes/functionality/sidebars/ appears to indicate that one has to register the sidebar/widget in the theme or child theme’s functions.php file and then create a sidebar-{name}.php file in which to run the dynamic_sidebar function. Is this the way instead? I’m using the Genesis Sample child theme, and switching to 2020 and 2017 wordpress themes, or deactivating all other plugins has not fixed problem.
The filter should be add_filter( 'the_content', 'spc_display_sidebar' );. You forgot the underscore.
If you are trying to display the sidebar in a page post type thenis_single() is not going to work. Try is_singular()instead.
Mistack is in hook not "the content", use "the_content"
Ex.
add_filter( 'the_content', 'spc_display_sidebar' );

Get template part in WordPress plugin

I'm trying to move a WordPress plugin from my child theme to a custom plugin. The current code is using get_template_part which works on the child theme but not on the custom WordPress plugin.
I have tried to replace it for "include" but it doesn't work.
function arbolesplantados_endpoint_content() {
get_template_part('arbolesplantados'); //I think the problem is here
}
add_action( 'woocommerce_account_arbolesplantados_endpoint', 'arbolesplantados_endpoint_content' );
function arbolesplantados_account_menu_items( $items ) {
$my_items = array(
'arbolesplantados' => __( 'Gestionar árboles plantados', 'woocommerce' )
);
$my_items = array_slice( $items, 0, 1, true ) +
$my_items +
array_slice( $items, 1, count( $items ), true );
return $my_items;
}
add_filter( 'woocommerce_account_menu_items', 'arbolesplantados_account_menu_items');
The previous code is supposed to create a page with the woocommerce menu (my account) on the left and then on the right print the custom form that's inside a file called arbolesplantados.php which is in the root folder of my plugin
You missed to register endpoint for the custom page. You can register endpoint like this.
function wpso_add_my_account_endpoint() {
add_rewrite_endpoint( 'arbolesplantados', EP_PAGES );
}
add_action( 'init', 'wpso_add_my_account_endpoint' );

Wordpress Custom Plugin CSS And <li> Tag

I have an issue while trying to apply a class on an li tag from my custom plugin css. I load my plugin after the theme is setup.
add_action('after_setup_theme', 'run_menufix');
Still the li tag is being controlled by the theme css. Is there anything I can do here to make the plugin css to take control of the li tag? Thanks for anyput.
You can try like this to add your own classes
function main_menu() {
add_theme_support('menus');
register_nav_menus(array(
'primary' => __('Main Menu'),
));
register_nav_menus(array(
'secondary' => __('Secondary Menu'),
));
}
function menu_item_class( $classes, $item, $args) {
unset($classes);
$classes = ["col-xs-6","col-sm-6","col-md-2","filter"];
return $classes;
}
//Menu
add_action('after_setup_theme', 'main_menu');
add_filter('nav_menu_css_class', 'menu_item_class', 1, 3);
First I dequeued the theme styles and enqued added plugin styles. When I checked the html source I found that dequeued files are coming first followed by plugin file. Following code is my solution.
wp_dequeue_style( 'child-style' );
wp_dequeue_style( 'main-styles' );
//re-enqueue the stylesheet
wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/menufix-public.css', array('child-style','main-styles'), $this->version, 'all' );

enqueing stylesheets for a child theme in wordpress

I have created a child theme in wordpress off of a parent theme called bigpont. I am also using woocommerce as well on the site. I had enqued my child theme style sheet and have noticed it loads twice and I'm not sure why. I am also wondering how I can get it to load so it overrides the woocommerce style sheet. Here is the code I'm currently using in my functions.php file:
function my_theme_enqueue_styles() {
$parent_style = 'bigpoint-css';
wp_enqueue_style( $parent_style, get_template_directory_uri() . '/base.css' );
wp_enqueue_style( 'child-style',
get_stylesheet_directory_uri() . '/style.css',
array( $parent_style ),
wp_get_theme()->get('Version')
);
}
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles');
and here is how the stylesheets are being loaded on my site
It seems to load as 'bigpoint-default-css' and then again as I added it "child-style-css'
****UPDATE: I found the answer to my css being loaded twice, in my parent theme's functions.php file it being called in with :
wp_register_style('bigpoint-default', get_stylesheet_uri(), '1.0');
so I used this to undo that:
function unhook_parent_style() {
wp_dequeue_style( 'bigpoint-default' );
wp_deregister_style( 'bigpoint-default' );
}
add_action( 'wp_enqueue_scripts', 'unhook_parent_style', 20 );
From looking at the file class-wc-frontend-scripts.php it looks like WooCommerce enqueues it scripts/styles with the default priority of 10.
public static function init() {
add_action( 'wp_enqueue_scripts', array( __CLASS__, 'load_scripts' ) );
add_action( 'wp_print_scripts', array( __CLASS__, 'localize_printed_scripts' ), 5 );
add_action( 'wp_print_footer_scripts', array( __CLASS__, 'localize_printed_scripts' ), 5 );
}
So if you enqueue your scripts with a lower priority they it will load after the WooCommerce style and overwrite that stylesheet as the file will be loaded after the WooCommerce one lower down the HTML document.
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles', 100 );
I would need more information to debug what is happening with the duplicate stylesheets though.

Advanced Custom Fields, Yoast SEO, not updating new values in WordPress

I have a problem: Every time I make changes to the SEO fields, the ACF fields, and Multiple content blocks on pages and try to click update, they won't save (Basically, fields that are added through customisations).
DETAILS:
I am just starting on making a theme, based on the starkers theme Blank slate (https://github.com/viewportindustries/starkers) and haven't gone that far yet.
I am running on Localhost via easyPHP
This problem is mostly Prevalent to Pages not posts (Posts save SEO details, thankfully).
Changes to the Body content for pages still save.
I am suspecting it's something bad I did or something I haven't included yet on my functions.php file--- if there are any PHP experts here that could take a look, please, I'm begging your help. The functions.php code is just below:
Workaround: For ACF (Custom Fields) Entering Custom fields details on the generic WP custom field works (which isolates the problem on customisations on the theme). BUT this does not help the problem with WP SEO.
Workaround: For WP SEO, I had to change to another theme (premium theme, Batakoo) edit the SEO, and the SEO saves just fine. Switching back to the theme I was building, the SEO fields become read-only again.
I can't live on workarounds forever, I need fixes, so if anyone can help, please do.
THINGS THAT I'VE TRIED
Just to make sure we're on the same page and no one has to comment solutions that I've tried, here are the things I've already tried (but did not work) based on searching WP.Org for solutions to "Fields don't save on WordPress Pages":
I've had the database tables repaired via PhpMyAdmin
Removed extra spaces and additional comments in functions.php
disabled all plugins except the ones I'm testing (SEO)
<?php
require_once( 'external/starkers-utilities.php' );
/* Theme settings */
define('WT_DIR', get_template_directory_uri());
define('WT_TEMPLATE_DIR', get_template_directory());
add_theme_support('post-thumbnails');
register_nav_menus(array('wt-prime-menu' => 'Main Menu'));
function wt_nav() {
wp_nav_menu( array('theme_location'=> 'wt-prime-menu','container'=> false,'menu_class'=> 'nav navbar-nav navbar-right' ));
}
/* Actions and Filters */
add_action( 'wp_enqueue_scripts', 'starkers_script_enqueuer' );
add_filter( 'body_class', array( 'Starkers_Utilities', 'add_slug_to_body_class' ) );
/* Custom Post Types */
require_once( 'parts/cpt/cpt.php' );
/* Enqueue Scripts */
function starkers_script_enqueuer() {
wp_register_script( 'site', WT_DIR.'/js/site.js', array( 'jquery' ) );
wp_enqueue_script( 'site' );
wp_register_style( 'screen', get_stylesheet_directory_uri().'/style.css', '', '', 'screen' );
wp_enqueue_style( 'screen' );
}
function wt_scripts_and_styles() {
wp_enqueue_style( 'bootstrap', WT_DIR . '/css/bootstrap.min.css' );
wp_enqueue_style( 'whitetower', WT_DIR . '/css/wts.css' );
wp_enqueue_style( 'googlefonts', 'http://fonts.googleapis.com/css?family=Roboto:500,400|Raleway:600,900|Montserrat:400,700' );
wp_enqueue_script( 'bootstrap', WT_DIR . '/js/bootstrap.min.js', array(), '3.2.0', true );
wp_enqueue_script( 'wtnav', WT_DIR . '/js/snav.js', array(), '1.0.0', true );
wp_enqueue_script( 'flexslider', WT_DIR . '/js/flexslider.js', array(), '2.2.2', true );
wp_enqueue_script( 'modernizr', WT_DIR . '/js/modernizr.js', array(), '2.7.1', true );
wp_enqueue_script( 'googlefonts', WT_DIR . '/js/wt-webfonts.js', array(), '1.0.0', true );
}
add_action( 'wp_enqueue_scripts', 'wt_scripts_and_styles' );
/* Comments */
function starkers_comment( $comment, $args, $depth ) {
$GLOBALS['comment'] = $comment;
?>
<?php if ( $comment->comment_approved == '1' ): ?>
<li>
<article id="comment-<?php comment_ID() ?>">
<?php echo get_avatar( $comment ); ?>
<h4><?php comment_author_link() ?></h4>
<time><a href="#comment-<?php comment_ID() ?>" pubdate><?php comment_date() ?> at <?php comment_time() ?></a></time>
<?php comment_text() ?>
</article>
<?php
endif;
}
Nevermind... found it all out when I tested regular pages other than the front page. It seems to be having a problem with the way WordPress checks static page for a front page by using a "home.php".

Categories