menu

arrow_back How do I create a hierarchy with a custom record type?

by
1 vote
I have two pages on my website: photographers and videographers. Each of these pages should display previews of entries with a list of cities. And so when you go to the city record itself in the breadcrumbs should be the following - "Home - Photographers - Moscow.
In this case, also on the site there is a blog.
Initially I thought to make an arbitrary record type for cities, but I do not understand how then indicate that they relate to the page photographers, for example, that the bread crumbs were displayed as indicated above. It is also possible to make a category Photographers and already at it records to output...but very much did not want to mix these records with the records of the blog.
Can you please tell me if there are more options how to do it right?

1 Answer

by
0 votes
You can do the following. Create two post-types photo and video. For each a different taxonomy photo_cat and video_cat. Now build two arrays, one with taxonomies and one with a list of cities:

$termsArray = array( 'photo_cat ', 'video_cat' );

$startArray = array(
'mos' => 'Москва',
'len' => 'Санкт-Петербург',
'nsk' => 'Новосибирск',
'ekb' => 'Екатеринбург',
);
Now you can cycle to create them
foreach ( $startArray as $key => $startItem ) {
foreach ( $termsArray as $termsItem ) {
// Проверяем, что такого термина еще не существует
$termID = get_term_by( 'slug', $key, $termsItem )->term_id ?: '0';
if ( $termID == '0' ) {
$termArgs = array(
'cat_ID' => $termID,
'cat_name' => $startItem,
'category_description' => '',
'category_nicename' => $key,
'category_parent' => '',
'taxonomy' => $termsItem
);

$termID = wp_insert_category( $termArgs );

// пишем ошибку/ успех
if( is_wp_error($termID) ) {
var_dump('Ошибка инсерта термина ' . $startItem . ' таксономии ' . $termsItem . ': ' . $termID->get_error_message())
} else {
var_dump('Термин ' . $startItem . ' таксономии ' . $termsItem . ' опубликован удачно!');
}

} // end if $termID == '0'
} // end foreach $termsArray
} // end foreach $startArray
The advantage of this method is that when you are on a page with one taxonomy you can get the posts of the other, because you know the current slang of the taxonomy To make a shared archive 'Photographers', specify the 'has_archive' argument. To make the taxonomy in breadcrumbs, you need to write the rules for link rewrite in the 'rewrite' argument

2 Comments

Alexa_KyKi , 1. Can
2. Make a common archive of custom posts with photographers by setting the 'has_archive' argument
Cities will definitely be categories, because if I make an array with a list of cities in the code, then the user will not be able to dynamically add cities...
As for the rest, I don't really understand how you propose to display entries that would belong to the Photographers or Videographers page, that would be displayed in crumbs. Make the Photographers page an archive?