Menu

How to display the title and thumbnail of child Custom Post Types in WordPress

How to display the title and thumbnail of child Custom Post Types on the parent custom post type in WordPress:

<?php
$pageChildren = get_pages('child_of='.$post->ID."&echo=0&post_type=custompostypename");
if ( $pageChildren ) {
foreach ( $pageChildren as $pageChild ) {
echo '<a href="' . get_permalink($pageChild->ID) . '">';
$childimage = wp_get_attachment_image_src(get_post_thumbnail_id($pageChild->ID), 'thumbnail_size');
$childimagealttext = get_post_meta(get_post_thumbnail_id($pageChild->ID), '_wp_attachment_image_alt', true);
echo '<img src="'. $childimage['0'] . '" alt="' . $childimagealttext . '"/></a>';
echo '<p><a href="' . get_permalink($pageChild->ID) . '">'. $pageChild->post_title.'</a></p>';
}
}
view raw gistfile1.txt hosted with ❤ by GitHub