I'm building a theme that needs some cross-information(?) between custom post types.
I have a metabox to select custom post types and display them inside the single page of another custom post type. So far things work 50%...
Right now I am using this code to display their thumbnail (but I will require their title and own meta):
$guests = get_post_meta($post->ID, 'cl_guest', true);
$guest_id = get_post_meta(get_the_ID(), 'cl_guest', true);
$guest_post = get_post( $guest_id[0] );
foreach($guest_id as $guest) {
echo get_the_post_thumbnail($guest_post->ID, 'thumbnail', array(
'class' => "img-responsive img-circle",
'alt' => trim(strip_tags( $guest_post->post_title )),
'title' => trim(strip_tags( $guest_post->post_title ))
));
}
It displays the correct number of posts, but it repeats the first item (ordered by ascending id).
Any help is appreciated! <3
I'm building a theme that needs some cross-information(?) between custom post types.
I have a metabox to select custom post types and display them inside the single page of another custom post type. So far things work 50%...
Right now I am using this code to display their thumbnail (but I will require their title and own meta):
$guests = get_post_meta($post->ID, 'cl_guest', true);
$guest_id = get_post_meta(get_the_ID(), 'cl_guest', true);
$guest_post = get_post( $guest_id[0] );
foreach($guest_id as $guest) {
echo get_the_post_thumbnail($guest_post->ID, 'thumbnail', array(
'class' => "img-responsive img-circle",
'alt' => trim(strip_tags( $guest_post->post_title )),
'title' => trim(strip_tags( $guest_post->post_title ))
));
}
It displays the correct number of posts, but it repeats the first item (ordered by ascending id).
Any help is appreciated! <3
Share Improve this question asked Aug 21, 2019 at 19:13 artist learning to codeartist learning to code 3311 gold badge5 silver badges18 bronze badges1 Answer
Reset to default 1Based on the code you've posted I assume cl_guest
post meta contains an array of post IDs. Building upon this assumption, you could do something like this.
1) Add these helper functions to your theme's funtions.php file
// custom template tag to render guests
function render_guests( array $guests ) {
if ( $guests ) {
// you could use foreach loop instead of array_walk
array_walk( $guests, 'render_guest' );
}
}
// Helper function to get guest ids
function get_guest_ids( int $id ) {
$meta = get_post_meta( $id, 'cl_guest', true);
return ( $meta && is_array( $meta ) ) ? array_map( 'intval', $meta ) : array();
}
// helper function to render single guest
function render_guest( int $id ) {
// get meta if needed
$meta = get_guest_meta( $id );
$title = get_the_title( $id );
$thumbnail = get_guest_thumbnail( $id, $title, $title );
// modify output as needed
echo '<div class="guest">' . $thumbnail . $title . '</div>';
}
// helper function
function get_guest_meta( int $id ) {
return get_post_meta( $id, 'some_meta_key', true );
}
// helper function
function get_guest_thumbnail( int $id, string $alt = '', string $title = '' ) {
return get_the_post_thumbnail(
$id,
'thumbnail',
array(
'class' => "img-responsive img-circle",
'alt' => esc_attr( $alt ),
'title' => esc_attr( $title )
)
);
}
2) add the custom tempalte tag to your post template where you want to display the guests
<?php render_guests( get_guest_ids( get_the_ID() ) ); ?>
P.S. You might wonder why I've split the code to multiple functions. The reason is that I'm just practicing a new coding style where each function is supposed to do one thing.
EDIT 1 Here's how you could render images and title+meta separately. First couple new helper functions.
function render_guest_images( array $ids ) {
if ( $ids ) {
array_walk( $ids, 'render_guest_image' );
}
}
function render_guest_image( int $id ) {
$title = get_the_title( $id );
echo get_guest_thumbnail( $id, $title, $title );
}
function render_guest_infos( array $ids ) {
if ( $ids ) {
array_walk( $ids, 'render_guest_info' );
}
}
function render_guest_info( int $id ) {
$title = get_the_title( $id );
$meta = get_guest_meta( $id ); // this could either return data that you render here or ready-to-use html that you just concatenate to the echo below
echo '<div class="guest-info">' . $title . '</div>';
}
// add this before you add the rendering functions on your post template
$guest_ids = get_guest_ids( get_the_ID() );
// somewhere on your post template
render_guest_images( $guest_ids );
// somewhere else on your post template
render_guest_infos( $guest_ids );
EDIT 2
without separate helper and render functions
// before any loops
$guest_ids = get_post_meta( get_the_id(), 'cl_guest', true);
$guest_ids = ( $guest_ids && is_array( $guest_ids ) ) ? array_map( 'intval', $guest_ids ) : array();
$guest_posts = array();
if ( $guest_ids ) {
foreach ( $guest_ids as $guest_id ) {
if ( $post = get_post( $guest_id ) ) {
$guest_posts[] = $post;
}
}
}
// titles only
if ( $guest_posts ) {
foreach( $guest_posts as $guest_post ) {
echo esc_html( $guest_post->post_title );
}
}
// thumbnails
if ( $guest_posts ) {
foreach( $guest_posts as $guest_post ) {
echo get_the_post_thumbnail(
$guest_post->ID,
'thumbnail',
array(
'class' => "img-responsive img-circle",
'alt' => esc_attr( $guest_post->post_title ),
'title' => esc_attr( $guest_post->post_title )
)
);
}
}
// meta only
if ( $guest_posts ) {
foreach( $guest_posts as $guest_post ) {
$meta = get_post_meta( $guest_post->ID, 'some_meta_key', true );
// do something with meta
}
}