I have a Wordpress problem that I can't fix....
Woocommerce is using a sidebar with product filters.
I'd simply like to hide the sidebar + filters on Woocommerce parent pages.
I first tried by adding a class which I could hide the sidebar content with css, like this :
add_filter( 'fusion_sidebar_1_class', 'usl_wc_product_cats_css_body_class' );
function usl_wc_product_cats_css_body_class( $classes )
{
if( is_tax( 'product_cat' ) ) {
$cat = get_queried_object();
if( 0 == $cat->parent ) $classes[] = 'usl-parent';
}
return $classes;
}
But this obviously leaves the main content at 80% or so.
So I'm now trying to remove the sidebar instead using a mix of the above and some example code from the Avada help pages. Code is below but totally doesn't work! Can anyone please help?
function remove_woo_commerce_sidebar() {
global $avada_woocommerce;
{
if( is_tax( 'product_cat' ) )
{
$cat = get_queried_object();
if( 0 == $cat->parent )
remove_action( 'woocommerce_sidebar', array( $avada_woocommerce, 'add_sidebar' ), 10 );
}
}
add_action( 'after_setup_theme', 'remove_woo_commerce_sidebar' );
Can't you just do this through CSS? Just change the selector to match the page type you want to affect
.woocommerce-page #content .single_wrap {
float: none;
width: 100%;
}
.woocommerce-page #sidebar {
display: none;
}
Couldn't find a neat way to unload the sidebar so added an extra body class so I could target both the main content & sidebar with this :
add_filter( 'body_class','usl_body_classes' );
function usl_body_classes( $classes )
{
if( is_tax( 'product_cat' ) ) {
$cat = get_queried_object();
if( 0 == $cat->parent ) $classes[] = 'usl_full_woo_css';
}
return $classes;
}
Related
I am trying to disable "add to cart" button on WooCommerce shop and archives pages if product's quantity stock is zero or less.
I don't want to hide it so I came up with the code below.
My issue is that code doesn't add style to element, it replaces the whole button code inside html tree so button is not displayed at all.
Any ideas on how to overcome the issue?
add_action( 'woocommerce_loop_add_to_cart_link', 'remove_price_from_variable_products', 9 );
function remove_price_from_variable_products() {
global $product;
if( $product->get_stock_quantity() <= 0 ) {
?>
<style>
.add_to_cart_button {
cursor: not-allowed !important;
}
</style>
<?php
add_action( 'woocommerce_after_shop_loop_item', 'custom_content_addtocart_button', 100 );
}
}
function custom_content_addtocart_button() {
echo '<br/><div class="button-notice">Contact Us for more information</div>';
}
To add/edit/remove CSS classes from the existing add to cart button on WooCommerce shop and archives pages you can use the woocommerce_loop_add_to_cart_args filter hook
So you get:
function action_woocommerce_loop_add_to_cart_args( $wp_parse_args, $product ) {
// Initialize
$custom_class = '';
// Certain condition, can be anything, in this specific case for the stock quantity. ADAPT TO YOUR NEEDS
if ( $product->get_stock_quantity() <= 0 ) {
$custom_class = 'button-not-allowed';
}
// NOT empty (from here on, no need to make any changes to my answer)
if ( ! empty ( $custom_class ) ) {
// Class
$wp_parse_args['class'] = implode(
' ',
array_filter(
array(
'button' . ' ' . $custom_class,
'product_type_' . $product->get_type(),
$product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '',
$product->supports( 'ajax_add_to_cart' ) && $product->is_purchasable() && $product->is_in_stock() ? 'ajax_add_to_cart' : '',
)
)
);
}
return $wp_parse_args;
}
add_filter( 'woocommerce_loop_add_to_cart_args', 'action_woocommerce_loop_add_to_cart_args', 10, 2 );
Note: the $product->get_stock_quantity() function is best used in combination with $product->managing_stock(), but this depends on your needs
Then apply the following CSS
.button-not-allowed {
cursor: not-allowed !important;
}
I've been trying to work on this little thingy for almost a full day so any help would be highly appreciated
You can see how the breadcrumbs looks like in here:
https://www.bitcoinhoy.co/criptomonedas/ethereum/
I managed to find the relevant php file and here's the code:
enter code here
<!-- ** Breadcrumb ** -->
<?php
if( !empty( $global_breadcrumb ) ) {
if(empty($settings)) { $settings['enable-sub-title'] = true; }
if( isset( $settings['enable-sub-title'] ) && $settings['enable-sub-title'] ) {
$breadcrumbs = array();
$bstyle = digibit_cs_get_option( 'breadcrumb-style', 'default' );
$separator = '<span class="'.digibit_cs_get_option( 'breadcrumb-delimiter', 'fa default' ).'"></span>';
if( is_singular('post') ){
$cat = get_the_category();
$cat = $cat[0];
$breadcrumbs[] = get_category_parents( $cat, true, $separator );
}
$breadcrumbs[] = the_title( '<span class="current">', '</span>', false );
$bcsettings = isset( $settings['breadcrumb_background'] ) ? $settings['breadcrumb_background'] : array();
$style = digibit_breadcrumb_css( $bcsettings );
digibit_breadcrumb_output ( the_title( '<h1>', '</h1>',false ), $breadcrumbs, $bstyle, $style );
}
}
?><!-- ** Breadcrumb End ** -->
Any idea how can I modify the "home" text to something else?
It looks like Digibit is a premium theme. Modifying a theme file may not be a great idea because if they update the theme, it will break the modifications you make.
What you can do is add this in your CSS (perhaps in Customize > Additional CSS):
.breadcrumb a:first-child {
font-size: 0;
}
.breadcrumb a:first-child:after {
content: "Something Else";
font-size: 14px;
}
I have a problem that drives me mad, and can't find a solution to it in the documentation...
I want to hook in to Wordpress' post_gallery function to add a Class to the dl element that wraps the WP generated gallery individual images. By this I want to have the option to have full width images that can be controlled in the Attachments window of WP Media via ACF switch
if ( get_field('fullwidth') == 1 ){
$classname = 'fullwidth';
} else {
$classname = "";
}
Also setting the img "src" to the Large for these fullwidth images would come handy, so the page-load is not too slow.
Do I need to rewrite the whole gallery generating code from here?
https://core.trac.wordpress.org/browser/trunk/src/wp-includes/media.php
Can it be done via this method at all, or should I somehow try to hook in to the_content? But also for that method, I didn't find any info in regard how galleries are generated...
to add a Class to the dl element that wraps the WP generated gallery individual images
Instead of that, why don't you — using the Text/HTML editor — add the gallery like this:
<div class="fullwidth">
[gallery ... /]
</div>
(the three dots indicates the Shortcode parameters such as ids, size, and link)
And write some CSS code similar to:
.fullwidth .gallery-item {
/* Add styles for the DL (or FIGURE for HTML5-supported themes) element that wraps the gallery image and caption. */
}
.fullwidth .gallery-icon {
/* Add styles for the DT (or DIV for HTML5-supported themes) element that wraps the gallery image. */
}
.fullwidth .gallery-caption {
/* Add styles for the DD (or FIGCAPTION for HTML5-supported themes) element that wraps the gallery caption. */
}
[EDIT] Below is a custom Shortcode; used the exact same way you'd use the default [gallery] Shortcode.
Add these to the theme's functions.php file:
/**
* #param array $attr
* #param WP_Post $attachment
*/
function gallery_shortcode2_image_attributes( $attr, $attachment ) {
// Name of the custom field. (ACF)
$field_name = 'fullwidth';
if ( get_field( $field_name, $attachment->ID ) ) {
$attr['data-gallery-layout'] = 'full-width';
}
return $attr;
}
add_shortcode( 'gallery2', 'gallery_shortcode2' );
function gallery_shortcode2( $attr ) {
$atts = shortcode_atts( array(
'itemtag' => 'figure', // I'm assuming we'll always be using HTML5-supported themes..
'fwclass' => 'fullwidth', // The CSS class applied to items with, well, full-width layout.
'size' => 'medium_large', // You may change the default value; to "large", "custom", etc.
), $attr );
// Don't modify anything below unless you're 100% sure of what you're doing. =)
$itemtag = tag_escape( $atts['itemtag'] );
$valid_tags = wp_kses_allowed_html( 'post' );
if ( ! isset( $valid_tags[ $itemtag ] ) ) {
$itemtag = 'dl';
}
add_filter( 'wp_get_attachment_image_attributes', 'gallery_shortcode2_image_attributes', 10, 2 );
$output = gallery_shortcode( array_merge( $attr, $atts ) );
$output2 = '';
remove_filter( 'wp_get_attachment_image_attributes', 'gallery_shortcode2_image_attributes', 10, 2 );
$_tag = '<' . preg_quote( $itemtag ) . ' class=\'gallery-item\'>';
$arr = preg_split( "/($_tag)/", $output, -1, PREG_SPLIT_DELIM_CAPTURE );
for ( $i = 0, $j = 1; $i < count( $arr ); $i++, $j++ ) {
if ( $_tag === $arr[ $i ] && isset( $arr[ $j ] ) ) {
$class = 'gallery-item';
if ( preg_match( '/<img (.*?) data-gallery-layout="full-width"/', $arr[ $j ] ) ) {
$class .= ' ' . $atts['fwclass'];
}
$output2 .= '<' . $itemtag . ' class=\'' . esc_attr( $class ) . '\'>';
} else {
$output2 .= $arr[ $i ];
}
}
unset( $output, $arr );
return $output2;
}
And use [gallery2] instead of [gallery]; same arguments (e.g. ids and size), but with one extra argument for [gallery2], which is fwclass. (Refer to the gallery_shortcode() function for details on that argument.)
Hopefully [gallery2] works for you, like it did for me.
NOTE: Use [gallery2] only if you want to add a custom CSS class to the individual gallery item element (i.e. .gallery-item). Otherwise, use the default Shortcode — [gallery].
I am trying yo Remove custom body class from pages with a specific category in WordPress.
Here is the code below I am trying to make to work. However, it does not.
function remove_body_class($wp_classes) {
if ( is_category ('places') ) :
foreach ( $wp_classes as $key=>$value ) {
if ( $value =='my_class' ) unset( $wp_classes[ $key ] );}
endig;
return $wp_classes;
} add_filter( 'body_class', 'remove_body_class');
It works when I remove class from all pages without using "if ( is_category ('places') ) :"
But I can't make it work only for specific category/posts.
Could you tell me if I do something wrong? I would highly appreciate it.
Thank you.
it will be help for you.
// Removes a class from the body_class array.
add_filter( 'body_class', function( $classes ) {
if ( isset( $classes['your-class-name'] ) ) {
unset( $classes['your-class-name'] );
}
return $classes;
} );
// 34 is your category id
if (is_category('34'))
{
add_filter( 'body_class', function( $classes ) {
if ( isset( $classes['your-class-name'] ) ) {
unset( $classes['your-class-name'] );
}
return $classes;
} );
}
// When the archive page for Category 34 is being displayed.
Define $cat_id to the category ID and change "your-class-name" to the name of the class you want removed:
if ( is_category($cat_id) ) {
add_filter( 'body_class', function( $classes ) {
if ( null !== array_search( 'your-class-name', $classes) ) {
unset( $classes[ array_search( 'your-class-name', $classes) ] );
}
return $classes;
} );
}
I have a page called "Portfolio". I use this page to show the archive for my custom post type called "Works". I do this by displaying the portfolio page on with a custom template called "Work archive".
I would like to highlight the Portfolio page in my menu when I am on a single post of Works.
Can You help me?
This could help you
function change_page_menu_classes($menu){
global $post;
if (get_post_type($post) == 'portfolio')
{
$menu = str_replace( 'current_page_parent', '', $menu ); // remove all current_page_parent classes
$menu = str_replace( 'page-item-366', 'page-item-366 current_page_parent', $menu ); // add the current_page_parent class to the page you want
}
return $menu;
}
add_filter( 'wp_page_menu', 'change_page_menu_classes', 0 );
Source
Hey I dunno if this is still relevant but I came across this and it worked great. I'm using the roots theme with a post type of "projects"
// Remove active class from menu
function remove_active_class($class) {
return ( $class == 'active' ) ? FALSE : TRUE;
}
// Add active class to menu of post type single template
function add_class_to_wp_nav_menu($classes) {
if( is_singular( 'projects' ) ) {
$classes = array_filter( $classes, 'remove_active_class' );
if( in_array( 'menu-projects', $classes) ) {
$classes[] = 'active';
}
} elseif( is_singular( 'resources' ) ) {
$classes = array_filter( $classes, 'remove_active_class' );
if( in_array( 'menu-resources', $classes) ) {
$classes[] = 'active';
}
}
return $classes;
}
add_filter('nav_menu_css_class', 'add_class_to_wp_nav_menu');
add_filter( 'nav_menu_css_class', 'namespace_menu_classes', 10, 2 );
function namespace_menu_classes( $classes , $item ){
if ( get_post_type() == 'attorneys' ) {
$classes = str_replace( 'current_page_parent', '', $classes );
if ( $item->url == '/attorneys' ) {
// Replace "attorneys" with your code
if(preg_match('/attorneys/', $item->url)) {
$classes = str_replace( 'menu-item', 'menu-item current_page_parent', $classes );
}
}
return $classes;
}
Altered from here: https://wordpress.org/support/topic/custom-post-type-highlighting-current-menu-item