custom function displaying on all pages - php

WordPress|storefront|woo-commerce.
i am trying to display header on desktop view and footer on mobile responsive for specific page id 1.but even using specific page id both classes shows on page id 1 header,footer.Tried both id and class with cascading rule of hierarchy.Here id abc displays on page id 1 desktop view and abcd on page id 1 mobile.
My code for child function.php is
add_action( 'storefront_header', 'header_content1', 30 );
function header_content1() { ?>
<div id/class="abc" > my text with css</div><?php
}
add_action( 'storefront_after_footer', 'header_content2', 10 );
function header_content2() { ?>
<div id/class="abcd" > my text with css</div><?php
}
CSS child theme.
.page-id-1 .abc {
text-align: right;
padding-top: 0.02em;
color: #0000ff;
}
.abc{
display:none!important;
}
.abcd{
display:none!important;
}
.page-id-1 .abcd{
display:none!important;
visibility:hidden;
}
#media only screen and (max-width: 599px){
.abc{
display:none!important;
}
.abcd{
display:none!important;
}
.page-id-1 .abc{
display:none !important;
visibility:hidden;
}
.page-id-1 .abcd {
display:block !important;
text-align: center;
padding-top: 0.02em;
color: #0000ff;
}
}

A better approach would be to only load your function on the page that is needed instead of hidding your HTML in all pages but one...
You can check the page ID before running the function with something like this:
if (is_page('1') {
add_action( 'storefront_header', 'header_content1', 30 );
function header_content1() { ?>
<div id/class="abc" > my text with css</div><?php
}
}

Related

Woocommerce How to get Term id from multiple slugs in css

Good Day,
I want to have a custom CSS for certain woo-commerce product category pages, to exclude the sidebar and make the content full width of the page. now I can get the CSS to work on these pages but I have over 450 categories. is there a way to make the term-id dynamic for excluded categories
the script I have so far is as follows
if ( !is_product_category( array( 'cat1','cat3','cat4','cat6') ) ) {
add_action( 'wp_head', function () { ?>
<style>
/*Hide SideBar*/
#primary {
width: 100%!important;
border-right: 0px!important;
}
.term-1 .ast-right-sidebar #secondary,.term-3 .ast-right-sidebar #secondary,.term-4 .ast-right-sidebar #secondary,.term-6 .ast-right-sidebar #secondary{
border-left: 0px!important
}
.term-1.sidebar-main,.term-3 .sidebar-main,.term-4 .sidebar-main,.term-6 .sidebar-main {
display:none!important
}
.term-1 #secondary,.term-3 #secondary,.term-4 #secondary,.term-6 #secondary {
display: none!important;
border-right: 0px solid #eee!important;
}
.term-1 .widget-area .secondary,.term-3 .widget-area .secondary,.term-4 .widget-area .secondary,.term-6 .widget-area .secondary
{
display: none!important;
}
.term-1 .ast-right-sidebar #primary,.term-3 .ast-right-sidebar #primary,.term-4 .ast-right-sidebar #primary,.term-6 .ast-right-sidebar #primary {
border-right: 0px solid #eee!important;
}
</style>
<?php } );
}
I would like to dynamically add the term-id to the CSS so that the CSS is more compact and does not get too long. any suggestions, please
#CBroe thanks for pointing me in the right direction.
The first thing I did is make a code snippet that you can copy and paste into the functions.php page of your child's theme or use plugins like code snippet. so when your theme updates you do not lose any of your code.
the first part that I did was as follows, I added this CSS to the body of the page.
add_filter('body_class', 'add_custom_body_class');
function add_custom_body_class($classes){
if(is_product_category(array( 'cat1','cat3','cat4','cat6'))){
$classes[] = 'your_new_class_here';
}
return $classes;
}
then I create a new snippet where I will add the class into the header now keep in mind that you do not want to have this code on every page so we will also add the is_product_category to the snippet to show code only when we are on that page
if ( !is_product_category( array( 'cat1','cat3','cat4','cat6') ) ) {
add_action( 'wp_head', function () { ?>
<style>
/*Hide SideBar*/
body.your_new_class_here .ast-right-sidebar #secondary { border-left: 0px!important; }
body.your_new_class_here .sidebar-main { display:none!important; }
#primary {
width: 100%!important;
border-right: 0px!important;
}
#secondary {
display: none!important;
border-right: 0px solid #eee!important;
}
</style>
<?php } );
}
What this does is add the CSS classes to the pages that you specified, hope this helped someone else

