1

I need to get the URL for Custom post type Thumbnail, My custom post type name is slider. I have defined on functions.php:

/* Custom post type */
add_action('init', 'slider_register');

function slider_register() {

    $labels = array(
        'name' => __('Slider', 'post type general name'),
        'singular_name' => __('Slider Item', 'post type singular name'),
        'add_new' => __('Add New', 'portfolio item'),
        'add_new_item' => __('Add New Slider Item'),
        'edit_item' => __('Edit Slider Item'),
        'new_item' => __('New Slider Item'),
        'view_item' => __('View Slider Item'),
        'search_items' => __('Search Slider'),
        'not_found' =>  __('Nothing found'),
        'not_found_in_trash' => __('Nothing found in Trash'),
        'parent_item_colon' => ''
    );

    $args = array(
        'labels' => $labels,
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'query_var' => true,
        'menu_icon' => get_stylesheet_directory_uri() . '/image/slider.png',
        'rewrite' => true,
        'capability_type' => 'post',
        'hierarchical' => false,
        'menu_position' => null,
        'supports' => array('title','editor','thumbnail')
        );

    register_post_type( 'slider' , $args );
    flush_rewrite_rules();
}

add_filter("manage_edit-slider_columns", "slider_edit_columns");

function slider_edit_columns($columns){
    $columns = array(
        "cb" => "<input type='checkbox' />;",
        "title" => "Portfolio Title",
    );

    return $columns;
}

My code is:

<!-- Slider -->
<?php
    $args = array(
        'post_type'=> 'post',
        'post_status' => 'publish',
        'order' => 'DESC',
        'tax_query' => array(
            array(
                'post-type' => array('post', 'slider')
            )
        )
    );
    $query = new WP_Query($args);
    if( $query -> have_posts() ) {
?>
    <div id="slider_area">
        <div class="slider">
            <a href='#' class="prev"><i class="fa fa-angle-double-left"></i></a>
            <a href='#' class="next"><i class="fa fa-angle-double-right"></i></a>
            <ul class="slider_list">
                <?php
                    while($query->have_posts()) : $query->the_post();
                        if(has_post_thumbnail()) { ?>
                            <li>
                                <?php the_post_thumbnail(); ?>
                            </li>
                        <?php }
                         elseif($thumbnail = get_post_meta($post->ID, 'image', true)) { ?>
                             <li>
                                 <img src="<?php echo $thumbnail; ?>" alt="<?php the_title(); ?>" title="<?php the_title(); ?>" />
                             </li>
                     <?php } endwhile;
                 ?>
            </ul>
        </div>
    </div>
<?php } ?>

What is the problem? Could anyone help me? Thank you for your helps.

9
  • Update your query while checking for thumbnail '$query->has_post_thumbnail();'
    – BlueSuiter
    Apr 19, 2017 at 9:17
  • still can not see image :/ I have also change name slider to expro_slider it a little make sence but cannot see image.
    – Ali Dayan
    Apr 19, 2017 at 9:35
  • Use get_the_post_thumbnail($post->ID) instead of the_post_thumbnail()
    – BlueSuiter
    Apr 19, 2017 at 9:40
  • Still the same :/
    – Ali Dayan
    Apr 19, 2017 at 9:43
  • Please see updated answer
    – BlueSuiter
    Apr 19, 2017 at 9:47

3 Answers 3

1

Please update the while loop with this: It will print the thumbnail url for you

** POST FETCHING ARGUMENTS **

<?php 
/**** Slider Call Function ****/
function callTheSlider()
{
    $args = array('post_type'=> 'expro_slider', 'post_status' => 'publish', 'order' => 'DESC');
    ?>
    <ul>
    <?php
    wp_reset_query();
    $query = new WP_Query($args);
    while($query->have_posts()) : $query->the_post();
            if(has_post_thumbnail()) {  ?>
            <li>
                <?php the_post_thumbnail(); ?>
            </li>
        <?php }
        elseif($thumbnail = get_post_meta($post->ID, 'image', true)) { echo 12323; ?>
            <li>
                <img src="<?php echo $thumbnail; ?>" alt="<?php the_title(); ?>" title="<?php the_title(); ?>" />
            </li>
        <?php } endwhile;
        ?>
        </ul>
    <?php
}
?>
0
1

You should use

'post_type' => array( 'slider' ),

This is what you should use to display content from a post type named slider not:

'post_type' => 'post',
3
  • I did but there is no image stil I dont know why. I have added featured image already.
    – Ali Dayan
    Apr 19, 2017 at 10:18
  • This is my first post remmyhome.com/expro_slider/uzun-omurlu and this is second remmyhome.com/expro_slider/cam-sagliktir as you can see I have images on single.php but not on index.php
    – Ali Dayan
    Apr 19, 2017 at 10:24
  • You need to code the image into your index.php or home.php or front-page.php or archive.php whatever template you are using.
    – Dev
    Apr 20, 2017 at 4:59
0

The following solution worked for me:

$q=new WP_Query(array('post_type'=>'slider', 'post_status' => 'publish', 'order' => 'DESC'));
if($q->have_posts()):
while($q->have_posts()):
$q->the_post(); 
      echo get_the_post_thumbnail_url(get_the_ID(),'full');
endwhile;
endif;

Reference

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.