Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/280.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)并集成到Pinterest按钮中_Php_Wordpress - Fatal编程技术网

PHP函数抓取特色图片(Wordpress)并集成到Pinterest按钮中

PHP函数抓取特色图片(Wordpress)并集成到Pinterest按钮中,php,wordpress,Php,Wordpress,我在函数中插入了这段代码。我的WordPress的PHP文件。它的基本功能是 如果用户单击Pinterest PIN IT按钮,它将检查博客文章页面中的第一个图像,并将其返回到Pinterest中 是否有人可以修改代码,使其完全忽略博客文章页面中的所有图像,而选择特色图像 捕捉第一个图像功能: function catch_that_image() { global $post, $posts; $first_img = ''; ob_start(); ob_end_clean(

我在函数中插入了这段代码。我的WordPress的PHP文件。它的基本功能是

如果用户单击Pinterest PIN IT按钮,它将检查博客文章页面中的第一个图像,并将其返回到Pinterest中

是否有人可以修改代码,使其完全忽略博客文章页面中的所有图像,而选择特色图像

捕捉第一个图像功能:

function catch_that_image() {
  global $post, $posts;
  $first_img = '';
  ob_start();
  ob_end_clean();
  $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
  $first_img = $matches [1] [0];

  if(empty($first_img)){ //Defines a default image
    $first_img = "http://www.bendaggers.com/wp-content/themes/Lord%20of%20Avernus%20-%20Abaddon/Images/Deafult_Img.png";
  }
  return $first_img;
}
<?php the_post_thumbnail(); ?> 
函数捕捉图像(){
全球$员额,$员额;
$first_img='';
ob_start();
ob_end_clean();
$output=preg_match_all('//i',$post->post_content,$matches);
$first_img=$matches[1][0];
if(empty($first_img)){//定义默认图像
$first_img=”http://www.bendaggers.com/wp-content/themes/Lord%20of%20Avernus%20-%20Abaddon/Images/Deafult_Img.png”;
}
返回$first\u img;
}
Wordpress特色图片:

function catch_that_image() {
  global $post, $posts;
  $first_img = '';
  ob_start();
  ob_end_clean();
  $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
  $first_img = $matches [1] [0];

  if(empty($first_img)){ //Defines a default image
    $first_img = "http://www.bendaggers.com/wp-content/themes/Lord%20of%20Avernus%20-%20Abaddon/Images/Deafult_Img.png";
  }
  return $first_img;
}
<?php the_post_thumbnail(); ?> 

给你:

function catch_that_image( $size = 'full' ) {
    global $post;
    if ( has_post_thumbnail($post->ID) ) {
        $featured_image = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), $size);
        return $featured_image[0];
    }
    return false;
}

如果设置了特征图像URL,则返回特征图像URL,否则返回
false
。您还可以在函数调用中设置大小,默认值为“大”。

如果
ob\u start()
和ob\u end\u clean()之后的值是多少?你希望这能做什么?谢谢你,彼得·迈克尔。只是一个$size='large'的问题,如果我不想指定一个大小,这样它就可以捕获图像的原始大小,该怎么办?我应该删除/删除$size='large'属性吗?更新了答案。除非你指定另一个尺寸,否则该函数现在将提取原始图像。太棒了!谢谢,谢谢!!