How to add current image number to each product gallery image in WooCommerce?

I am looking for a solution to show product images counter in my WooCommerce store. For example if my product has 5 photos I want to display under each photo: 1/5, 2/5, 3/5 etc.
First what I was thinking about was CSS and jQuery. For example I could add CSS with nth-child(1), nth-child(2) up to 10 or 20 like:
figure.woocommerce-product-gallery__wrapper div:nth-child(1):after {
display: block !important;
z-index: 100;
color: #000;
font-size: 14px;
position: absolute;
top: 20px;
left: 20px;
width: 20px;
height: 30px;
overflow: visible;
content: "1/";
}
And then using JS I could catch number of child DIV's inside the FIGURE container and display it after my code using CSS.
But maybe there is other easiest way to use some loop in functions.php or gallery PHP file? Any ideas how to achieve that?
Try with this:
// add the current number for each gallery image product
add_action( 'wp_footer', 'add_number_for_each_gallery_image_product' );
function add_number_for_each_gallery_image_product(){
// only on the product page
if ( ! is_product() ) {
return;
}
?>
<script type="text/javascript">
jQuery(function($){
// count the gallery images
var count = $('figure.woocommerce-product-gallery__wrapper .woocommerce-product-gallery__image').length;
// for each gallery image adds the number
$('figure.woocommerce-product-gallery__wrapper .woocommerce-product-gallery__image').each(function(index){
index++;
$(this).prepend('<span class="image_no">' + index + '/' + count + '</span>');
});
});
</script>
<?php
}
The code has been tested and works. Add it to your active theme's functions.php.
Finally add this CSS rule:
.flex-active-slide .image_no {
position: absolute;
background: #eeeeee;
border-radius: 4px;
padding: 5px 8px;
color: #333;
top: 12px;
left: 12px;
z-index:1;
}
Add it to your active theme's stylesheet.
RESULT

WooCommerce: Remove thumbnails from gallery but keep slider navigation

I want to remove the thumbnails from the gallery (flexslider) of the product single page.
But I want to keep the arrow for the previous/next images (in case there is more than 1 image).
I found the following code:
add_action( 'woocommerce_product_thumbnails', 'enable_gallery_for_multiple_thumbnails_only', 5 );
function enable_gallery_for_multiple_thumbnails_only() {
global $product;
if( ! is_a($product, 'WC_Product') ) {
$product = wc_get_product( get_the_id() );
}
if( empty( $product->get_gallery_image_ids() ) ) {
remove_action( 'woocommerce_product_thumbnails', 'woocommerce_show_product_thumbnails', 20 );
}
}
Source: https://stackoverflow.com/a/56238267/1788961
The problem is, that the function removes the thumbnail and the arrows.
Is there any way to keep the arrows?
And I know, that I could use display:none or maybe change the template file.
But I'm searching a solution with an own function.
if you want keep only arrow then you just put this code in functions.php:
// for arrow on single product page slide
add_filter( 'woocommerce_single_product_carousel_options', 'sf_update_woo_flexslider_options' );
/**
* Filer WooCommerce Flexslider options - Add Navigation Arrows
*/
function sf_update_woo_flexslider_options( $options ) {
$options['directionNav'] = true;
return $options;
}
And this code put it in your theme style.css file:
/*add for arrow on main image slide*/
ul.flex-direction-nav {
position: absolute;
top: 30%;
z-index: 99999;
width: 100%;
left: 0;
margin: 0;
padding: 0px;
list-style: none;
}
li.flex-nav-prev {float: left;}
li.flex-nav-next {float: right;}
a.flex-next {visibility:hidden;}
a.flex-prev {visibility:hidden;}
a.flex-next::after {
visibility:visible;content: '\f054';
font-family: 'Font Awesome 5 Free';
margin-right: 10px;
font-size: 20px;
font-weight: bold;
}
a.flex-prev::before {
visibility:visible;
content: '\f053';
font-family: 'Font Awesome 5 Free';
margin-left: 10px;
font-size: 20px;
font-weight: bold;
}
ul.flex-direction-nav li a {
color: black;
}
ul.flex-direction-nav li a:hover {
text-decoration: none;
}
.flex-control-nav .flex-control-thumbs{
display: none;
}
add_filter( 'woocommerce_single_product_carousel_options', 'sf_update_woo_flexslider_options' );
/**
* Filer WooCommerce Flexslider options - Add Navigation Arrows
*/
function sf_update_woo_flexslider_options( $options ) {
$options['directionNav'] = true;
$options['controlNav'] = false;
return $options;
}
The given approved answer is close but still resorts to CSS to hide the thumbnails.
You can disable controlNav directly on the same filter you use to show the arrows.

