A
A
akf_13rm2021-09-24 14:16:50
PHP
akf_13rm, 2021-09-24 14:16:50

How to display a custom post type depending on the taxonomy that belongs to it?

Hello!
I'm working on stretching the layout on Wordpress and faced a task that I don't know how to solve: I
created a custom_post_type (programs) and a taxonomy (genretax). Now I want to display records on the archive-programs.php page depending on the taxonomy specified for them. As in the screenshot:
614db0040426f811333080.png
The layout uses the MixItUp 3 library (for the tile filter) https://www.kunkalabs.com/mixitup/
I now display like this: That is,
614db117c6965201209961.png
the posts are linked to the taxonomy correctly, but the taxonomies themselves are duplicated ..
Please help, already I've been trying for 3 days and it doesn't work.
Taxonomy code:

register_taxonomy( 'genretax', [ 'programs', 'regions' ], [
  'label'                 => '',
  'labels'                => [
    'name'              => 'Регионы активности',
    'singular_name'     => 'Регион активности',
    'search_items'      => 'Поиск регионов активности',
    'all_items'         => 'Все Регионы активности',
    'view_item '        => 'Просмотр Региона активности',
    'parent_item'       => 'Parent Регионы активности',
    'parent_item_colon' => 'Parent Регионы активности:',
    'edit_item'         => 'Изменить Регионы активности',
    'update_item'       => 'Обновить Регионы активности',
    'add_new_item'      => 'Добавить новый регион активности',
    'new_item_name'     => 'New Регионы активности Name',
    'menu_name'         => 'Регионы активности',
  ],
  'description'           => '',
  'public'                => true,
  'publicly_queryable'    => true,
  'show_ui'               => true,
  'hierarchical'          => true,

  'rewrite'               => true,
  'capabilities'          => array(),
  'meta_box_cb'           => null,
  'show_admin_column'     => false,
  'show_in_rest'          => true,
  'rest_base'             => null,
] );


custom_post_type code:
register_post_type( 'programs',
    array(
      'labels'      => array(
        'name'          => esc_html__( 'Программы', ),
        'singular_name' => __( 'Программы' ),
      ),
      'public'      => true,
      'has_archive' => true,
      'menu_icon'   => 'dashicons-admin-site',
      'supports'    => array( 'title', 'editor', 'thumbnail' ),
      'show_in_rest' => true,
      'taxonomies'          => array('genretax' ),
    )
  );


Taxonomy output code:
<?php
        $args = array(
          'post_type' => 'programs',
        );
        $query = new WP_Query( $args );

        if ($query->have_posts()) {

          while ( $query->have_posts() ) : $query->the_post();
            $page_categories = get_the_terms( get_the_ID(), 'genretax' );
            if ( $page_categories && ! is_wp_error( $page_categories ) ) {

              foreach ( $page_categories as $page_category ) {

                ?>
                                <li class="filter" data-filter=".category<?php the_ID(); ?>">
                                    <i>
                                        <img src="<?php echo carbon_get_term_meta($page_category->term_id, 'crb_thumb'); ?>" alt="">
                                    </i>
                                    <?php	echo $page_category->name; ?>
                                </li>

                <?php
              }
            }
          ?>
          <?php endwhile;
        }
        wp_reset_postdata();

        ?>


Post output code:
<?php
        $args = array(
          'post_type' => 'programs',
        );
        $query = new WP_Query( $args );

        if ($query->have_posts()) {

          while ( $query->have_posts() ) : $query->the_post(); ?>

                        <li class="mix category<?php the_ID(); ?>">
                            <a href="<?php the_permalink(); ?>" class="img_col_item_programs">
                              <?php echo get_the_post_thumbnail(get_the_ID()); ?>
                            </a>
                            <a href="<?php the_permalink(); ?>" class="link_col_item_programs"><?php the_title(); ?></a>
                        </li>

            <?php

           endwhile;

        }
        wp_reset_postdata(); ?>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladislav Chernenko, 2021-09-25
@akf_13rm

You get all categories for all posts... You just need to call all categories:

<?php $terms = get_terms('genretax');
foreach($terms as $term){ ?>
   <li class="filter" data-filter=".category<?php echo $term->term_id; ?>"><?php echo $term->name; ?></li>
<?php } ?>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question