на моем сайте есть 3 пользовательских типа сообщений, каждый из которых имеет уникальную пользовательскую таксономию для этого типа сообщений.
Я хочу отобразить эти таксономии на одной странице сообщений и только таксономии, связанные с этим типом сообщения, а не с другими, без категорий и тегов.
установка _builtin на false приведет к трюку для категорий и тегов, но я не могу понять, как правильно отображать эти пользовательские таксономии, а на моей странице сообщений отображаются все пользовательские таксономии из всех типов сообщений.
Я не знаю php отлично и пытался найти код с нескольких сайтов, включая этот сайт, и это мой код:
// get taxonomies terms links function custom_taxonomies_terms_links() { global $post, $post_id; // get post by post id $post = &get_post($post->ID); // get post type by post $post_type = $post->post_type; // get post type taxonomies $args = array( 'public' => true, '_builtin' => false ); $output = 'names'; // or objects $operator = 'and'; // 'and' or 'or' $taxonomies = get_taxonomies( $args, $output, $operator ); $out = "<ul>"; foreach ($taxonomies as $taxonomy) { $out .= "<li>".$taxonomy.": "; // get the terms related to post $terms = get_the_terms( $post->ID, $taxonomy ); if ( !empty( $terms ) ) { foreach ( $terms as $term ) $out .= '<a href="' .get_term_link($term->slug, $taxonomy) .'">'.$term->name.'</a> '; } $out .= "</li>"; } $out .= "</ul>"; return $out; }
любая помощь будет оценена. tnx
Попробовать код ниже
function custom_taxonomies_terms_links() { global $post, $postid; $custom_taxonomies = get_post_taxonomies( $post ); if($custom_taxonomies) { foreach ($custom_taxonomies as $custom_taxonomy) { // get post type taxonomies $args = array( 'public' => true, '_builtin' => false ); $args['name']=$custom_taxonomy; $output = 'names'; // or objects $operator = 'and'; // 'and' or 'or' $taxonomies = get_taxonomies( $args, $output, $operator ); $out = "<ul>"; foreach ($taxonomies as $taxonomy) { $out .= "<li>".$taxonomy.": "; // get the terms related to post $terms = get_the_terms( $post->ID, $taxonomy ); if ( !empty( $terms ) ) { foreach ( $terms as $term ) $out .= '<a href="' .get_term_link($term->slug, $taxonomy) .'">'.$term->name.'</a> '; } $out .= "</li>"; } $out .= "</ul>"; echo $out; } } }
вы можете изменить размещение HTML-тегов в вышеуказанном коде согласно вашему требованию. Надеюсь, что это поможет вам!