Question

I'm getting some really weird results trying to pull all the featured image URL's for a custom post type.

Only the first URL will be pulled all others show blank. I have checked the post ID and it does have a value. Also the thumbnail ID is pulling correctly. If I place that value into the function hardcoded it will return the proper URL this is in a theme page. Here is the code:

global $post;
                $type = 'slider';
                $args=array(
                  'post_type' => $type,
                  'post_status' => 'publish',
                  'posts_per_page' => 5 );

                $slider_posts = null;
                $slider_posts = new WP_Query($args);
                while ($slider_posts->have_posts()) {
                    $slider_posts->the_post(); 
                    $post_id = $post->ID;
                    $thumbnail_id = intval(get_post_thumbnail_id( $post_id ));

                    if ( has_post_thumbnail()) {
                        $url = wp_get_attachment_url( $thumbnail_id );
                        ?>

                    <div class="slide">
                        <img class="slider_images" src="<?php echo $url; ?>" width="587" height="330" />
                        <div>
                            <h4><?php the_title(); ?></h4>
                            <p id="spacer">&nbsp;</p>
                            <p><?php the_excerpt(); ?></p>
                            <p><a href="<?php the_permalink(); ?>">Read More...</a></p>
                        </div>
                    </div>
                    <?php
                    }
                }
                wp_reset_query();

                ?> 
            </div>

The URL to see what is happening is here: http://template.seniorshomecaregivers.com/

I'm using the URL in the slider.

As you can see it only pulls the first URL after that they all return blank.

Thanks in advance for your help.

Was it helpful?

Solution

For those who want a solution this code will store the featured image url in the post meta when the custom post is added or updated. You then only need to grab the url from post meta in a loop by post id and meta name. This eliminates the myrid of calls to get the thumbnail id and then the url.

function add_slider_posttype() {
$labels = array(
    'name'               => _x( 'Slider', 'post type general name' ),
    'singular_name'      => _x( 'Slider', 'post type singular name' ),
    'add_new'            => _x( 'Add New', 'book' ),
    'add_new_item'       => __( 'Add New Slide' ),
    'edit_item'          => __( 'Edit Slide' ),
    'new_item'           => __( 'New Slide' ),
    'all_items'          => __( 'All Slides' ),
    'view_item'          => __( 'View Slides' ),
    'search_items'       => __( 'Search Slides' ),
    'not_found'          => __( 'No slides found' ),
    'not_found_in_trash' => __( 'No slides found in the Trash' ), 
    'parent_item_colon'  => '',
    'menu_name'          => 'Slider'
);

$args = array(
    'labels'        => $labels,
    'description'   => 'Holds our slides and slider specific data',
    'public'        => true,
    'menu_position' => 25,
    'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
    'has_archive'   => true,
);

register_post_type( 'slider', $args );  

 }
add_action( 'init', 'add_slider_posttype', 0 );

function save_slider_meta($post_id) {

if(get_post_type( $post_id ) == "slider"){
    $thumbnail_id = get_post_thumbnail_id( $post_id );
    $url = wp_get_attachment_url( $thumbnail_id );

    if (!empty($url)){
        update_post_meta($post_id, 'slider_img_url', $url);
    }
}

}

add_action( 'save_post', 'save_slider_meta');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top