我在这里要做的基本上是制作循环帖子的 div 背景图像的背景。
伪代码看起来像这样:
if this.post has an img tag {
store the img tag's src into a var,
then use that img src var as the background-image of the div which holds the permalink to the post
}
这有意义吗?我在想我必须从 WPDB 中检索 src,但我真的不知道如何不仅从 WPDB 获取该数据,而且将它放入一个 var 中,然后可以在 div 中使用背景图片...
我知道,这是一个相当高的要求,对我来说可能是一个很大的学习曲线。但这就是我们正确学习的方式! :)
提前致谢,任何人。
最佳答案
您可以使用帖子的“特色图片”。首先,通过将其添加到您的 functions.php
文件来激活发布缩略图:
add_theme_support( 'post-thumbnails' );
然后您可以使用以下代码检索特色图片:
$image_src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID));
然后将其应用于您的 div:
<div style="background-image: url(<?php echo $image_src; ?>);">
关于javascript - 如何从未加载的 wordpress 帖子中检索 img src 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14531545/