在Wordpress中设置默认的标题图像


Setting default Post header image in Wordpress?

如何修改Wordpress主题以自动填充帖子的标题图像,如果没有手动设置?对于PHP,我完全是个新手。

使用get_post_meta()函数可以实现此功能。你可以在第一个参数中传递post id,可以在第二。检查下面的代码是否在数据库中可用不是。

试试这段代码。

<?php
    if(is_single() & !is_home()) 
    {
      $myfield = 'custom_field_name'; // Change this to the name of the custom field you use..
      $postimage = get_post_meta($post->ID, $myfield, true);
          if($postimage) 
          { 
                // If the field has a value.. set image path using value...
          } 
          elseif(!$postimage) 
          { 
                // If no value than set Default image path..
          }
    }
?>