WP + storefront Can´t make top header nav and logo responsive

Hi there and thanks in advance,
NOTE: The site is NSFW, just some ebooks for adults (romance and that stuff, but the ebooks covers may be hot).
My environment:
I have WP with a woocommerce, storefront+bookshop child theme
I have the Snippets plugins where I add all my PHP code
And I add CSS to Custom CSS within customizer
Now the problem:
I can´t figure out how to make site header, top nav bar and logo + buttons responsive.
The fact is that I could get some Top header styling using some PHP and CSS, BUT when I go to mobile view, the Top nav var, header, logo and menu goes crazy!
I need to fix that, I don´t know what to try (I googled a lot, tried changing values, %, float, inline, block, important, whatever) but I can´t get the right tip.
I don{t know if the child theme or storefront itself is overriding my attempts, maybe I have to unhook some function.
Maybe is just as simple as putting all 3 PHP snippets into one div and many spans tag, but I can´t figure out how I am burnt today.
I would thank any advice.
Here goes my CSS
/* Logo size for mobile site */
#media screen and (max-width: 768px) {
.site-header .site-branding img {
max-height: none !important;
max-width: none !important;
width: 322px !important;
}
}
/*Remove title from Shop page*/
.woocommerce-products-header
{display: none;}
/*Hide Pages Tite with custom Class added to PHP snippets*/
.hidetitle .entry-header {
display:none;
}
/*Make primary Pages full width*/
body.woocommerce #primary {
width: 100%;
}
/*Align Search Bar and make text pink*/
#woocommerce-product-search-field-0 {
display:inline-block;
width:100%;
color: #fe00a1;
border: solid 1px #fe00a1;
}
/*Edited Cart*/
#site-header-cart{
padding-bottom:20px;
width:15%;
}
/*Changed cart icon bag f\290 to to cart icon f\217 */
.site-header-cart .cart-contents:after,
.storefront-handheld-footer-bar ul li.cart > a:before {
content: “\f217”;
}
/*Align Help link*/
#help{
display:inline-block;
padding-left: 35px;
padding-right: 35px;
}
/*Align Gift card*/
#gift-cardl{
margin-left:80px;
}
/*Add top padding to rectangle*/
#rectangle {
padding:17px;
}
**PHP snippets**
add_action( ‘storefront_header’, ‘header_custom_gift_button’, 40 );
function header_custom_gift_button() { ?>
<span>
<button id=”gift-cardl”>Gift Card</button>
</span>
<?php
}
add_action( ‘storefront_header’, ‘header_custom_help_link’, 40 );
function header_custom_help_link() { ?>
<span style=”width:100%;”>
Help
</span>
<?php
}
add_action( ‘storefront_header’, ‘header_custom_subscribe_button’, 40 );
function header_custom_subscribe_button() { ?>
<button class=”subscribe_newsletter_btn”>Get Daily Book Bargains</button>
</span>
<?php
}

