我在这方面有点新手,但我正在尝试创建一个wordpress功能,用于显示带有短代码的页面上最新4个帖子的片段。这个想法是有4个面板,每个面板显示帖子标题,特色图像和摘录。我可以将所有这些元素显示在页面上。但是,图像始终插入到其余内容之上。该函数的代码是:
function recent_posts_function($atts){
extract(shortcode_atts(array(
'posts' => 1,
), $atts));
$return_string = '<h2>Recent Projects</h2><div>';
query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));
if (have_posts()) :
while (have_posts()) : the_post();
$return_string .= '<div class="portfolio-posts"><h4><a href="'.get_permalink().'">'.get_the_title().'</a></h4>'.get_the_excerpt().'<a href="'.get_permalink().'" title="'.get_the_title().'">'.the_post_thumbnail('medium').'</a></div>';
endwhile;
endif;
$return_string .= '</div>';
wp_reset_query();
return $return_string;
}
这次返回的代码首先放置图像,然后是其余的内容:
<div class="entry-content">
<img src="http://URL.com/image1.jpg" class="attachment-medium wp-post-image" alt="alt text 1">
<img src="http://URL.com/image2.jpg" class="attachment-medium wp-post-image" alt="alt text 2">
<h2>Recent Posts</h2>
<div>
<div class="portfolio-posts">
<h4><a href="http://www.URL.com/category/post-title-1/">Post Title 1</a></h4>
<p class="lead">excerpt text from post goes here</p>
<p class="more-link-p"><a class="btn btn-danger" href="http://www.URL.com/category/post-title-1/">Read more →</a></p>
<a href="http://www.URL.com/category/post-title-1/" title="Post Title 1"></a>
</div>
<div class="portfolio-posts">
<h4><a href="http://www.URL.com/category/post-title-2/">Post Title 2</a></h4>
<p class="lead">excerpt text from post 2 goes here</p>
<p class="more-link-p"><a class="btn btn-danger" href="http://www.URL.com/category/post-title-2/">Read more →</a></p>
<a href="http://www.URL.com/category/post-title-2/" title="Post Title 2"></a>
</div>
</div>
</div>
问题是如何让图像显示在标题下方和摘录文本之前?非常感谢任何帮助!
谢谢...
答案 0 :(得分:0)
所以,我记得有一段时间我自己也遇到过这个问题。结果the_post_thumbnail
是所要求的“转储”。您需要的是可以回显或存储在变量中供以后使用的内容。
解决方法是get_the_post_thumbnail
- 请查看链接
通过这种方式,您可以echo
将其存储或存储在变量中供以后使用,而不是像某种粗暴的那样被严厉地抛弃。
快乐的编码!