skip to Main Content

I have this code that adds a custom field to my products in woocommerce:

add_action( 'woocommerce_single_product_summary', 'shoptimizer_custom_author_field', 3 );
function shoptimizer_custom_author_field() { ?>
<?php if(get_field('author')) { ?>
<div class="cg-author"><?php the_field('author'); ?></div>
<?php }
}

Now I would want to add a condition to the if-statement that says "if field is not empty, hide product title".

The class for the product page product title seems to be "product_title".

Will be fascinating how this will look like once It’s added into this piece of code above. I think it’s not a big deal, but my comprehension ends with HTML and CSS sadly.

2

Answers


  1. Chosen as BEST ANSWER

    Just to conclude this thread, below anybody that seeks a similar answer can copy and paste the solution that worked for me:

    add_action( 'wp_head', function() {
    
        ?><style>
        h1.product_title.entry-title {
        display: none;
    }
    
    .cg-title h1.product_title.entry-title {
        display: block !important;
    }
        }</style><?php
        
    } );
    
    
    add_action( 'woocommerce_single_product_summary', 'shoptimizer_custom_author_field', 3 );
      
    function shoptimizer_custom_author_field() { ?>
    
    <?php if(get_field('author')) { ?>
        
        <div class="cg-author"><h1><?php the_field('author'); ?></h1></div>
    <?php }
    else {?>
    <div class="cg-title"><?php woocommerce_template_single_title(); ?> </div> 
    <?php   
        }
    }


  2. I’m not sure if I understand your question correctly, but if you want to hide the product title if the custom field is not empty, you can use the following code:

    add_action('woocommerce_single_product_summary', 'shoptimizer_custom_author_field', 3);
    
    function shoptimizer_custom_author_field() {
        if (get_field('author')) {
            echo '<style>.product_title { display: none; }</style>';
        }
    }
    

    If you want to hide the product title if the custom field is empty, you can use the following code:

    add_action('woocommerce_single_product_summary', 'shoptimizer_custom_author_field', 3);
    
    function shoptimizer_custom_author_field() {
        if ( ! get_field('author')) {
            echo '<style>.product_title { display: none; }</style>';
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search