Support » Plugin: WooCommerce » modifying the recent posts shortcode

  • Resolved charlie101

    (@charlie101)


    The following is the code for the shortcode for recent_posts –

    [code]
    add_shortcode('recent_posts', 'ns_recent_products');
    function ns_recent_products( $atts ) {
    global $woocommerce_loop;

    $atts = shortcode_atts( array(
    'per_page' => '12',
    'columns' => '4',
    'orderby' => 'date',
    'order' => 'desc',
    ), $atts );

    $meta_query = WC()->query->get_meta_query();

    $args = array(
    'post_type' => 'product',
    'post_status' => 'publish',
    'ignore_sticky_posts' => 1,
    'posts_per_page' => $atts['per_page'],
    'orderby' => $atts['orderby'],
    'order' => $atts['order'],
    'meta_query' => $meta_query
    );

    ob_start();

    $products = new WP_Query( apply_filters( 'woocommerce_shortcode_products_query', $args, $atts ) );

    $columns = absint( $atts['columns'] );
    $woocommerce_loop['columns'] = $columns;

    if ( $products->have_posts() ) : ?>

    <?php woocommerce_product_loop_start(); ?>

    <?php while ( $products->have_posts() ) : $products->the_post(); ?>

    <?php wc_get_template_part( 'content', 'product' ); ?>

    <?php endwhile; // end of the loop. ?>

    <?php woocommerce_product_loop_end(); ?>

    <?php endif;

    wp_reset_postdata();

    return '<div class="woocommerce columns-' . $columns . '">' . ob_get_clean() . '</div>';
    }
    [/code]

    As a non-PHP developer, I don’t really understand how this code works (that much!) – the code outputs the image, title and button for each recent product. I’d like to modify it to include an extra variable (a custom attribute added in the Add Product page, let’s called it ‘Extra’) to be displayed below the title variable.

    Any one can help?

    Thanks.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Support Remi Corson

    (@corsonr)

    Automattic Happiness Engineer

    Hi,

    As you can see the shortcode is creating a loop that uses the following template for every single product

    wc_get_template_part( ‘content’, ‘product’ );

    That’s where you could add custom content.

    In this case the best method would be to work with WooCommerce custom templates. Basically a custom template allows you to override WooCommerce default files and use your own custom files instead. Here is a [quick tutorial](https://docs.woocommerce.com/document/template-structure/) that will explain you how to create your custom templates. Also, if some of your existing custom templates are outdated, I would suggest [updating them](https://docs.woocommerce.com/document/fix-outdated-templates-woocommerce/).

    WooCommerce comes with a number of front-end HTML templates as well as email templates. Instead of editing these files directly within the plugin (which is a very bad idea because once update the plugin and all of your changes will be lost!), you can copy them into your theme:

    1. In your theme directory, make a new folder called woocommerce.
    2. Navigate to the WooCommerce plugin directory and open the templates folder. The templates folder has a lot of subfolders with all of the different templates that WooCommerce uses.  Fortunately, the template file structure and naming in WooCommerce is easy to follow.
    3. In your newly created woocommerce folder, copy the template file that you want to edit. Remember to keep the directory structure the same here. If the template you want to edit is within a subfolder then remember to create that subfolder within your theme’s directory.
    4. Edit the file from within your woocommerce folder and save the changes.

    Plugin Support Hannah S.

    (@fernashes)

    Automattic Happiness Engineer

    We haven’t heard back from you in a while, so I’m going to mark this as resolved – if you have any further questions, you can start a new thread.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘modifying the recent posts shortcode’ is closed to new replies.