[英]Get Wordpress posts so things like get_post_thumbnail_id don't make a database call
[英]Wordpress get_post_thumbnail_id not returning anything
為什么當我使用wp_get_attachment_image_src(get_post_thumbnail_id($post_id));
但是,當我var_dump
post_id
時,我什么也沒得到,我得到string(3) "652"
所以我知道有一個ID
沒有(string)
它給了我int(652)
碼:
<?php
$getPosts = new WP_Query(array('showposts' => 5, 'orderby' => 1));
if($getPosts->have_posts()):
$firstPost = true;
while($getPosts->have_posts()):
$getPosts->the_post();
$cssClass = '';
if($firstPost)
{
$cssClass = array('article','first-post');
$postValue = get_the_ID();
$post_id = (string)$postValue;
}else{
$cssClass = array('article');
}
?>
<?php if($firstPost): ?>
<?php $image = wp_get_attachment_image_src(get_post_thumbnail_id($post_id));?>
<a href="<?php echo $image[0];?>">Test</a>
似乎您使用的wp_get_attachment_image_src
不正確,如WordPress文檔中所述:
返回一個有序數組,其值對應於圖像附件(或表示任何附件的圖標)的(0)url,(1)寬度,(2)高度和(3)比例。
因此,看來您的代碼應如下所示:
<?php if($firstPost): ?>
<?php
$image_attributes = wp_get_attachment_image_src(get_post_thumbnail_id($post_id));
$image = $image_attributes[0];
?>
<a href="<?php echo $image;?>">Test</a>
編輯:實際上,當您執行此操作時,我仍然不清楚您的用法:
<a href="<?php echo $image;?>">Test</a>
您要在<a href></a>
中設置圖片網址嗎? 為什么? 應該不是這樣的:
<img src="<?php echo $image;?>" />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.