Inside the loop:
echo implode (', ', get_post_meta($post->ID, 'typeofpub', true) ? : array() );
This will print all ‘typeofpub’ comma separed.
If you want to do something more advanced wheen you print, e.g. wrap it in some html, a good way is create your own function in functions.php, something lie:
function typeofpub( $post_id = 0 ) {
if ( empty( $post_id ) ) {
global $post;
$post_id = isset( $post->ID ) ? $post->ID : 0;
}
if ( empty( $post_id ) ) return;
$types = (array) get_post_meta($post->ID, 'typeofpub', true);
if ( ! empty($types) ) {
echo '<ul class="types_container">';
foreach( $types as $type) {
echo '<li class="type-' . sanitize_title_with_dashes($type) . '">';
echo ucwords($type);
echo '</li>';
}
echo '</ul>';
}
}
Then in the loop simply use it in like so: typeofpub();
You can change the html output as you want, in my example an unordered list is printed, where the ul
tag has the class types_container
and every li
item has the class type-{$type}
e.g. for the type ‘Sport’ is outputted something like:
<li class="type-sport">Sport</li>
The function can be used also outside the loop, passing a post id as param, e.g.
typeofpub( 10 );