How to duplicate header in checkout pages? - php

I've three headers in my theme, and i theming the shop with my theme using templates overriding default templates.
i've this in my header.php
Header 1: For my Home site (Not shop)
Header 2: For all pages of my site (Not shop)
Header 3: For shop (Product Categories, single product, archive product)
But, when i buy any product, i go to any checkout page (Register, Cart, My Account, etc), Header 2 appears.
Simplified header.php code:
if(!is_woocommerce()) {
<!--Header Wordpress-->
if(is_home);
Home site header (Header 1)
} else {
All site header (except Home) (Header 2)
<!--End Header Wordpress-->
} else { // Woocommerce conditional
Shop header (Header 3)
}
How to solve it?

The cart and checkout are standard pages with short codes and aren't included in the other Woo templates, i.e. is_woocommerce() won't return true for them. So perhaps use
if ( is_page( 'checkout-page-slug' ) || is_page( 'other_shop_page' ) ) {
instead to target them.
Edit:
Replace checkout-page-slug or other_shop_page with the slug of a page you want to target to have the shop header:
if ( ! is_woocommerce() ) {
if ( is_home() ) {
// Home site header (Header 1)
} elseif( is_page( 'checkout-page-slug' ) || is_page( 'other_shop_page' ) ) {
// Shop header
} else {
// All site header (except Home) (Header 2)
}
} else { // Woocommerce conditional
// Shop header
}

Related

Weglot get_current_language redirection WordPress

I made a redirection to a custom page when the cart is empty. Website is now multilingual, using weglot.
When the language is french, it should redirect the user to the french version of the custom page, same scenario when language is italian.
The redirection per se works, but I always end up on the german (initial language) version.
Example: When the site language is french and you click on cart (which is empty), the browser makes the following redirections:
Start: https://example.com/fr --> Clicking on cart --> https://example.com/fr/cart --> https://example.com/fr/cart-empty (where it should stay) --> https://example.com/fr/?wg-choose-original=false --> https://example.com/cart-empty
// Redirect if cart is empty
add_action("template_redirect", 'redirection_function');
function redirection_function(){
global $woocommerce;
if(is_cart()){
if( WC()->cart->cart_contents_count == 0 && weglot_get_current_language()=='fr'){
wp_safe_redirect('https://example.com/fr/cart-empty' );
exit;
}
if(WC()->cart->cart_contents_count == 0 && weglot_get_current_language()=='it'){
wp_safe_redirect('https://example.com/it/cart-empty' );
exit;
}
if(WC()->cart->cart_contents_count == 0){
wp_safe_redirect('https://example.com/cart-empty' );
exit;
}
}
}
What am I missing?

Display header based on user role and page (Wordpress)

I have created multiple different headers as templates with Elementor.
I would like to display all of the different headers based on user role (Logged in/Logged out) and page.
I'm looking for a code snippet that I could easily customize to assign all of the different headers for different scenarios.
Could someone please create an example code snippet that would:
Display header A for Logged Out users on the entire
website, EXCEPT pages X and Y.
Display header B for Logged In users on the entire
website, EXCEPT pages X and Y.
Display header C for Logged Out users only on pages X and
Y.
Display header D for Logged In users only on pages X and
Y.
This way, people can easily copy the code and customize it to fit their needs.
EDIT
There's 2 places where I can create templates.
1st one is added by Elementor and is found in Admin > Templates > Saved Templates. Here I can create either section or page templates (Screenshot).
2nd one is added by my theme, OceanWP, and is found in Admin > Theme Panel > My Library. Here I can create only 1 type of template. The templates created here can later be assigned as custom headers or footers to individual pages or the entire website.
Are the templates created in these 2 places considered to be template parts? Is there a difference where I choose to create the header templates?
Here's a list of the header templates I have created:
Template title
Post ID
A
Main Header (Logged Out)
5448
B
Main Header (Logged In)
6714
C
Checkout Header (Logged Out)
6724
D
Checkout Header (Logged In)
3960
Here's the page I want to have a different header than the entire website:
Page title
Post ID
Slug
X
Checkout
18
checkout
Something like this should work:
<?php
if (! is_user_logged_in() && ! is_page(array( 'page-x-slug', 'page-y-slug' ))){
// display header A
}
if (is_user_logged_in() && ! is_page(array( 'page-x-slug', 'page-y-slug' ))){
// display header B
}
if (! is_user_logged_in() && is_page(array( 'page-x-slug', 'page-y-slug' ))){
// display header C
}
if (is_user_logged_in() && is_page(array( 'page-x-slug', 'page-y-slug' ))){
// display header D
}
?>
The following offer the same result as the previous answer but is minified and has less repetitiveness.
<?php
if ( is_page( [ 'page-x', 'page-y' ] ) )
if ( is_user_logged_in() )
get_header( 'B' ); //... header-B.php
else
get_header( 'A' ); //... header-A.php
else
if ( is_user_logged_in() )
get_header( 'D' ); //... header-D.php
else
get_header( 'C' ); //... header-C.php
?>
Following your comments
I'm guessing that what you refer as...
section templates
...are in fact templates part. Instead of using get_header( string $name ); you would then use get_template_part( string $slug, string $name = null );.
$slug and $name can be anything that you chose.
Source # https://developer.wordpress.org/reference/functions/get_template_part/
For example, section-A.php would be get_template_part( 'section', 'A' );.
<?php
//...
if ( is_user_logged_in() )
get_template_part( 'section', 'B' ); //... section-B.php
else
get_template_part( 'section', 'A' ); //... section-A.php
?>
In regards to specifying pages and templates. is_page() can take IDs, slugs or titles.
is_page( int|string|int[]|string[] $page = '' )
Parameter
Description
$page
(int|string|int[]|string[]) (Optional) Page ID, title, slug, or array of such to check against. Default value: ''
Source # https://developer.wordpress.org/reference/functions/is_page/
But you could also use other is_ function like is_search() or is_archives(), is_404()... etc.
Here is a complete list # https://codex.wordpress.org/Conditional_Tags
If you want to add multiple conditional statement you can just add elseif statements in-between.
<?php
if ( is_page( [ 'page-x', 'page-y' ] ) )
//...
elseif ( is_search() || is_archive() )
//...
else
//...
?>
If you want to get a better understanding of PHP operators, which are how conditional statements are built, take a look # https://www.w3schools.com/php/php_operators.asp

Open different contact form based on which Wordpress page I am

So basically I have several pages with Woocommerce products listed in them (same products in Page 1 and Page 2) . What I have to do is basically open up different form based on which page the product was clicked on.
Example:
I open 1st page width products and click on product -> Form nr 1 Opens
I open 2nd page with products and click on product -> Form nr 2 Opens
Any ideas how to approach this problem? Don't know which direction to even look.
You can add a content filter to detect the page and amend the content of it.
add_filter( 'the_content', 'my_content_filter' );
function gwp_ec_content_filter ($content)
{
global $post;
if ($post->ID == $page1_page_id)
{
// Change $content
} else if ($post->ID == $page2_page_id)
{
// Change $content
}
}
Or if you doing this inside a theme page template, then
global $post;
if ($post->ID == $page1_page_id)
{
// open form 1
} else if ($post->ID == $page2_page_id)
{
// open form 2
}

Show content if child of a specific page

I am building a website in WordPress and have a set of photos that display along the top of the page (in the background) and have them to where the set of photos can change depending on where the visitor is on the website. For example, if they are looking at the FAQ page, then photos pertaining to asking questions are shown. If they are looking at the news page, then photos pertaining to news are shown. The code I have works great, however, I'd like to now show photos based on the parent of the page they are on; for instance, if they are looking at a child page under a parent.
Here's my code as it stands now:
<?php if ( is_front_page() ) { include'media/home.php'; }
elseif ( is_404() ) { include'media/404s.php'; }
elseif ( is_page('news') ) { include'media/home.php'; }
elseif ( is_page('faqs') ) { include'media/faqs.php'; }
elseif ( is_category('africa-2') ) { include'media/faqs.php'; }
else { include'media/general.php'; }
?>
How can I tell WordPress, I want it to show a certain php file if it is a parent page and all of it's children?
If you're in the loop you can just use
if( is_page('faqs') && $post->post_parent ){
//include template here
}
If you're outside the loop you'll need to include global $post before the conditional.
global $post
You can reference this link http://codex.wordpress.org/Conditional_Tags#Testing_for_sub-Pages

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