Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/247.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php WordPress:Set';特色图片';保存帖子时作为第一个ACF库_Php_Wordpress_Advanced Custom Fields - Fatal编程技术网

Php WordPress:Set';特色图片';保存帖子时作为第一个ACF库

Php WordPress:Set';特色图片';保存帖子时作为第一个ACF库,php,wordpress,advanced-custom-fields,Php,Wordpress,Advanced Custom Fields,我找到了这个函数。 我用的是ACF pro 更新:我根据下面的注释添加了变量,这消除了错误,但是函数仍然不工作 functions.php: add_action( 'save_post', 'set_featured_image_from_gallery' ); function set_featured_image_from_gallery() { $post = get_post(); //Edit according to comment below $has_th

我找到了这个函数。 我用的是ACF pro

更新:我根据下面的注释添加了变量,这消除了错误,但是函数仍然不工作

functions.php:

add_action( 'save_post', 'set_featured_image_from_gallery' );

function set_featured_image_from_gallery() {
  $post = get_post(); //Edit according to comment below      
  $has_thumbnail = get_the_post_thumbnail($post->ID);

  if ( !$has_thumbnail ) {

    $images = get_field('gallery', false, false);
    $image_id = $images[0];

    if ( $image_id ) {
      set_post_thumbnail( $post->ID, $image_id );
    }
  }
}
保存帖子时出现错误消息(按“更新”-按钮):

add_action( 'save_post', 'set_featured_image_from_gallery' );

function set_featured_image_from_gallery() {
  $post = get_post(); //Edit according to comment below      
  $has_thumbnail = get_the_post_thumbnail($post->ID);

  if ( !$has_thumbnail ) {

    $images = get_field('gallery', false, false);
    $image_id = $images[0];

    if ( $image_id ) {
      set_post_thumbnail( $post->ID, $image_id );
    }
  }
}
注意:未定义变量:第600行的post in/Applications/MAMP/htdocs/pf blank/wp/wp content/themes/pf blank theme/functions.php

注意:尝试在第600行的/Applications/MAMP/htdocs/pf blank/wp/wp content/themes/pf blank theme/functions.php中获取非对象的属性

警告:无法修改标题信息-第197行/Applications/MAMP/htdocs/wp-blank/wp/wp-content/themes/pf-blank-theme/functions.php:600)中已发送的标题


警告:无法修改标题信息-标题已由发送(输出开始于/Applications/MAMP/htdocs/pf blank/wp/wp content/themes/pf blank theme/functions.php:600)在第1174行的/Applications/MAMP/htdocs/pf blank/wp/wp includes/pluggable.php中,您需要通过提供参数在函数中传递参数

尝试以下代码:

function set_featured_image_from_gallery() {

    $args = array( 'posts_per_page' => 10, 'order'=> 'ASC');
    $postslist = get_posts( $args );
    foreach ( $postslist as $post ) :
      $has_thumbnail = get_the_post_thumbnail($post->ID);

      if ( !$has_thumbnail ) {

        $images = get_field('gallery', false, false);
        $image_id = $images[0];

        if ( $image_id ) {
          set_post_thumbnail( $post->ID, $image_id );
        }
      }
      endforeach; 
    }

    add_action( 'save_post', 'set_featured_image_from_gallery' );

首先,您需要在$post变量中获取post数据,然后您可以使用$post->ID,这是您在函数中传递的,因此使用get_posts data将所有post数据获取到$post变量中,然后尝试添加一个
$post=get_posts()
不起作用,在
$has\u缩略图周围使用foreach循环尝试,也不起作用。请给我一个代码示例好吗?为了让它起作用:
$postlist=get_posts($args)=
$postslist=get_posts($args)
&
if($has\u缩略图){
=
if(!$has\u缩略图){
@Carl Papworth抱歉,没有收到您的详细说明?我的解决方案不适用于您?该函数可以工作,但有一些输入错误:
$postlist
变量名称不相同,
$has_缩略图
应该是
!$has_缩略图
,这样它就不会更改以前设置的缩略图。这只是为了复制/粘贴-目标,干杯。