简体   繁体   English

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

[英]WP - Show thumbnail of a specific page

Trying to include a specific page on the frontpage of a WordPress Theme. 尝试在WordPress主题的首页上包含特定页面。

The overal HTML structure should be similar to this: 总的HTML结构应与此类似:

<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: 这是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. 但是,我已经开始使用PHP来展示我的想法。 The thumbnail should be $image or similar. 缩略图应为$image或类似$image

<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' );
}

If you want to get post thumbnail, you can make this like that: 如果要获取帖子缩略图,可以这样:

if ( has_post_thumbnail( $post_id_7->ID ) ) {
    $image = get_the_post_thumbnail( $post_id_7->ID, $size, $args );
}

This returns image tag. 这将返回图像标签。 You can find more info here Codex - get_the_post_thumbnail 您可以在此处找到更多信息法典-get_the_post_thumbnail

Your theme must have 'post-thumbnail' support enabled. 您的主题必须启用“缩略图后”支持。

This is the easiast way to attach image to your post. 这是将图片附加到帖子的最简便的方法。

And if you are in theLoop, you can use the_post_thumbnail() function, it directly prints thumbnail image: 如果您在theLoop中,可以使用the_post_thumbnail()函数,它直接打印缩略图:

<?php the_post_thumbnail( $size, $attr ); ?> 

Codex - the_post_thumbnail() 食典-the_post_thumbnail()

If you want to print image as background, you must pit 'large' as $size. 如果要将图像打印为背景,则必须将“大”作为$ size加深。

Use wp_get_attachment_url($id) for retrieving the URL of the desired post. 使用wp_get_attachment_url($ id)检索所需帖子的URL。

In your example: 在您的示例中:

$my_id = 7;
            $post_id_7 = get_post($my_id);
            $my_thumb =  wp_get_attachment_url($my_id);
<div style="background-image: url('<?php echo $my_thumb; ?>')" class="omtekst col-md-6 half-content-wrapper">
        <h1 class="semibold">Om</h1>

    </div>

to get the url of image you can use 获取图像的URL,您可以使用

$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 获取图像大小字符串,请查看您主题的“ add_image_size”函数

and as pgk said post-thumbnails have to be enabled inside functions but i guess you already did this 就像pgk所说的,必须在函数内部启用post-thumbnails,但是我想你已经做到了

edit: 编辑:

[ID_HERE] replace with post id [ID_HERE]替换为帖子ID

[IMAGE SIZE HERE] you can test this with "full" [IMAGE SIZE HERE]您可以使用“完整”进行测试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM