donglei3370 2016-01-14 15:13
浏览 21
已采纳

WP - 显示特定页面的缩略图

Trying to include a specific page on the frontpage of a WordPress Theme.

The overal HTML structure should be similar to this:

<div>
     <h1>Om</h1>
     <p>Content comes here</p>
</div>

<div style="background-image: url(...)">
</div>

At the moment it echos the content of the page successfully.

Here is how that part works with PHP:

<div class="omtekst col-md-6 half-content-wrapper">
    <h1 class="semibold">Om</h1>
    <?php
        $my_id = 7;
        $post_id_7 = get_post($my_id);
        $content = $post_id_7->post_content;
        $content = apply_filters('the_content', $content);
        $content = str_replace(']]>', ']]>', $content);
        echo $content;
    ?>
</div>

As mentioned earlier I want to include the thumbnail below this. This is where I am having problems.

However, I have started on the PHP to show how I am thinking. The thumbnail should be $image or similar.

<div class="ombilde col-md-6" style="background-image: url('<?php echo $image; ?>')">

Do you have a working solution for this?

Edit

My custom thumb which is not used with this image:

add_action( 'after_setup_theme', 'mytheme_custom_thumbnail_size' );
function mytheme_custom_thumbnail_size(){
    add_image_size( 'frontpage_thumb', 500 ); // Unlimited height
}

Enabled post thumbnails:

if ( function_exists( 'add_theme_support' ) ) { 
    add_theme_support( 'post-thumbnails' ); 
    add_theme_support( 'nav-menus' );
    add_theme_support( 'widgets' );
}
  • 写回答

3条回答 默认 最新

  • douraoyw194498 2016-01-14 15:30
    关注

    to get the url of image you can use

    $image = wp_get_attachment_image_src( get_post_thumbnail_id([ID_HERE]), [IMAGE SIZE HERE] );
    

    then you can use

    <div class="ombilde col-md-6" style="background-image: url('<?php echo $image[0]; ?>')">
    

    to get image size string look at your theme for "add_image_size" function

    and as pgk said post-thumbnails have to be enabled inside functions but i guess you already did this

    edit:

    [ID_HERE] replace with post id

    [IMAGE SIZE HERE] you can test this with "full"

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解
  • ¥15 有偿请教计算电磁学的问题涉及到空间中时域UTD和FDTD算法结合的
  • ¥15 three.js添加后处理以后模型锯齿化严重