admin管理员组

文章数量:1386682

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 4 years ago.

Improve this question

I've been using Woocommerce shortcodes alot these days (Woocommerce 3.8).

For a lot of them you can pass "class" as a value - for example products shortcode: [products class="custom-class"] and then the wrapper of the returned content will have custom-class.

But not for [reltative_products]!!??

Very useful if you have a dynamic design and would like to be able to change the design of your relative products based on what content is being showed on your page.

If you look in the bottom of the Woocommerce Docs you can see the function:

    public static function related_products( $atts ) {
        if ( isset( $atts['per_page'] ) ) {
            $atts['limit'] = $atts['per_page'];
        }

        // @codingStandardsIgnoreStart
        $atts = shortcode_atts( array(
            'limit'    => '4',
            'columns'  => '4',
            'orderby'  => 'rand',
        ), $atts, 'related_products' );
        // @codingStandardsIgnoreEnd

        ob_start();

        // Rename arg.
        $atts['posts_per_page'] = absint( $atts['limit'] );

        woocommerce_related_products( $atts );

        return ob_get_clean();
    }

As you see there is no class in the shortcode_atts???

How to change this?

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 4 years ago.

Improve this question

I've been using Woocommerce shortcodes alot these days (Woocommerce 3.8).

For a lot of them you can pass "class" as a value - for example products shortcode: [products class="custom-class"] and then the wrapper of the returned content will have custom-class.

But not for [reltative_products]!!??

Very useful if you have a dynamic design and would like to be able to change the design of your relative products based on what content is being showed on your page.

If you look in the bottom of the Woocommerce Docs you can see the function:

    public static function related_products( $atts ) {
        if ( isset( $atts['per_page'] ) ) {
            $atts['limit'] = $atts['per_page'];
        }

        // @codingStandardsIgnoreStart
        $atts = shortcode_atts( array(
            'limit'    => '4',
            'columns'  => '4',
            'orderby'  => 'rand',
        ), $atts, 'related_products' );
        // @codingStandardsIgnoreEnd

        ob_start();

        // Rename arg.
        $atts['posts_per_page'] = absint( $atts['limit'] );

        woocommerce_related_products( $atts );

        return ob_get_clean();
    }

As you see there is no class in the shortcode_atts???

How to change this?

Share Improve this question asked Apr 23, 2020 at 20:09 Peter Højlund AndersenPeter Højlund Andersen 1319 bronze badges 3
  • As a rule, the community considers questions about third-party plugins (such as woocommerce) off-topic so you might notice some down-votes and answers may be slow if they come at all. You can find out what questions are a good fit here. – Matthew Brown aka Lord Matt Commented Apr 23, 2020 at 20:14
  • 1 Okay. Thank you the notice. I was just wondering what the wocoomerce tag is for then? Is it just hundreds like me who was off topic? – Peter Højlund Andersen Commented Apr 23, 2020 at 21:22
  • 1 you are not alone in wondering this. There is a bit of an ongoing debate on this topic in meta. Anyone with enough rep can remake the tags and no one seems quite sure what we can do to make it clear. That's why I make a point of commenting on new posts when I am on the site. Personally, I'm all for opening up to some 3rd party but the long-standing users got a bit burned when this was tried before I was even here. – Matthew Brown aka Lord Matt Commented Apr 23, 2020 at 22:18
Add a comment  | 

1 Answer 1

Reset to default 1

I have found a solution but i am not sure how good it is

1) Copy the shortcode into your functions.php

If you copy the function from the above link to Woocommerce Docs and create a your own shortcode (it_related_products_function) with it and then add 'class' => '' to the arguments you can now pass 'class' to your shortcode.

function it_related_products_function( $atts ) {
 if ( isset( $atts['per_page'] ) ) {
   $atts['limit'] = $atts['per_page'];
 }

 $atts = shortcode_atts( array(
   'limit'    => '4',
   'columns'  => '4',
   'orderby'  => 'rand',
   'class'    => '',
 ), $atts, 'related_products' );

 ob_start();

 $atts['posts_per_page'] = absint( $atts['limit'] );

 woocommerce_related_products( $atts );

 return ob_get_clean();

}

2) Grab the attribute in your related.php and echo if is set

    <section class="woocommerce columns-4 <?php echo !empty($class) ? $class : "" ?>">

3) Now you can use your custom shortcode with 'class' anywhere

<?php echo do_shortcode('[it_related_products limit="4" class="row it-books-fp red-border-bottom"]'); ?>

本文标签: Pass class to Woocommerce shortcode relativeproducts