• Resolved MarkJ

    (@forusak)


    I am using this code and it works only in some loops. Any idea, why it does not work with woocommerce product shortcode? It adds a class to each item instead.

    add_filter( 'post_class', 'first_item' );
    function first_item( $classes ) {
       global $wp_query;
       if( 0 === $wp_query->current_post )
          $classes[] = 'first-item-class';
          return $classes;
       }
    }
    • This topic was modified 4 years, 11 months ago by MarkJ.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Support Gabriel – a11n

    (@gabrielfuentes)

    Hi there 👋

    This is a fairly complex development topic. I’m going to leave it open for a bit to see if anyone is able to chime in to help you out.

    I can also recommend the WooCommerce Developer Resources Portal for resources on developing for WooCommerce.

    You can also visit the WooCommerce Facebook group or the #developers channel of the WooCommerce Community Slack. We’re lucky to have a great community of open-source developers for WooCommerce, and many of our developers hang out there, as well.

    Cheers!

    Thread Starter MarkJ

    (@forusak)

    Thanks @gabrielfuentes for tips, I joined Slack.

    As I can’t find help with this, I resolved it by allowing function to run only once. It is good enough in this case, but still it is not answering why is it happening.

    New code is:

    add_filter('post_class', 'first_item');
    function first_item($classes)
    {
       static $foo_called = false;
       if (!$foo_called) {
          global $wp_query;
          if (0 == $wp_query->current_post) {
             $classes[] = 'first-item-class';
             $foo_called = true;
             return $classes;
          } else {
             return $classes;
          }
       }
    }

    I am not closing it as resolved yet, if someone would like to clarify it.

    • This reply was modified 4 years, 11 months ago by MarkJ.
Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Add class for first item in shortcode loop’ is closed to new replies.