Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on WordPress.org (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

Element Help

  • Greetings, I’m using the imprint site from your site library. When I show a category of products and use the before and after element to scroll to another product, how can I contain that scroll to just that category that I’ve chosen and or I’m currently in? At the moment as I go right or left it begins to show all products and not just that category.

    Thanks!

  • fernandoazarcon2

    Hi Riverman,

    The category archive should only show products from that category. Can you share a link to a category page of your product?

  • Absolutely. Here’s the url. https://kencasadyart.com/
    Click on Zion National Park, then click on #2 and then start scrolling, this is where it shows artwork from other categories instead of staying in that category. Probably and Element design and that’s where I need your help. Thanks!!

  • Hi there,

    Can you try modifying the code in the hook element to this?

    <?php
    $current_post_id = get_the_ID();
    $category = get_the_category($current_post_id);
    $category_id = isset($category[0]) ? $category[0]->term_id : 0;
    
    echo '<div class="prev_next_buttons">';
    $previous = previous_post_link('%link', '&larr;', true, '', 'category', $category_id);
    $next = next_post_link('%link', '&rarr;', true, '', 'category', $category_id);
    echo $previous;
    echo $next;
    echo '</div>';
    ?>
  • Same result. Do I need to change the display rules?

  • fernandoazarcon2

    Can you share admin login credentials through the Private Information field?

  • Yes, listed below.

  • Can you try this instead? It’s actually more complicated as I thought, here’s the updated code.

    Or you can try creating a block element – post navigation, it has the built-in option to choose the same category.

    <? echo '<div class="prev_next_buttons">';
    
    // Get the current product
    global $product;
    
    // Get the product categories
    $categories = wp_get_post_terms( get_the_ID(), 'product_cat', array( 'fields' => 'ids' ) );
    
    // Query for the previous product
    $previous_product = get_adjacent_product( 'previous', $categories );
    if ( $previous_product ) {
        $previous_link = get_permalink( $previous_product->get_id() );
        echo '<a href="' . esc_url( $previous_link ) . '">&larr;</a>';
    }
    
    // Query for the next product
    $next_product = get_adjacent_product( 'next', $categories );
    if ( $next_product ) {
        $next_link = get_permalink( $next_product->get_id() );
        echo '<a href="' . esc_url( $next_link ) . '">&rarr;</a>';
    }
    
    echo '</div>';
    
    /**
     * Get the adjacent product within the same category
     *
     * @param string $direction 'previous' or 'next'.
     * @param array $categories Array of category IDs.
     * @return WC_Product|false The adjacent product or false if not found.
     */
    function get_adjacent_product( $direction, $categories ) {
        $product_id = get_the_ID();
        
        // Get the query arguments
        $args = array(
            'post_type'      => 'product',
            'posts_per_page' => 1,
            'post_status'    => 'publish',
            'orderby'        => 'menu_order',
            'order'          => 'ASC',
            'tax_query'      => array(
                array(
                    'taxonomy' => 'product_cat',
                    'field'    => 'term_id',
                    'terms'    => $categories,
                ),
            ),
        );
        
        // Set the order direction based on previous or next
        if ( $direction === 'previous' ) {
            $args['order'] = 'DESC';
        }
        
        // Set the offset based on current product
        $current_key = array_search( $product_id, $categories );
        if ( $current_key !== false ) {
            if ( $direction === 'previous' ) {
                $offset = $current_key + 1;
            } else {
                $offset = $current_key - 1;
            }
            if ( isset( $categories[ $offset ] ) ) {
                $args['offset'] = $offset;
            }
        }
        
        // Query for the adjacent product
        $query = new WP_Query( $args );
        
        if ( $query->have_posts() ) {
            $query->the_post();
            $product = wc_get_product( get_the_ID() );
            wp_reset_postdata();
            return $product;
        }
        
        return false;
    } ?>
    
  • Thank you Ying. Your response has now thinking about a redesign, which is probably what it needs right? Thank you again!

  • I would definitely recommend trying creating a block element – post navigation instead of the code.

    In that way, you can define your own style and functionality 🙂

Viewing 10 posts - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.