简体   繁体   English

如何在WP_Query(Wordpress)上包含缩略图

[英]how to include Thumbnail on WP_Query(Wordpress)

Hi guys I am working on a calendar in wordpress and my ajax work fine but I have a problem, on the query I need to include the post pic, this is my query 嗨,大家好,我正在wordpress的日历上工作,我的ajax工作正常,但是我有一个问题,在查询中我需要包括帖子图片,这是我的查询

function fnt_get_treatments() {

// what i recive from ajax
$n=(int)$_REQUEST['mm']+1;
$s = DateTime::createFromFormat('Y-m-d', $_REQUEST['yy'].'-'.$_REQUEST['mm'].'-0');
$u = DateTime::createFromFormat('Y-m-d', $_REQUEST['yy'].'-'.$n.'-0');
// query from the db
$query = new WP_Query(
    array(
        'post_type' => 'tribe_events',
        'post_status' => array(
            'publish'
        ),
        array(
            'after'     => $s,
            'before'    => $u,
            'inclusive' => true,
        ),
        'order'   => 'DESC',
    )
);

// return the data on json format

echo json_encode($query->posts);
die(); // end the script

} }

the result is fine, is exactly what i want, but i dont know how to include the post picture cause on the json each object have everything but not the pic url, can anyone help me, i am sure thatś simple but i am new 结果很好,正是我想要的,但是我不知道如何在json上包含帖子图片,因为每个对象都有所有内容,但没有图片网址,任何人都可以帮助我,我敢肯定那很简单,但我是新手

First of all a little modification to your query. 首先,对您的查询进行一些修改。 You have mentioned the 'date_query' array but have forgotten to set the key date_query 您已经提到了'date_query'数组,但是忘记设置键date_query

$query = new WP_Query(
    array(
        'post_type' => 'tribe_events',
        'post_status' => 'publish',
        'date_query' => array(
            'after'     => $s,
            'before'    => $u,
            'inclusive' => true,
        ),
        'order'   => 'DESC',
    )
);

And after that instead of doing anything for getting the thumb and slowing down the query you can simply use thumbnail function inside the loop, and you can get any size as per your requirement. 之后,您无需执行任何操作即可使拇指变慢并降低查询速度,而只需在循环内使用缩略图功能,即可根据需要获得任意大小。

Copy the below code to your loop if you want to display the default size, 如果要显示默认大小,请将以下代码复制到循环中,

// check if the post has a Post Thumbnail assigned to it.
if ( has_post_thumbnail() ) {
    the_post_thumbnail();
} 

And if custom size is needed, then you can use the following, 如果需要自定义尺寸,则可以使用以下内容,

// without parameter -> Post Thumbnail (as set by theme using set_post_thumbnail_size())
the_post_thumbnail();

the_post_thumbnail('thumbnail');       // Thumbnail (default 150px x 150px max)
the_post_thumbnail('medium');          // Medium resolution (default 300px x 300px max)
the_post_thumbnail('medium_large');    // Medium Large resolution (default 768px x 0px max)
the_post_thumbnail('large');           // Large resolution (default 1024px x 1024px max)
the_post_thumbnail('full');            // Original image resolution (unmodified)

the_post_thumbnail( array(100,100) );  // Other resolutions

Page Reference - Post Thumbnail 页面参考- 发布缩略图

Hope this once helps ;) 希望这会有所帮助;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM