WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Get the thumbnails url of my custom post type (3 posts)

  1. zqzq
    Member
    Posted 2 years ago #

    WP config
    - Version : 3.4.1
    - PHP/MySQL Version : 5.3
    - Theme : My own
    - Plugins : None
    - Website URL : localhost

    Hello,

    I try to use the thumbnails url of my custom post types "Features" to integrate them into a custom slider, but I encounter an "HIERARCHY_REQUEST_ERR: DOM Exception 3" error.

    <div id="features_content">
    <?php $loop = new WP_Query( array( 'post_type' => 'features', 'posts_per_page' => 99 ) ); ?>
    <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
    <div class="slider_wrap">
    <div data-src="<?php echo get_post_thumbnail_url($post->ID); ?>">
    <div class="slider_caption"><?php the_title ?></div>
    <?php the_excerpt(); ?>
    </div>
    <?php endwhile; ?>
    </div>

    functions.php code :

    <?php
    function get_post_thumbnail_url($postid, $imagesize = 'full'){
    $post_thumbnail_id = get_post_thumbnail_id($postid);
    $image_array = wp_get_attachment_image_src($post_thumbnail_id, $imagesize);
    return $image_array[0];
    }
    ?>

    The url of the thumbnails is ok but nothings displayed.

    How can I get an url that can be used in the data-src of my div ?

    Thanks

  2. Steven Jones
    Member
    Posted 2 years ago #

    I would use the built in WordPress function rather than your own.

    <?php
    $url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID) );
    echo $url['0'];
    ?>
  3. zqzq
    Member
    Posted 2 years ago #

    Ok, Ive already tried this one but it didnt work.
    Tested again, the problem was the loop call.

    Here the working code :

    <div id="features_content">
    <div class="slider_wrap">
    <?php $loop = new WP_Query( array( 'post_type' => 'features', 'posts_per_page' => 99 ) ); ?>
    <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
    <div data-src="<?php echo get_post_thumbnail_url($post->ID); ?>">
    <div class="slider_caption"><?php the_title ?></div>
    <?php the_excerpt(); ?>
    </div>
    <?php endwhile; ?>
    </div>

    Thanks to redirected me in the right way Steven.

Topic Closed

This topic has been closed to new replies.

About this Topic