L
L
LastGeneral2021-08-16 10:50:19
WordPress
LastGeneral, 2021-08-16 10:50:19

Why is post category not working?

Made costume type entries Recipes:

function super_grill_recipes() {
    $labels = array(
        'name' => _x( 'Рецепты', 'Тип записей Рецепты', 'root' ),
        'singular_name' => _x( 'Рецепты', 'Тип записей Рецепты', 'root' ),
        'menu_name' => __( 'Рецепты', 'root' ),
        'all_items' => __( 'Все рецепты', 'root' ),
        'view_item' => __( 'Смотреть рецепт', 'root' ),
        'add_new_item' => __( 'Добавить новый рецепт', 'root' ),
        'add_new' => __( 'Добавить новый', 'root' ),
        'edit_item' => __( 'Редактировать рецепт', 'root' ),
        'update_item' => __( 'Обновить рецепты', 'root' ),
        'search_items' => __( 'Искать рецепт', 'root' ),
        'not_found' => __( 'Не найдено', 'root' ),
        'not_found_in_trash' => __( 'Не найдено в корзине', 'root' ),
    );

    $args = array(
        'label' => __( 'recipes', 'root' ),
        'description' => __( 'Каталог рецептов', 'root' ),
        'labels' => $labels,
        'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
        'taxonomies' => array( 'genres' ),
        'hierarchical' => false,
        'public' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'show_in_nav_menus' => true,
        'show_in_admin_bar' => true,
        'menu_position' => 5,
        'can_export' => true,
        'has_archive' => true,
        'exclude_from_search' => false,
        'publicly_queryable' => true,
        'capability_type' => 'page',
    );

    register_post_type( 'recipes', $args );
  
  register_taxonomy( 'super_grill_recipes_categories', array('recipes'), array(
        'label'                 => 'Категории рецептов',
        'labels'                => array(
            'name'              => 'Категории рецептов',
            'singular_name'     => 'Категории   ',
            'search_items'      => 'Искать категорию',
            'all_items'         => 'Все категории',
            'parent_item'       => 'Родит. категоря',
            'parent_item_colon' => 'Родит. категоря:',
            'edit_item'         => 'Редактировать категорию',
            'update_item'       => 'Обновить категорию',
            'add_new_item'      => 'Добавить категорию',
            'new_item_name'     => 'Заголовок',
            'menu_name'         => 'Категория рецептов',
        ),
            'description' => 'Категории для рецептов',
            'public' => true,
            'show_in_nav_menus' => true,
            'show_ui' => true,
            'show_tagcloud' => false,
      'hierarchical' => true,
            'rewrite' => array( 'hierarchical' => true ),
      'show_admin_column' => true,			
      'show_in_rest' => true,
      'publicly_queryable' => true,
    )
  );
}
add_action( 'init', 'super_grill_recipes' );

But when I display the name of the category, it says Array
<section class="recipes">
    <div class="container">
      <h3 class="section-title">Рецепты для гриля</h3>
      <div class="recipes-container">
        <?php
        $args = array( 'post_type' => 'recipes', 'posts_per_page' => 10 );
        $the_query = new WP_Query( $args );
        ?>
        <?php if ( $the_query->have_posts() ) : ?>
        <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

        <article class="recipes-item">
          <div class="recipes-img">
            <?php echo get_the_post_thumbnail( $page->ID, 'full'); ?>
          </div>
          <div class="recipes-title">
            <h3><?php the_title(); ?></h3>
          </div>
          <p class="recipes-category"><?php echo get_the_category(); ?></p>
          <a href="<?php echo get_permalink(); ?>" class="recipes-link"></a>
        </article>

        <?php endwhile; ?>

        <?php wp_reset_postdata(); ?>

        <?php else: ?>

        <?php endif; ?>
      </div>
    </div>
  </section>

611a19133d8d2696675772.jpeg

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
kentos, 2021-08-16
@kentos

Because get_the_category() returns an array of data, try -- get_cat_name()

A
Artem Zolin, 2021-08-16
@artzolin

get_the_category()won't work because you don't have categories, but custom taxonomies. Useget_the_terms()

$cur_terms = get_the_terms( get_the_ID(), 'super_grill_recipes_categories' );
if( is_array( $cur_terms ) ) {
  echo '<ul class="term-list">';
  foreach( $cur_terms as $cur_term ){
    echo '<li class="term-list__item">';
      echo '<a class="term-list__link" href="'. get_term_link( $cur_term->term_id, $cur_term->taxonomy ) .'">'. $cur_term->name .'</a>';
    echo '</li>';
  }
  echo '</ul>';
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question