M
M
mack-reid2019-08-09 15:02:42
metadata
mack-reid, 2019-08-09 15:02:42

How to display article tags on a Wordpress category page?

You need to display a list of tags, under each of them - a list of articles that have this tag. I have all articles displayed under all tags.

function wph_alltags_shortcode($atts, $content) {
    $posttags = get_tags();
    if($posttags) {
        foreach($posttags as $tag) {
            $output.='<div class="category info"><div class="category-name">'.$tag->name.'</div> ';
      $args = array(
                   'post_type' => 'info',
                   'publish' => true,
                   'paged' => get_query_var('paged'),
               );
            $output.='<div class="owl-carousel owl-theme">';
      $image_id = get_post_thumbnail_id();			
      $the_query = new WP_Query( $args );
      while  ($the_query->have_posts() ) : $the_query->the_post();
        $output.='<div class="item"><a href="'.get_permalink().'">'.get_the_post_thumbnail( $image_id,'large' ).'<div class="name">'.get_the_title().'</div></a></div>';
      endwhile;
      $output.='</div>';

      wp_reset_postdata();
      $output.='</div>';
        }
    }
    return $output;
}
add_shortcode('alltags', 'wph_alltags_shortcode');

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
mack-reid, 2019-08-12
@mack-reid

'tag' => $tag->slug,

A
Anton Neverov, 2019-08-09
@TTATPuOT

In $args add
'tag' => $tag->name

D
deadnice, 2019-08-09
@deadnice

You need to add a value to the args array with something like the following content:

'tax_query' => array(
    array(
      'taxonomy' => 'taxonomyName',
      'field'    => 'id',
      'terms'    => $tag->term_id
    )
  )

Where taxonomyName is the registered name of your tags when registering your "info" post type https://wp-kama.ru/function/register_post_type
Your final code:
function wph_alltags_shortcode($atts, $content) {
  $posttags = get_tags();
  if($posttags) {
    foreach($posttags as $tag) {
      $output.='<div class="category info"><div class="category-name">'.$tag->name.'</div> ';
      $args = array(
           'post_type' => 'info',
           'publish' => true,
           'paged' => get_query_var('paged'),
           'tax_query' => array(
            array(
              'taxonomy' => 'taxonomyName',
              'field'    => 'id',
              'terms'    => $tag->term_id
            )
          )
      );
      $output.='<div class="owl-carousel owl-theme">';
      $image_id = get_post_thumbnail_id();			
      $the_query = new WP_Query( $args );
      while  ($the_query->have_posts() ) : $the_query->the_post();
        $output.='<div class="item"><a href="'.get_permalink().'">'.get_the_post_thumbnail( $image_id,'large' ).'<div class="name">'.get_the_title().'</div></a></div>';
      endwhile;
      $output.='</div>';

      wp_reset_postdata();
      $output.='</div>';
    }
  }
  return $output;
}
add_shortcode('alltags', 'wph_alltags_shortcode');

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question