WooCommerce sidebar wrong display

I have problem with WooCommerce sidebar on archive-page and categories page,
Its display under my products ( right after page-pagi )
I tried to style it with css, like
.woocommerce .products ul, .woocommerce ul.products { width: 70%; float: right )
.sidebar { width: 29%; float: left;}
But it looks like it's in completely another section or something cause this have no effect.
If your theme isn't configured to work with woocommerce, and you've just installed it, it will probably be totally messed up. What you can do is add your own containers. Put these in functions.php
if (!function_exists('custom_open_woocommerce_content_wrappers')) {
function custom_open_woocommerce_content_wrappers(){
echo '<div class="container shop_container"><div class="row">';
}
}
if (!function_exists('custom_close_woocommerce_content_wrappers')) {
function custom_close_woocommerce_content_wrappers(){
echo '</div></div>';
}
}
if (!function_exists('custom_product_wrapper_open')) {
function custom_product_wrapper_open(){
echo '<div class="span8 content_with_right_sidebar">';
}
}
if (!function_exists('custom_product_wrapper_close')) {
function custom_product_wrapper_close(){
echo '</div>';
}
}
if (!function_exists('custom_before_shop_loop_sidebar')) {
function custom_before_shop_loop_sidebar() {
echo '<aside class="span4 sidebar sidebar_right">';
dynamic_sidebar('Sidebar Name Goes Here');
echo '</aside>';
}
}
add_action( 'woocommerce_after_shop_loop', 'custom_before_shop_loop_sidebar', 20);
if (!function_exists('custom_prepare_woocommerce_wrappers')) {
function custom_prepare_woocommerce_wrappers(){
remove_action( 'woocommerce_before_main_content', 'woocommerce_output_content_wrapper', 10 );
remove_action( 'woocommerce_after_main_content', 'woocommerce_output_content_wrapper_end', 10);
remove_action( 'woocommerce_before_shop_loop', 'woocommerce_output_content_wrapper', 10);
remove_action( 'woocommerce_after_shop_loop', 'woocommerce_output_content_wrapper_end', 10);
add_action( 'woocommerce_before_main_content', 'custom_open_woocommerce_content_wrappers', 10 );
add_action( 'woocommerce_after_main_content', 'custom_close_woocommerce_content_wrappers', 10 );
add_action( 'woocommerce_before_shop_loop', 'custom_product_wrapper_open', 10 );
add_action( 'woocommerce_after_shop_loop', 'custom_product_wrapper_close', 10 );
}
}
add_action( 'wp_head', 'custom_prepare_woocommerce_wrappers' );
This will create a wrapper with a right sidebar. And you can style it with
.container{
width: 1170px;
margin: 0 auto;
}
.row{
width: 100%;
}
.row:before,
.row:after{
display: table;
line-height: 0;
content: "";
}
.row:after{
clear: both;
}
.row > [class*="span"]{
display: block;
float: left;
width: 100%;
min-height: 20px;
margin-left: 2.564102564102564%;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
.row > [class*="span"]:first-child{
margin-left: 0;
}
.row .span8 {width: 65.81196581196581%;}
.row .span4 {width: 31.62393162393162%;}
Etc.
You can customize the wrappers to your liking, add options to choose left/right no sidebar by linking it with customizer, etc.
Basically, you can add your own classes in those divs, that will suit your needs. This one has these classes because when I'm working with themes I use grid that has 12 columns, and it has container with 1170px width and row and spans (1-12 corresponding to columns).
Hope this helps :)
set .woocommerce and .sidebar padding and margin = 0

Categories