• Здравствуйте, подскажите пожалуйста, вот код функции my_trending_tags

    function my_trending_tags() {
    
        $categories =  get_categories(array( 'taxonomy' => 'post_tag', 'hide_empty'       => 0, 'exclude' => '14', 'orderby' => 'rand', 'number' => '4' ));
    
         foreach ($categories as $category) {
            $termlink = get_category_link( $category->term_id );
            $termname = $category->cat_name;
            $tax_term_id = $category->term_taxonomy_id;
            $images = get_option('taxonomy_image_plugin');
            echo  '<a title='.$termname.' href='.$termlink.' style="background:url('.wp_get_attachment_image_src( $images[$tax_term_id], 'full' )[0].') no-repeat top center;"></a>';
    
    }
    }

    Все отлично, картинка отображается, но ‘orderby’ => ‘rand’, не работает, показывает постоянно одни и те же метки, хотя их больше и каждая привязана к записи.

Просмотр 2 ответов — с 1 по 2 (всего 2)
  • Модератор Sergey Biryukov

    (@sergeybiryukov)

    Live and Learn

    rand не входит в число допустимых параметров:

    ‘orderby’
    (string) Field(s) to order terms by. Accepts term fields (‘name’, ‘slug’, ‘term_group’, ‘term_id’, ‘id’, ‘description’), ‘count’ for term taxonomy count, ‘include’ to match the ‘order’ of the $include param, or ‘none’ to skip ORDER BY. Defaults to ‘name’.

    Добавлять его не имеет смысла, потому что у get_categories(), в отличие от get_posts(), нет разбиения по страницам.

    Можно запросить все рубрики (бонус в том, что такой запрос можно кешировать) и затем отсортировать в случайном порядке средствами PHP:

    $categories = get_categories( array(
    	'taxonomy'   => 'post_tag',
    	'hide_empty' => 0,
    	'exclude'    => '14',
    ) );
    
    shuffle( $categories );
    
    $categories = array_slice( $categories, 0, 4 );

    Вы просто гений. Спасибо большое!

Просмотр 2 ответов — с 1 по 2 (всего 2)
  • Тема «'orderby' => 'rand' не работает.» закрыта для новых ответов.