Page not showing after add a function on child theme - php

I am using Genesis framework and modern-studio-pro as a child theme. I have just copy this code
function genesis_standard_loop() {
//* Use old loop hook structure if not supporting HTML5
if ( ! genesis_html5() ) {
genesis_legacy_loop();
return;
}
if ( have_posts() ) :
do_action( 'genesis_before_while' );
while ( have_posts() ) : the_post();
do_action( 'genesis_before_entry' );
printf( '<article %s>', genesis_attr( 'entry' ) );
do_action( 'genesis_entry_header' );
do_action( 'genesis_before_entry_content' );
printf( '<div %s>', genesis_attr( 'entry-content' ) );
do_action( 'genesis_entry_content' );
echo '</div>';
do_action( 'genesis_after_entry_content' );
do_action( 'genesis_entry_footer' );
echo '</article>';
do_action( 'genesis_after_entry' );
endwhile; //* end of one post
do_action( 'genesis_after_endwhile' );
else : //* if no posts exist
do_action( 'genesis_loop_else' );
endif; //* end loop
}
from Genesis to child theme functions.php. After that my site is showing
Fatal error: Cannot redeclare genesis_standard_loop() (previously declared in /home/u282646374/public_html/wp-content/themes/modern-studio-pro/functions.php:198) in /home/u282646374/public_html/wp-content/themes/genesis/lib/structure/loops.php on line 115
I have delete that code from child theme but site not showing. Please tell me how can i regain my site? This is the link of my page http://andrewspalding.co.uk/

Your problem is that you are redeclaring the function genesis_standard_loop() inside functions.php of your child theme while on the main genesis theme this function is inside lib/structure/loops.php child themes will overwrite the parent theme functions if they are inside the same file that they are in the parent theme, if you want to modify a function from the parent theme you might contemplate the idea of looking for filters or actions.
If you MUST redeclare the function try recreating the same file structure, but remember to add all of the other functions inside that particular file.

Related

Add shortcode child theme WordPress

I work in multisite
I have a child theme of my parent theme to customize a page template.
In this customize page template I would like to display a shortcode, which allows to retrieve the url address of the site in question.
My function in my child theme file, function.php:
add_action( 'init', function() {
add_shortcode( 'site_url', function( $atts = null, $content = null ) {
return site_url();
} );
} );
I would like to declare this shortcode in my template customizer, like this:
/*
Template name: Test
*/
get_header(); ?>
<div>
<h1>Notre site web : [site_url]</h1>
<?php
if ( have_posts() ) :
while ( have_posts() ) :
the_post();
get_template_part( 'content', 'page' );
endwhile;
endif;
?>
</div>
<?php get_footer(); ?>
````but the shotcode doesn't generate anything at all in my customize template.
I must have missed something... :-(
Can you help me ?
Thanks
This is because shortcodes that are hardcoded into templates don't get rendered by default. What you want to do is put the shortcode in the function do_shortcode() and then it should output correctly.
echo do_shortcode('[site_url]');

Where to insert php code into the functions.php file of my child theme

First of all, I'm not a developer. I've managed to insert some functions for my WooCommerce webshop by inserting code snippets into the functions.php file of my parent theme. I've made a child theme in case I have to update the theme. I'm afraid these functions get lost otherwise.
I've used the Child Theme Configurator plugin to create the child theme. Now I'm trying to insert the same code snippets into the functions.php file of the child theme. I however, break my site every time. Before adding the code I've removed the functions from the parent theme, otherwise an overlap might excist.
I think I'm adding these code snippets at the wrong location within the file. Below you see the code from the child theme:
<?php
// Exit if accessed directly
if ( !defined( 'ABSPATH' ) ) exit;
// BEGIN ENQUEUE PARENT ACTION
// AUTO GENERATED - Do not modify or remove comment markers above or below:
if ( !function_exists( 'chld_thm_cfg_locale_css' ) ):
function chld_thm_cfg_locale_css( $uri ){
if ( empty( $uri ) && is_rtl() && file_exists( get_template_directory() . '/rtl.css' ) )
$uri = get_template_directory_uri() . '/rtl.css';
return $uri;
}
endif;
add_filter( 'locale_stylesheet_uri', 'chld_thm_cfg_locale_css' );
if ( !function_exists( 'child_theme_configurator_css' ) ):
function child_theme_configurator_css() {
wp_enqueue_style( 'chld_thm_cfg_child', trailingslashit( get_stylesheet_directory_uri() ) . 'style.css', array( 'hello-elementor','hello-elementor','hello-elementor-theme-style' ) );
}
endif;
add_action( 'wp_enqueue_scripts', 'child_theme_configurator_css', 10 );
// END ENQUEUE PARENT ACTION
This is the code I've successfully added to the parent theme. Now I want to add it to the child theme:
add_filter( 'woocommerce_product_tabs', 'woo_remove_product_tabs', 98 );
function woo_remove_product_tabs( $tabs ) {
unset( $tabs['description'] ); // Remove the description tab
unset( $tabs['additional_information'] ); // Remove the additional information tab
return $tabs;
}
/* melding uitschakelen: toegevoegd aan winkelwagen */
add_filter( 'wc_add_to_cart_message_html', 'ql_remove_add_to_cart_message' );
function ql_remove_add_to_cart_message( $message ){
return '';
}
Where and how do I add it?
Thanks for the help in advance! :)

Wordpress child theme can't prevent parent theme error

