Show header only on shoppage Wordpress - php

Took me some time to find out a working code to show the header above the sidebar.
But I now have the problem that I just want to show the header on the shop-page and not on the product pages.
So I'm trying to figure out what do I need to change?
add_action('woocommerce_before_main_content','before_main_content');
function before_main_content() { ?
<img src="image url">
<?php
}
?
Thank you guys for all the answers :)
This one worked for me :
function before_main_content() {
if (is_shop()) {
?>
<img src="image url">
<?php
}
}
?>

Replace *** with your shop page name:
<?php
if ( is_page('***')) {
**Insert your header code here**;
}
?>
This works for me :)

Use the page conditional, is_page(), to determine if you should show the image. WooCommerce has a function to get the shop page ID. Test for it and return if it's not a match.
Example:
function wpse_before_main_content() { // before_main_content could be problematic, choose a prefix.
// Check if we're on the shop page.
if ( ! is_page( woocommerce_get_page_id( 'shop' ) ) ) {
return false;
} ?>
<img src=". . .
<?php
}
add_action( 'woocommerce_before_main_content', 'wpse_before_main_content' );

Related

How to run php on front page or home page only?

I have the code below which I'm using to show categories only.
The problem I'm facing is it shows categories list on all pages.
I want to use this piece of code on front page or home page only.
How can I show categories list on front page/home page only?
I only have knowledge of html and css, so any help would be appreciated.
Thanks and Regards.
add_action( 'genesis_entry_content', 'list_categories' );
function list_categories() {
echo '<div class="archive-description">';
wp_list_categories(
array(
'title_li' => '')
);
echo '</div>';
}
You have to test if page is home or front page:
add_action( 'genesis_entry_content', 'list_categories' );
function list_categories() {
if ( is_front_page() || is_home() ) {
echo '<div class="archive-description">';
wp_list_categories(['title_li' => '']);
echo '</div>';
}
}

Add custom canonical for front page - Yoast

I want to add a custom url for the front page using YOAST. I'm trying this approach but it doesn't do anything, can you help me?
function design_canonical($url) {
global $post;
$url == ( 'www.cator.com/cator/');
}
add_filter( 'wpseo_canonical', 'design_canonical' );
any idea where is the problem?
thanks
You first need to check if your page is home or not which you can check through is_home()
function design_canonical($url) {
if (is_home()) {
return 'your-custom-url.com'; //Enter here your custom url
} else {
return $url;
}
}
add_filter( 'wpseo_canonical', 'design_canonical' );

Change the Woocommerce breadcrumb link

I'm using the following snippet to change the URL but I want to make it dynamic rather than hard coding a link in.
add_filter( 'woocommerce_breadcrumb_home_url', 'woo_custom_breadrumb_home_url' );
function woo_custom_breadrumb_home_url() {
return 'http://woothemes.com';
}
I tried the following which not only breaks the link but it appears in the top of the screen.
$breadcrumb = bloginfo('url');
add_filter( 'woocommerce_breadcrumb_home_url', 'woo_custom_breadrumb_home_url' );
function woo_custom_breadrumb_home_url() {
return '$breadcrumb';
}
How can I make the Home link dynamic?
You can try
return home_url();
This should give you the same address as bloginfo('url')

Header on pages. (wordpress)

(my site is http://www.musaeus.dk/)
I have a Wordpress.org site and I want to make a button that takes the user back to the front page but I really can't figure out how. The button should be up in the top left corner and it shouldn't be on the front page.
If have tried putting this in the "header.php" file
<?php if(is_page(<page id>)) { ?>
--------- Header 1 -----
<?php } else { ?>
--------- Header 2 --------
<?php } ?>
but with no luck. If you have any ideas or know how I should do this I would really appreciate your help!
in your header.php file do something like this:
<?php
// check to see we are not on front page or on home page
if ( ! ( is_front_page() ) || ! ( is_home() ) ) : ?>
<div class="back-to-home">
Back to Home
</div>
<?php endif; ?>
and then style back-to-home class to match your needs.
Try this. It should works.
<?php
/**
* If page is front page,
* don't display button "Back to Home"
*/
if ( is_front_page() ) :
$btn = false;
else :
$btn = true;
endif;
if ( $btn === true ) : ?>
<div class="back-to-home">
Back to Home
</div>
<?php endif; ?>
Just add this CSS:
.home .back-to-home {
display: none;
}
The .home class is (only) in the body tag of your homepage, so that rule with this combined selector will hide the .back-to-home element only on the homepage

How to display advanced custom fields with an if statement

I'd like to display advanced custom fields on my archive page only but the following code keeps giving me an error:
<?php
do_action( 'editorial_post_categories' );
if ( is_single() ) {
// do something or nothing
} else {
<?php the_field('acf_123'); ?>
}
?>
The advanced custom field I like to display is "acf_123"
Looks like you're nesting <?php tags. You've opened one at the top, so you don't need to open them again. Try:
<?php
do_action( 'editorial_post_categories' );
if ( is_single() ) {
// do something or nothing
} else {
the_field('acf_123');
}
?>
That is, remove the <?php and ?> around the_field() call.

Categories