如何为用户未设置帖子的帖子设置缩略图

时间:2019-10-02 10:10:04

标签: wordpress

如果用户在创建帖子时未选择缩略图,则希望主题为帖子设置缩略图。

我尝试浏览WordPress代码库,但可以找到任何“设置方法”。

<div class="standard-featured">
    <?php if( has_post_thumbnail() ) { ?>
        <?php the_post_thumbnail(); ?>
    <?php } else { ?>
        <img src="//Set your image default Path here.">
    <?php } ?>
</div>

3 个答案:

答案 0 :(得分:2)

有很多方法可以做到这一点。这是一种更清洁的方法。下面的代码示例。

<?php if( has_post_thumbnail() ) : ?>

  <?php the_post_thumbnail() ?>

<?php else : ?>

  // Dafult thumbnail image for post. try to write the exact image location.
  <img src="default.jpg" />

<?php endif; ?>

答案 1 :(得分:0)

尝试使用功能set_post_thumbnail()

更多信息可以在这里找到:https://developer.wordpress.org/reference/functions/set_post_thumbnail/

答案 2 :(得分:0)

如果要从帖子详细信息页面(single.php)设置帖子缩略图。然后首先添加来自wordpress媒体的图像。从管理面板或数据库中复制帖子的ID。

调用下面的函数。

 $post_media_id= '';/ id of your uploaded image
 $current_post_id = get_the_ID();// current post id            
 set_post_thumbnail($current_post_id , $post_media_id);