首页 > 解决方案 > 包装WordPress最新帖子链接

问题描述

我正在尝试用指向最新帖子的链接包装一个 div。目前,代码返回特色图像的值以用作连续 div 的背景图像。我只想用最新帖子的 url 包装它。谢谢

<?php
/* Get Recent Post */
$recent_post = wp_get_recent_posts(array(
    'numberposts' => 1,
    'post_status' => 'publish'
));
/* If Featured Image Set */
if ( has_post_thumbnail($recent_post[0]['ID']) ){
    /* Get Image */
    $image = wp_get_attachment_image_src( get_post_thumbnail_id($recent_post[0]['ID']), 'full');
    /* Output Div with Image Set Inline, Use padding Top for Responsive Ratio Size */
    echo '
<div class="featured-image-div" style="background-image:url('.$image[0].');"></div>
    ';                           
}

标签: phpwordpress

解决方案


get_the_permalink像这样使用

/* If Featured Image Set */
if ( has_post_thumbnail($recent_post[0]['ID']) ){
    $image = wp_get_attachment_image_src( get_post_thumbnail_id($recent_post[0]['ID']), 'full');
    echo '<a href="' . get_the_permalink($recent_post[0]['ID']) . '">
<div class="featured-image-div" style="background-image:url('.$image[0].');"></div></a>
    ';                           
}

函数文档:https ://developer.wordpress.org/reference/functions/get_the_permalink/


推荐阅读