I'm trying to figure a way to use the home icon from FontAwesome in my breadcrumb trail for WooCommerce but it just shows up as a blank. Basically, I tweaked the code Woo provides to replace breadcrumb text with other text, but I guess it doesn't like HTML tags. This is my code.
add_filter( 'woocommerce_breadcrumb_defaults', 'wcc_change_breadcrumb_home_text' );
function wcc_change_breadcrumb_home_text( $defaults ) {
$defaults['home'] = '<i class="fas fa-home"></i>';
return $defaults;
}
The filter only allows you to change the text without adding HTML tags as the home delimiter is wrapped in esc_html(). Here's one solution. Change the filter to the following:
add_filter( 'woocommerce_breadcrumb_defaults', 'wcc_change_breadcrumb_home_text' );
function wcc_change_breadcrumb_home_text( $defaults ) {
$defaults['before'] = '<span class="nmr-crumb">';
$defaults['after'] = '</span>';
$defaults['home'] = ' ';
return $defaults;
}
Use a space inside the single quotes on the line with $defaults['home'] = ' ';
Next, you'll need to add the following to your theme style.css:
.woocommerce-breadcrumb .nmr-crumb:first-child a {
text-decoration: none;
}
.woocommerce-breadcrumb .nmr-crumb:first-child a::before {
font-family: FontAwesome;
content: '\f015';
}
Credit: https://wordpress.org/support/topic/swap-home-for-icon/
A less elegant solution would be to edit the breadcrumbs template file. You'll need to use a child theme and have an existing breadcrumbs template file under:
/woocommerce/global/breadcrumb.php
where / is your child theme folder. Inside breadcrumb.php find the following if statement:
if ( ! empty( $crumb[1] ) && sizeof( $breadcrumb ) !== $key + 1 ) {
echo '' . esc_html( $crumb[0] ) . '';
} else {
echo esc_html( $crumb[0] );
}
change the above if statement to the following:
if ( ! empty( $crumb[1] ) && sizeof( $breadcrumb ) !== $key + 1 ) {
if (0==$key){
echo '<i class="fas fa-home"></i>';
}else{
echo '' . esc_html( $crumb[0] ) . '';
}
}else{
echo esc_html($crumb[0]);
}
Here's a guide on how to edit WooCommerce template files.
Related
I'm trying to display some attributes directly on the shop page. The attributes are all collected in the database. To make it easier to show, I made a screenshot so you can get a better idea. the thing is now, with which code I can do that. the attributes, there are always 4, should be display centered under the block with the pictures and the buy button. I would be very happy if we could find a way to do this. Thanks very much
Ok, i've found some code here on stackoverflow... the good news are, i get the results/attributes i want, the bad news are, on the wrong page (shop page instead of shop page single).
this is the code:
add_action('woocommerce_after_shop_loop_item_title', 'display_custom_product_attributes_on_loop', 5 );
function display_custom_product_attributes_on_loop() {
global $product;
// Settings: Here below set your product attribute label names
$attributes_names = array('alter', 'bausteine', 'publicationdate', 'sku');
$attributes_data = array(); // Initializing
// Loop through product attribute settings array
foreach ( $attributes_names as $attribute_name ) {
if ( $value = $product->get_attribute($attribute_name) ) {
$attributes_data[] = $attribute_name . ': ' . $value;
}
}
if ( ! empty($attributes_data) ) {
echo '<div class="items" style="color: red;"><p>' . implode( '<br>', $attributes_data ) . '</p></div>';
}
}
this code shows me the attributes and the results on the shoppage, but i need it on the single shop page/product page.
Thank you!
Change the hook name and then check
add_action('woocommerce_single_product_summary', 'display_custom_product_attributes_on_loop', 5 );
function display_custom_product_attributes_on_loop() {
global $product;
// Settings: Here below set your product attribute label names
$attributes_names = array('alter', 'bausteine', 'publicationdate', 'sku');
$attributes_data = array(); // Initializing
// Loop through product attribute settings array
foreach ( $attributes_names as $attribute_name ) {
if ( $value = $product->get_attribute($attribute_name) ) {
$attributes_data[] = $attribute_name . ': ' . $value;
}
}
if ( ! empty($attributes_data) ) {
echo '<div class="items" style="color: red;"><p>' . implode( '<br>', $attributes_data ) . '</p></div>';
}
}
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 got this code from researching and trying to modify it but no luck. I want the last item in yoast breamcrumbs not to appear on single pages only
if ( is_single() ) {
/*Remove Last item in Yoast SEO Breadcrumb */
function adjust_single_breadcrumb( $link_output) {
if(strpos( $link_output, 'breadcrumb_last' ) !== false ) {
$link_output = '';
}
return $link_output;
}
add_filter('wpseo_breadcrumb_single_link', 'adjust_single_breadcrumb' );
};
I tried adding if is single in between the function and also this
if(strpos( $link_output, 'breadcrumb_last' ) !== false && is_single) {
unfortunately, the whole breadcrumb disappears.
with this code, you can remove post title in single page.
add_filter('wpseo_breadcrumb_single_link_info', 'remove_post_title_wpseo_breadcrumb', 10, 3);
function remove_post_title_wpseo_breadcrumb($link_info, $index, $crumbs)
{
if (is_singular() && isset($link_info['id']))
return [];
return $link_info;
}
Updated: In the latest version above code does not work perfectly.
for this reason, I removed the last item with regex:
function getBreadcrumb() {
if ( function_exists( 'yoast_breadcrumb' ) ) {
ob_start();
yoast_breadcrumb( '<p id="breadcrumb" class="meta-info">', '</p>' );
$breadcrumb = trim( ob_get_clean() );
if ( is_singular() )
$breadcrumb = trim( preg_replace( '/ ' . WPSEO_Options::get( 'breadcrumbs-sep' ) . ' <span class="breadcrumb_last" aria-current="page">(.*)<\/span>/i', '',
$breadcrumb ) );
echo $breadcrumb;
}
}
And call the function anywhere you want:
<?php getBreadcrumb() ?>
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'm relatively new to WordPress theme development, and I had to create a theme with options tree. I have successfully add some options with options tree plugin in my wordpress theme.But i am really stand when i go to add Background option. I have complete section an settings on theme option with 'type' => 'background', after i see i have find some options on dashboards theme options like 'select color', 'background-repeat', ''background-attachment', 'background-position' and background size. Now i want to query all methods but i have did'nt know how can i do this. exactly i want to do dynamic this code
body{background:url(from option tree attach file) option tree repeat option scroll options tree position options tree background size options tree color}
this is exact css body{background:url(img/body_bg.png) no-repeat scroll 0 0 # ddd}. Any one Please help me.
You can try something like this....
<?php $bg_body = ot_get_option( 'bg_body', array() ); ?>
body
{
background-color: <?php if($bg_body['background-color']){echo $bg_body['background-color'] ; }else{ echo '#ffffff';} ?>;
background-repeat:<?php if($bg_body['background-repeat']){echo $bg_body['background-repeat'] ; }else{ echo 'repeat-x';} ?>;
background-attachment:<?php if($bg_body['background-attachment']){echo $bg_body['background-attachment'] ; }else{ echo 'fixed';} ?>;
background-position:<?php if($bg_body['background-position']){echo $bg_body['background-position'] ; }else{ echo 'top';} ?>;
background-image:url(<?php if($bg_body['background-image']){echo $bg_body['background-image'] ; }else{ echo get_template_directory_uri().'/images/bg.png';} ?>) ;
}
I personally use this for my premium themes.
www.wpmania.net
so first you have to create a option for background.....
then you can show the option using following code....
body
{
background-color: ;
background-repeat:;
background-attachment:;
background-position:;
background-image:url() ;
}
Actually when you create a background option it generate an array and you need to have all the data separately. I also improved the above code into a PHP function. Please check it out as well.....
// A function that will create css for background options
function WpmBgColor( $wpm_options, $wpm_class, $identifier){
$wpm_options = ot_get_option( $wpm_options, array() );
if($wpm_options['background-color'] | $wpm_options['background-repeat'] | $wpm_options['background-attachment'] | $wpm_options['background-position'] | $wpm_options['background-image'] ){ echo
$identifier.$wpm_class . "{
background-color: ".$wpm_options['background-color'].";
background-repeat:".$wpm_options['background-repeat'].";
background-attachment:".$wpm_options['background-attachment'].";
background-position:".$wpm_options['background-position'].";
background-image:url(".$wpm_options['background-image'].") ;
background-size:".$wpm_options['background-size'].";
} ";
}
}
This function has several arguments...
$wpm_options : it will be your option tree field id
$wpm_class : Your css selector name for which you are adding css
$identifier : your css selector identifier whether it is a class or id. Just put # when it is id and put " . " when it is a class. And leave it blank when it is a HTML tag. Like for body you can leave it empty.
PM me if you need more clarification.
Thanks
Sabbir
People are asking me how to use background option for betabox ( OptionTree Metabox ).....
First of all, you have to write the following codes within the loop Otherwise it might not work......
<?php
$MyBg= get_post_meta($post->ID, 'option_tree_meta_value', false);
$MyBg = $MyBg['0'];
?>
<style type="text/css">
<!--
.css_selector
{
background-color: <?php if($MyBg['background-color']){echo $MyBg['background-color'] ; }else{ echo '#ffffff';} ?>;
background-repeat:<?php if($MyBg['background-repeat']){echo $MyBg['background-repeat'] ; }else{ echo 'repeat-x';} ?>;
background-attachment:<?php if($MyBg['background-attachment']){echo $MyBg['background-attachment'] ; }else{ echo 'fixed';} ?>;
background-position:<?php if($MyBg['background-position']){echo $MyBg['background-position'] ; }else{ echo 'top';} ?>;
background-image:url(<?php if($MyBg['background-image']){echo $MyBg['background-image'] ; }else{ echo get_template_directory_uri().'/images/bg.png';} ?>) ;
}
-->
</style>
If you still need more help just PM me. I must get back to you when I get some time.
#wpmania.net
I am currently using the wordpress inline style function to add the css for posts & pages after my main stylesheet is being loaded. Using the metaboxes from option tree ofc.
// Inline style for post/page backgrounds
if ( is_single() || is_page() ) {
$background = get_post_meta( get_the_ID(), 'my_post_background_metabox', true );
if ( !empty( $background ) ) {
$background_color = ( $background['background-color'] != '' ) ? $background['background-color'] . ' ' : '';
$background_image = ( $background['background-image'] != '' ) ? 'url('.$background['background-image'].') ' : '';
$background_repeat = ( $background['background-repeat'] != '' ) ? $background['background-repeat']. ' ' : '';
$background_positon = ( $background['background-position'] != '' ) ? $background['background-position']. ' ' : '';
$background_attachment = ( $background['background-attachment'] != '' ) ? $background['background-attachment']. ' ' : '';
$background_size = ( $background['background-size'] != '' ) ? 'background-size: '. $background['background-size']. ';' : '';
$custom_post_background =
'/* Custom Background for ' . get_the_title() . ' */' . "\n" .
'.css_selector { background: '.$background_color.$background_image.$background_repeat.$background_attachment.$background_positon.';'."\n". $background_size .'}';
wp_add_inline_style( 'my_theme_style', $custom_post_background );
}
}
EDIT: I forgot to mention that this part of code is inside the main wp_enqueue_script function