Add subtitle to Woocommerce product title

I’m using WooCommerce on a site built with Pagelines Framework. I need to have a subtitle/customisable field appear under the product name anywhere it appears in the site. As it is, WooCommerce doesn’t offer that option.

I’ve tried using custom fields but WooCommerce use these too and output a bunch of stuff I don’t want along with my subtitle. If I were to name my custom field “bookauthor” would this code work to display only the custom field I want?

Read More
<?php echo get_post_meta($id, "bookauthor", true); ?>

And if so, how do I make my new field output straight after the product title on the front end?

I’ve found the hooks I need in this php file (I think, I don’t know php, which is why I’m asking you):

    <?php 
/*
  * @hooked woocommerce_template_single_title - 5
  * @hooked woocommerce_template_single_price - 10
  * @hooked woocommerce_template_single_excerpt - 20
  * @hooked woocommerce_template_single_add_to_cart - 30
  * @hooked woocommerce_template_single_meta - 40
  * @hooked woocommerce_template_single_sharing - 50
*/
?>

I know how to filter out but how do I add in the custom field to that list?

Or is there an entirely different way to achieve what I need?

Undying gratitude to anyone who can help.

Related posts

2 comments

  1. To answer your first question, getting your post meta »bookauthor« this way will echo/display just that. If you are defining the variable $id in your code right – or you can do it like shown below.

    The code should answer your second question, how to insert your second title line to the product page via the hook woocommerce_single_product_summary. Just add your extra information like this:

        function wpse116660_wc_add_2nd_title() {
            ?>
            <div class="2nd-tile">
                <?php echo get_post_meta(get_the_ID(), "bookauthor", true); ?>
            </div>
            <?php
        }
        add_action( 'woocommerce_single_product_summary', 'wpse116660_wc_add_2nd_title', 6 );
    

    To have some more comfort with your custom post meta you can do what @pl4g4 and @brasofilo suggested and add a metabox to the product edit screen, but it’s not necessary of course, you seem to know how to do it with the standard wordpress custom fields metabox.


    You can add your meta box like this, the code is based on the first example on the add_meta_box wordpress codex page.

    /**
     * Adds a box to the main column on the Post and Page edit screens.
     */
    function wpse116660_wc_2nd_title_mb() {
    
        $screen = array( 'product' );
    
            add_meta_box(
                'wc_2nd_title_mb',
                __( '2nd title', 'your_textdomain' ),
                'wc_2nd_title_inner_mb',
                $screen,
                'advanced',
                'high'
            );
    }
    add_action( 'add_meta_boxes', 'wpse116660_wc_2nd_title_mb', 0 );
    
    /**
     * Prints the box content.
     * 
     * @param WP_Post $post The object for the current post/page.
     */
    function wpse116660_wc_2nd_title_inner_mb( $post ) {
    
      // Add an nonce field so we can check for it later.
      wp_nonce_field( 'wc_2nd_title_inner_mb', 'wc_2nd_title_inner_mb_nonce' );
    
      /*
       * Use get_post_meta() to retrieve an existing value
       * from the database and use the value for the form.
       */
      $value = get_post_meta( $post->ID, 'bookauthor', true );
    
      echo '<label for="bookauthor_field">';
           _e( "Bookauthor", 'your_textdomain' );
      echo '</label> ';
      echo '<input type="text" id="bookauthor_field" name="bookauthor_field" value="' . esc_attr( $value ) . '" size="50" />';
    
    }
    
    /**
     * When the post is saved, saves our custom data.
     *
     * @param int $post_id The ID of the post being saved.
     */
    function wpse116660_wc_2nd_title_save_postdata( $post_id ) {
    
      /*
       * We need to verify this came from the our screen and with proper authorization,
       * because save_post can be triggered at other times.
       */
    
      // Check if our nonce is set.
      if ( ! isset( $_POST['wc_2nd_title_inner_mb_nonce'] ) )
        return $post_id;
    
      $nonce = $_POST['wc_2nd_title_inner_mb_nonce'];
    
      // Verify that the nonce is valid.
      if ( ! wp_verify_nonce( $nonce, 'wc_2nd_title_inner_mb' ) )
          return $post_id;
    
      // If this is an autosave, our form has not been submitted, so we don't want to do anything.
      if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
          return $post_id;
    
      // Check the user's permissions.
      if ( 'page' == $_POST['post_type'] ) {
    
        if ( ! current_user_can( 'edit_page', $post_id ) )
            return $post_id;
    
      } else {
    
        if ( ! current_user_can( 'edit_post', $post_id ) )
            return $post_id;
      }
    
      /* OK, its safe for us to save the data now. */
    
      // Sanitize user input.
      $mydata = sanitize_text_field( $_POST['bookauthor_field'] );
    
      // Update the meta field in the database.
      update_post_meta( $post_id, 'bookauthor', $mydata );
    }
    add_action( 'save_post', 'wpse116660_wc_2nd_title_save_postdata' );
    
  2. You can add an extra metabox to the product post. This meta box should have and input form so u can enter the sub-title, when you have added the metabox save the value in the post_meta when the product is saved, then in the single product page from the woocommerce template use the code

    <?php echo get_post_meta($id, "bookauthor", true); ?>
    

    to get it.

    you can find info about metaboxess Here And also Here

Comments are closed.