如何显示页面的特色图像

问题描述 投票:-1回答:1

嗨,我正在尝试为每个页面提取我的页面特色图像。我已经设置了我的特色图像,但似乎无法显示特色图像。我试图从这些(如:Get featured image of a page (no post) in Wordpress

Add a pages featured image to a foreach loop

Wordpress Display Child page data: Title, featured image and custom field

Get Featured Image of Pages - WordPress

但仍然没有把我的特色图片拿出来。当我试图检索我的页面数据时,似乎没有存储在数组中的特色图像URL。请帮忙。

<?php                           
    $menuLocations = get_nav_menu_locations();
    $menuID = $menuLocations['introBlock_menu']; 
    $primaryNav = wp_get_nav_menu_items($menuID); 
    foreach ( $primaryNav as $navItem ) {
?>

<div class="col-md-4">
    <div class="intro-block">
    <!-- This is where I am trying to get the featured image url -->
        <div class="intro-block-inner">
            <h2>
            <?php  
                echo '<li style="list-style-type: none;"><a style="text-decoration: none;" href="'.$navItem->url.'" title="'.$navItem->title.'">'.$navItem->title.'</a></li></div></div></div>';
                } 
            ?>
            </h2>
php wordpress
1个回答
0
投票

首先创建图像URL的绝对路径并在需要的地方回显它。请检查以下示例:

<?php                           
    $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
?>

<div class="col-md-4">
    <div class="intro-block">
        <img src="<?php echo $url; ?>">
        <div class="intro-block-inner">
            <h2>Your Heading Element</h2>
        </div>
    </div>
</div>
© www.soinside.com 2019 - 2024. All rights reserved.