I
I
Ivan2020-06-02 16:12:43
Taxonomy
Ivan, 2020-06-02 16:12:43

wp how to add number of categories?

Good afternoon!
There is a piece of code that displays taxonomies (categories), but since there are a lot of them, they drag down like an endless sheet. Tell me how to display only 5 categories or how to display certain categories on the page. Thank you!

<div class="categories">
    <?php
    
    $terms = get_categories();
    $count = count($terms);
    if ($count > 0) {
      foreach ($terms as $term) {
        ?>
        <a href="<?php echo get_term_link($term); ?>" class="item">
          <?php
          $images = get_option('taxonomy_image_plugin');
          if (array_key_exists($term->term_id, $images)) {
            $url = wp_get_attachment_image_url($images[$term->term_id], 'full');
          }
          ?>
          <span class="img" style="background-image: url(<?php echo $url; ?>);">
              <span class="detal">
                <?php 
                  if (ICL_LANGUAGE_CODE == 'ru') {echo 'Подробнее';}
                  if (ICL_LANGUAGE_CODE == 'en') {echo 'detal';} 
                ?>
              </span>
            </span>
          <span class="text"><p><?php echo $term->name; ?></p></span>
        </a>
        <?php
      }
    }
    ?>
  </div>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Sedov, 2020-06-02
@ivansedov

if ($count > 0 && $count <= 5) or $terms = get_categories(['number' => 5]);
And if you need to display certain categories, read the article (look towards include )

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question