H
H
hypero2018-06-24 17:20:03
WordPress
hypero, 2018-06-24 17:20:03

How to display custom post types by category?

Hello.
Registered a custom post type. It is possible to add categories for them.
They need to be sorted by category. There are, for example, more than 10 pages, you need to display posts by category on each page.
Page 1 - post from category #1
Page 2 - post from category #2
.. etc
Code function.php

register_post_type('services', array(
    'label'  => null,
    'labels' => array(
      'name'               => 'Услуги', 
      'singular_name'      => '____', 
      'add_new'            => 'Добавить', 
      'add_new_item'       => 'Добавление', 
      'edit_item'          => 'Редактирование', 
      'new_item'           => 'Новое', 
      'view_item'          => 'Смотреть', 
      'search_items'       => 'Искать', 
      'not_found'          => 'Не найдено', 
      'not_found_in_trash' => 'Не найдено в корзине', 
      'menu_name'          => 'Услуги', 
    ),
    'public'              => true,
    'show_ui'             => true, 
    'menu_icon'           => 'dashicons-controls-play', 
    'taxonomies'          => array('category'),
    'supports'            => array('title','editor', 'thumbnail', 'excerpt', 'permalink'),
  ) );
});

function getServices() {
  $args = array(
    'numberposts' => 4,
    'category' => 0,
    'orderby' => 'date',
    'order' => 'DESC',
    'include' => array(),
    'exclude' => array(),
    'meta_key' => '',
    'meta_value' => '',
    'post_type' => 'services',
    'suppress_filters' => true,
    );

  return get_posts($args);
}

Cycle code:
<?php foreach(getServices() as $post): ?>
<div class="content-page__block">
  <div class="content-page__image">
    <?php echo get_the_post_thumbnail( $page->ID, 'thumbnail', array('class' => 'content-page__img')); ?>
  </div>
  <div class="content-page__wrap">
    <h3 class="content-page__title"><?php echo $post->post_title; ?></h3>
    <p class="content-page__text">
      <?php echo $post->post_content; ?>
    </p>
  </div>
</div>
<?php endforeach; ?>

So far, all posts from all categories are displayed.
Help people.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Sobolev, 2018-06-25
@san_jorich

Try to add GetServices to receive variables and arrange a loop with parsing the array of parameters.
In general, it is more reliable to do all this through arbitrary fields and taxonomies, just now I am writing such a thing)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question