Trying to update a Wordpress instance to PHP 7.2 and I've got a parent theme that uses the deprecated $this in its functions.php.
function paperback_filter_page_templates( $page_templates, $this, $post ) {
if( ! class_exists( 'Easy_Digital_Downloads' ) ) :
unset( $page_templates['inc/edd/home-downloads.php'] );
endif;
return $page_templates;
}
add_filter( 'theme_page_templates', 'paperback_filter_page_templates', 10, 3 );
I've been unsuccessful...
1: Overriding the function in the child theme's functions.php:
(removing $this from the param and with higher priority add_filter)
function paperback_filter_page_templates( $page_templates, $post ) {
if( ! class_exists( 'Easy_Digital_Downloads' ) ) :
unset( $page_templates['inc/edd/home-downloads.php'] );
endif;
return $page_templates;
}
add_filter( 'theme_page_templates', 'paperback_filter_page_templates', 15, 2 );
2: Using a function that removes the filter in the child theme's functions.php:
function rm_paperback_filter_page_templates() {
remove_filter( 'theme_page_templates', 'paperback_filter_page_templates', 10);
}
add_action( 'wp_loaded', 'rm_paperback_filter_page_templates', 15 );
(have tried hooking this action into init, setup_theme, after_setup_theme...)
I can remove the offending function from the parent theme to avoid a catastrophic php 7.2 upgrade for now (parent theme authors/company have been acquired so an update isn't coming) and I know to look for parent themes with pluggable functions from now on... but it'd be nice to know if a real fix is possible!

Custom templates in Woocommerce 3

I'm trying to make a separate template for only one product with ID 5555. To delete a photo from its page and change the blocks structure. Overriding this file affects all product pages.
wp-content\plugins\woocommerce\templates\content-single-product.php
The logical solution is to add a condition: if the product single prpduct page have ID 5555, then another template is used for its page.
I tried this option, but it does not work.
/**
* Custom single product template.
*/
add_filter( 'single_template', 'get_custom_post_type_template' );
function get_custom_post_type_template($single_template) {
global $post;
if ($post->post_type == 'product') {
$single_template = dirname( __FILE__ ) . '/single-template.php';
}
return $single_template;
}
add_filter( 'template_include', 'portfolio_page_template', 99 );
function portfolio_page_template( $template ) {
if ( is_page( 'slug' ) ) {
$new_template = locate_template( array( 'single-template.php' ) );
if ( '' != $new_template ) {
return $new_template ;
}
}
return $template;
}
Or option 2: remove this hooks:
remove_action( 'woocommerce_before_single_product_summary', 'woocommerce_show_product_images', 20 );
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20 );
from only one specific product page:
Custom templates specifically since Woocommerce 3.3
Since Woocommerce 3.3 Wordpress hook template_include doesn't seem to work always, as you could see in a lot of recent related threads in StackOverFlow and somewhere else too.
The specific filter hooks related to templates for Woocommerce:
wc_get_template
wc_get_template_part
woocommerce_locate_template
woocommerce_template_path (The theme's override templates folder)
Case 1. Using wc_get_template_part (your case):
If you look at single-product.php template, the content-single-product.php template is loaded with the following at this line:
<?php wc_get_template_part( 'content', 'single-product' ); ?>
So we will use the hook wc_get_template_part.
Let say that your custom template replacement file is named content-single-product-custom.php and located in the woocommerce folder of your active child theme (or active theme). To use this custom template for a specific product ID (5555) you will need the following:
add_filter( 'wc_get_template_part', 'custom_wc_template_part', 10, 3 );
function custom_wc_template_part( $template, $slug, $name ) {
// The specific product ID
$product_id = 5555;
// The custom template file name
$custom_template_name = 'content-single-product-custom.php';
// For a specific product ID and content-single-product.php template
if( get_the_ID() == $product_id && $slug == 'content' && $name == 'single-product' ){
$template = trailingslashit( get_stylesheet_directory() ) . 'woocommerce/' . $custom_template_name;
}
return $template;
}
This code goes in function.php file of your active child theme (or active theme). Tested and works.
Case 2. Using wc_get_template:
If templates is loaded via wc_get_template() function, like for example single-product/meta.php and your custom replacement template is named single-product/custom-meta.php (and located in the woocommerce folder of your active child theme). To use this custom template for a specific product ID (5555) you will need the following:
add_filter( 'wc_get_template', 'custom_wc_template', 10, 5 );
function custom_wc_template( $located, $template_name, $args, $template_path, $default_path ) {
// The specific product ID
$product_id = 5555;
// The custom template file name and path
$custom_template_name = 'single-product/custom-meta.php';
if( is_product() && get_the_ID() == 37 && $template_name == 'single-product/meta.php'){
$located = trailingslashit( get_stylesheet_directory() ) . 'woocommerce/' . $custom_template_name;
}
return $located;
}
This code goes in function.php file of your active child theme (or active theme). Tested and works.
About overriding woocommerce templates:
Official documentation: Template structure & Overriding templates via a theme
WooCommerce action hooks and overriding templates
Overriding specific third party Woocommerce plugin templates
Add this to your theme's 'functions.php' where 5 is product ID and woocommerce/single-product-watermelon.php is custom product template.
add_filter( 'template_include', 'watermelon_template_include' );
function watermelon_template_include( $template ) {
if ( is_singular('product') && get_the_ID() == 5 ) { //if category, change has_term( 'watermelon', 'product_cat') instead of get_the_ID()
$template = get_stylesheet_directory() . '/woocommerce/single-product-watermelon.php';
}
return $template;
}

Genesis Menu duplicating instead of moving

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.

Categories