T
T
TosterUserName2018-05-26 22:26:53
WordPress
TosterUserName, 2018-05-26 22:26:53

How to display categories on post pages in order?

I found the breadcrumb code (below), where for the post pages the names of the categories to which the post belongs are displayed separated by commas. But I will attach schema markup to them, so it is necessary that the parent category (if any) is displayed first, and then the child with separate links. I suspect that it should be similar to how it is implemented for category pages in the same code, but I don’t know how to adapt it correctly to post pages.

function the_breadcrumb(){
global $post;
if(!is_home()){ 
   echo '<a href="'.site_url().'">Главная</a> &amp;raquo; ';
  if(is_single()){ // записи
  the_category(', ');
  echo " &amp;raquo; ";
  the_title();
  }
  elseif (is_page()) { // страницы
    if ($post->post_parent ) {
      $parent_id  = $post->post_parent;
      $breadcrumbs = array();
      while ($parent_id) {
        $page = get_page($parent_id);
        $breadcrumbs[] = '<a href="' . get_permalink($page->ID) . '">' . get_the_title($page->ID) . '</a>';
        $parent_id  = $page->post_parent;
      }
      $breadcrumbs = array_reverse($breadcrumbs);
      foreach ($breadcrumbs as $crumb) echo $crumb . ' &amp;raquo; ';
    }
    echo the_title();
  }
  elseif (is_category()) { // категории
    global $wp_query;
    $obj_cat = $wp_query->get_queried_object();
    $current_cat = $obj_cat->term_id;
    $current_cat = get_category($current_cat);
    $parent_cat = get_category($current_cat->parent);
    if ($current_cat->parent != 0) 
      echo(get_category_parents($parent_cat, TRUE, ' &amp;raquo; '));
    single_cat_title();
  }
  elseif (is_search()) { // страницы поиска
    echo 'Результаты поиска для "' . get_search_query() . '"';
  }
  elseif (is_tag()) { // теги (метки)
    echo single_tag_title('', false);
  }
  elseif (is_day()) { // архивы (по дням)
    echo '<a href="' . get_year_link(get_the_time('Y')) . '">' . get_the_time('Y') . '</a> &amp;raquo; ';
    echo '<a href="' . get_month_link(get_the_time('Y'),get_the_time('m')) . '">' . get_the_time('F') . '</a> &amp;raquo; ';
    echo get_the_time('d');
  }
  elseif (is_month()) { // архивы (по месяцам)
    echo '<a href="' . get_year_link(get_the_time('Y')) . '">' . get_the_time('Y') . '</a> &amp;raquo; ';
    echo get_the_time('F');
  }
  elseif (is_year()) { // архивы (по годам)
    echo get_the_time('Y');
  }
  elseif (is_author()) { // авторы
    global $author;
    $userdata = get_userdata($author);
    echo 'Опубликовал(а) ' . $userdata->display_name;
  } elseif (is_404()) { // если страницы не существует
    echo 'Ошибка 404';
  }
 
  if (get_query_var('paged')) // номер текущей страницы
    echo ' (' . get_query_var('paged').'-я страница)';
 
} else { // главная
   $pageNum=(get_query_var('paged')) ? get_query_var('paged') : 1;
   if($pageNum>1)
      echo '<a href="'.site_url().'">Главная</a> &amp;raquo; '.$pageNum.'-я страница';
   else
      echo 'Вы находитесь на главной странице';
}
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question