获取自定义帖子类型缩略图

时间:2017-04-19 作者:Ali Dayan

我需要获取自定义帖子类型缩略图的URL,我的自定义帖子类型名称是slider。我已经定义了函数。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;
}
我的代码是:

<!-- 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 } ?>
问题是什么?有人能帮我吗?谢谢你的帮助。

3 个回复
最合适的回答,由SO网友:BlueSuiter 整理而成

请使用以下内容更新while循环:它将为您打印缩略图url

**获取后参数**

<?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
}
?>

SO网友:Dev

您应该使用

\'post_type\' => array( \'slider\' ),
这是您应该用来显示名为slider not的帖子类型中的内容的内容:

\'post_type\' => \'post\',

SO网友:habib

以下解决方案对我有效:

$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

相关推荐