Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress WooCommerce store - Subtitle under Products Titles

I've been reading WP forums and trying different plugins for over a week now with no luck, so I decided to give it a try here.

I'm creating a WP website with a premium theme, that supports a woocommerce. What I need to do is the following:

  • Create a subtitle area (which would be named REG. NO:), so I could not only write Title of the product, but subtitle also. So when you would open a single product page, it would be like:

This_is_my_product_title REG.NO: this_is_my_reg_no

  • another issue is, I would need a REG.NO: (subtitle) to be an external hyperlink to a different website.

Greatest thanks to anyone who could help me out.

like image 408
Prima Avatar asked Aug 18 '15 09:08

Prima


People also ask

How do I add a sub title in WordPress?

To do this, open up the post or page you want to edit and then enter your subtitle in the 'Secondary Title' box in the right hand menu. Then, click 'Update' or 'Publish' to make your changes live. Now, when your visitors view your post, they'll see your new subtitle.

How do I add text after a product title in WooCommerce?

Add Text After the Product TitleUse the woocommerce_single_product_summary action hook to customize the default product title with our custom text add it. add_action( 'woocommerce_single_product_summary', 'add_custom_text_after_product_title', 5); After adding the code in your theme's functions.


2 Answers

If you want to go pure WooCommerce way, here's the gist.

1 - Add custom field ( this code goes in functions.php )

add_action( 'woocommerce_product_options_general_product_data', 'my_custom_field' );

function my_custom_field() {

woocommerce_wp_text_input( 
    array( 
        'id'          => '_subtitle', 
        'label'       => __( 'Subtitle', 'woocommerce' ), 
        'placeholder' => 'Subtitle....',
        'description' => __( 'Enter the subtitle.', 'woocommerce' ) 
    )
);

}

The field will appear as shown in this screen grab : http://i.imgur.com/fGC86DA.jpg

2 - Save the field's data when product is saved. ( this code goes in functions.php )

add_action( 'woocommerce_process_product_meta', 'my_custom_field_save' );

function my_custom_field_save( $post_id ){  

    $subtitle = $_POST['_subtitle'];
    if( !empty( $subtitle ) )
        update_post_meta( $post_id, '_subtitle', esc_attr( $subtitle ) );

}

3 - Edit single product template and display the field's value

<?php 
global $post;
echo get_post_meta( $post->ID, '_subtitle', true );
?>
like image 182
Anand Shah Avatar answered Oct 21 '22 22:10

Anand Shah


Ok so for everyone else who might have the same problem. Although both of options posted already are worth considering and will definitely save them as favorites because I'm sure I will need this in the future, this is the solution that worked best for me.

Although I'm trying to use as few plugins as possible, I ultimately decided to go with KIA SUBTITLE plugin. Then, you have to write this code in your functions.php:

function kia_add_subtitle_link_to_woocommerce(){
if( function_exists( 'the_subtitle' ) ){

    $link = the_subtitle( '<h2 class="subtitle"><a href="%s" title="%s">', '</a></h2>', false );

    printf( $link, get_permalink(), sprintf( __( 'Permalink to %s', 'your-text-domain' ), get_the_title() ) );
}
}

add_action( 'some_custom_hook', 'kia_add_subtitle_link_to_woocommerce' );

I used the following hook:

add_action( 'woocommerce_single_product_summary', 'kia_add_subtitle_link_to_woocommerce' );
like image 2
Prima Avatar answered Oct 21 '22 20:10

Prima