我的短代码乱七八糟,他改变了顺序。这是我的代码:
add_shortcode( \'recent-portfolio\', \'brechting_recent_portfolio\' );
function brechting_recent_portfolio( $atts ) {
extract( shortcode_atts( array(
\'numbers\' => \'5\',
), $atts ) );
$rposts = new WP_Query(
array( \'post_type\' => \'portfolio\', \'posts_per_page\' => $numbers, \'orderby\' => \'date\' )
);
if ( $rposts->have_posts() ) {
$html = \'<div class="recent-portfolio grid" data-masonry="{ "columnWidth": 200, "itemSelector": "img" }">\';
while( $rposts->have_posts() ) {
$rposts->the_post();
if ( has_post_thumbnail()) {
$html .= sprintf(
\'<div class="grid-item"><a href="%s" title="">%s</a></div>\',
the_post_thumbnail_url( \'full\' ),
the_title_attribute(),
//get_the_title()
the_post_thumbnail()
);
}}
$html .= \'</div>\';
}
wp_reset_query();
return $html;
}
这是输出:
<div class="entry-content2" data-masonry="{ " columnwidth":="" 200,="" "itemselector":="" "img"="" }"="">
http://localhost:8888/wp-content/uploads/2017/08/geboortekaart-Bart.pngGeboortekaartje Bart<img src="http://localhost:8888/wp-content/uploads/2017/08/geboortekaart-Bart.png" class="attachment-post-thumbnail size-post-thumbnail wp-post-image" alt="" srcset="http://localhost:8888/wp-content/uploads/2017/08/geboortekaart-Bart.png 788w, http://localhost:8888/wp-content/uploads/2017/08/geboortekaart-Bart-300x171.png 300w, http://localhost:8888/wp-content/uploads/2017/08/geboortekaart-Bart-768x439.png 768w" sizes="100vw" width="788" height="450">http://localhost:8888/wp-content/uploads/2017/08/lejo.pngLejo<img src="http://localhost:8888/wp-content/uploads/2017/08/lejo.png" class="attachment-post-thumbnail size-post-thumbnail wp-post-image" alt="" srcset="http://localhost:8888/wp-content/uploads/2017/08/lejo.png 788w, http://localhost:8888/wp-content/uploads/2017/08/lejo-300x171.png 300w, http://localhost:8888/wp-content/uploads/2017/08/lejo-768x439.png 768w" sizes="100vw" width="788" height="450">
<div class="recent-portfolio grid" data-masonry="{ " columnwidth":="" 200,="" "itemselector":="" "img"="" }"="">
<div class="grid-item">
<a href="" title=""></a></div><div class="grid-item"><a href="" title=""></a>
</div>
</div>
</div>
如果我使用
get_title()
它也可以工作,但如果我使用基于特征图像的功能,它会改变顺序。。。
谁能帮帮我吗?
最合适的回答,由SO网友:Johansson 整理而成
the_post_thumbnail_url()
将回显URL。您应该使用具有get_the_...
在其名称的开头,因为函数以the_...
通常会回显内容。
那么,你的sprintf
应使用get_the_post_thumbnail_url()
像这样:
$html .= sprintf(
\'<div class="grid-item"><a href="%s" title="">%s</a></div>\',
get_the_post_thumbnail_url( get_the_ID(), \'full\' ),
the_title_attribute( \'echo=0\' ),
//get_the_title()
//the_post_thumbnail()
);
然而,在这种情况下
the_title_attribute()
没有
get_the_...
据我所知的版本。但是可以通过将其作为参数传递给函数来禁用echo。
此外,使用wp_reset_postdata();
而不是wp_reset_query()
. 使用时应使用后者query_posts()
(您不应该使用!)。