26.03.2026

Собственный шорткод для вывода товаров Woocommerce по значению атрибута (Только заголовок по Алфавиту)

Добавляем в functions.php



// Собственный шорткод для вывода товаров Woocommerce (Только заголовок по Алфавиту) zaplata.ru
function custom_product_links_shortcode($atts) {
    $atts = shortcode_atts(array(
        'limit'     => 10,
        'category'  => '',
        'attribute' => '',
        'terms'     => '',
        'orderby'   => 'title',
        'order'     => 'ASC',
    ), $atts);

    $args = array(
        'limit'   => $atts['limit'],
        'status'  => 'publish',
        'return'  => 'ids',
        'orderby' => $atts['orderby'],
        'order'   => $atts['order'],
    );

    // Фильтр по категории
    if (!empty($atts['category'])) {
        $args['category'] = array($atts['category']);
    }

    // Фильтр по атрибуту (ИСПРАВЛЕНО: через tax_query)
    if (!empty($atts['attribute']) && !empty($atts['terms'])) {
        $args['tax_query'] = array(
            array(
                'taxonomy' => 'pa_' . $atts['attribute'],
                'field'    => 'slug',
                'terms'    => array($atts['terms']),
            ),
        );
    }

    $products = wc_get_products($args);
    
    $output = '<ul class="simple-product-links">';

    if ($products) {
        foreach ($products as $product_id) {
            $output .= '<li><a href="' . get_permalink($product_id) . '">' . get_the_title($product_id) . '</a></li>';
        }
    } else {
        $output .= '<li>Товары не найдены</li>';
    }

    $output .= '</ul>';
    return $output;
}
add_shortcode('product_links', 'custom_product_links_shortcode');

Сам пример шорткода (От А-до Я)
Вы водим атрибут "Тематика" со значением "Страны"

[product_links attribute="thematics" terms="strany" limit="250" orderby="title" order="ASC"]

Обновлённый шорткод с поддержкой class и columns



function custom_product_links_shortcode($atts) {
    $atts = shortcode_atts(array(
        'limit'     => 10,
        'category'  => '',
        'attribute' => '',
        'terms'     => '',
        'orderby'   => 'title',
        'order'     => 'ASC',
        'class'     => '',           // Доп. класс для обёртки
        'columns'   => 1,            // Количество колонок (1-6)
    ), $atts);

    $args = array(
        'limit'   => $atts['limit'],
        'status'  => 'publish',
        'return'  => 'ids',
        'orderby' => $atts['orderby'],
        'order'   => $atts['order'],
    );

    // Фильтр по категории
    if (!empty($atts['category'])) {
        $args['category'] = array($atts['category']);
    }

    // Фильтр по атрибуту
    if (!empty($atts['attribute']) && !empty($atts['terms'])) {
        $args['tax_query'] = array(
            array(
                'taxonomy' => 'pa_' . $atts['attribute'],
                'field'    => 'slug',
                'terms'    => array($atts['terms']),
            ),
        );
    }

    $products = wc_get_products($args);
    
    // Формируем классы: базовые + пользовательские + колонки
    $wrapper_classes = array('simple-product-links', 'product-links-list');
    if (!empty($atts['class'])) {
        $wrapper_classes[] = sanitize_html_class($atts['class']);
    }
    if ($atts['columns'] > 1) {
        $wrapper_classes[] = 'product-columns-' . intval($atts['columns']);
    }
    
    $output = '<ul class="' . esc_attr(implode(' ', $wrapper_classes)) . '">';

    if ($products) {
        foreach ($products as $product_id) {
            $output .= '<li><a target="_blank" href="' . esc_url(get_permalink($product_id)) . '">' . esc_html(get_the_title($product_id)) . '</a></li>';
        }
    } else {
        $output .= '<li>Товары не найдены</li>';
    }

    $output .= '</ul>';
    return $output;
}
add_shortcode('product_links', 'custom_product_links_shortcode');

И сам шорткод:


<!-- Сетка + доп. класс для кастомных стилей -->
[product_links attribute="thematics" terms="strany" limit="50" columns="2" class="my-custom-style"]
guest
Закрыть меню