[英]how to display posts image from wordpress blog in html/php page
Below is my code to display recent posts from blog in html/php page下面是我的代码,用于在 html/php 页面中显示博客的最新帖子
<div class="row">
<?php
include('blog/wp-load.php');
$recent_posts = wp_get_recent_posts(array(
'numberposts' => 3,
'post_type' => 'post',
'post_status' => 'publish'
));
foreach($recent_posts as $post) {
echo '<div class="col-lg-4">';
echo '<div class="blog-entry">';
echo '<a href="', get_permalink($post['ID']), '" style="background-image: url(images/BlogImg.png);"></a>';
echo '<div class="text">';
echo '<div class="meta">';
echo '<div><span class="fa fa-calendar-alt"></span>', $post['post_date'], '</a></div>';
echo '<div><span class="fa fa-user"></span>', $post['post_author'], '</div>';
echo '<div><span class="fa fa-comments meta-chat"></span>', $post['comment_count'], '</div>';
echo '</div>';
echo '<div class="desc">';
echo '<h3 class="heading"><a href="', get_permalink($post['ID']), '">', $post['post_title'], '</a></h3>';
echo '</div>';
echo '</div>';
echo '</div>';
echo '</div>';
}
?>
</div>
How can I display post image using background-image: url(images/BlogImg.png);
如何使用
background-image: url(images/BlogImg.png);
In place of images/BlogImg.png
I want post image url of respective blog post.代替
images/BlogImg.png
我想要相应博客文章的帖子图像 url。
Can anyone please tell me how can I achieve this?谁能告诉我如何实现这一目标?
Depending how you have the image stored, if this is a featured image attached to your posts add this根据您存储图像的方式,如果这是附加到您的帖子的特色图像,请添加此
$backgroundImg = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'full' ); $backgroundImg = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'full' );
foreach($recent_posts as $post) {
$backgroundImg = wp_get_attachment_image_src(get_post_thumbnail_id($post['ID']), 'full' );
echo '<div class="col-lg-4">';
echo '<div class="blog-entry">';
echo '<a href="', get_permalink($post['ID']), '" style="background-image: url('<?php echo $backgroundImg[0]; ?>');"></a>';
echo '<div class="text">';
echo '<div class="meta">';
echo '<div><span class="fa fa-calendar-alt"></span>', $post['post_date'], '</a></div>';
echo '<div><span class="fa fa-user"></span>', $post['post_author'], '</div>';
echo '<div><span class="fa fa-comments meta-chat"></span>', $post['comment_count'], '</div>';
echo '</div>';
echo '<div class="desc">';
echo '<h3 class="heading"><a href="', get_permalink($post['ID']), '">', $post['post_title'], '</a></h3>';
echo '</div>';
echo '</div>';
echo '</div>';
echo '</div>';
}
Use the following lines:使用以下行:
<div class="row">
<?PHP
include('blog/wp-load.php');
$recent_posts = wp_get_recent_posts(array(
'numberposts' => 3,
'post_type' => 'post',
'post_status' => 'publish'
));
foreach($recent_posts as $post) {
$imgurl = wp_get_attachment_url( get_post_thumbnail_id($post->ID), 'thumbnail' );
echo '<div class="col-lg-4">
<div class="blog-entry">
<a href="'. get_permalink($post['ID']). '" style="background-image: url('.$imgurl.');"></a>
<div class="text">
<div class="meta">
<div><span class="fa fa-calendar-alt"></span>' . $post['post_date'] . '</a></div>
<div><span class="fa fa-user"></span>'. $post['post_author'] . '</div>
<div><span class="fa fa-comments meta-chat"></span>' . $post['comment_count'] . '</div>'
</div>
<div class="desc">
<h3 class="heading"><a href="' . get_permalink($post['ID']) . '">' . $post['post_title'] . '</a></h3>
</div>
</div>
</div>
</div>';
}
?>
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.