I have custom taxonomy, how can I add new element to taxonomy edit page in admin panel. For example, I have album as taxonomy and song as post type, my issue is that I want to show all songs that belong to current album when I edit album. Thanks.
I have custom taxonomy, how can I add new element to taxonomy edit page in admin panel. For example, I have album as taxonomy and song as post type, my issue is that I want to show all songs that belong to current album when I edit album. Thanks.
Share Improve this question asked May 19, 2017 at 1:09 Huy NguyenHuy Nguyen 134 bronze badges1 Answer
Reset to default 0Check that below code block, hope it will help you. Put it in your plugin or in theme's functions.php
file and it'll work. I tested it with category
taxonomy.
The code block-
// {$taxonomy}_edit_form_fields you should use
add_action( 'album_edit_form_fields', 'the_dramatist_add_custom_fields_to_taxonomy_edit_page', 10, 2 );
/**
* The function for rendering posts related to the term.
*
* @param object $tag Current taxonomy term object.
* @param string $taxonomy Current taxonomy slug.
*/
function the_dramatist_add_custom_fields_to_taxonomy_edit_page( $tag, $taxonomy) {
$songs_query = new WP_Query(
array(
'post_type' => 'post',
'post_per_page' => -1,
array(
'taxonomy' => 'album',
'field' => 'slug',
'terms' => $taxonomy // slug of the term
)
)
);
?>
<tr class="form-field term-description-wrap">
<th scope="row"><label for="description">Songs</label></th>
<td>
<table>
<?php
if( $songs_query->have_posts() ):
while( $songs_query->have_posts() ): $songs_query->the_post();
?>
<tr>
<td>
<h3><?php the_title()?></h3>
<p><?php the_content()?></p>
</td>
</tr>
<?php
endwhile;
endif;
?>
</table>
</td>
</tr>
<?php
wp_reset_postdata();
}