从POST中获取第一张图片URL并设置为特色图片URL

时间:2015-07-26 作者:Nicholas John

我想主题,以获取在张贴区的第一个图像URL,并显示为特色图像它。这是来自旧的附属公司WordPress主题functions.php 文件这家公司已经倒闭了。

这是我正在处理的代码:

function pixr_get_thumb( $post )
{
    $wprobot = get_post_meta( $post->ID, "pixr_wpr_thumb", true );
    $wpzon = get_post_meta( $post->ID, "amzn_MediumImageURL", true );
    $goliath = get_post_meta( $post->ID, "amazon-image-url", true );
    $reviewazon = get_post_meta( $post->ID, "ReviewAZON_MediumImage", true );
    $amaniche = get_post_meta( $post->ID, "product_image", true );
    if ( has_post_thumbnail( ) )
    {
        $image_id = get_post_thumbnail_id( );
        $image_url = wp_get_attachment_image_src( $image_id, "blog-thumb", true );
        $image_url = $image_url[0];
    }
    $manual = $image_url;
    $pixrdefault = get_post_meta( $post->ID, "pixr_thumb", true );
    $nothumb = get_bloginfo( "template_directory" )."/assets/img/nothumb.png";
    if ( $wprobot )
    {
        $thumb = $wprobot;
    }
    else if ( $wpzon )
    {
        $thumb = $wpzon;
    }
    else if ( $goliath )
    {
        $thumb = $goliath;
    }
    else if ( $reviewazon )
    {
        $thumb = $reviewazon;
    }
    else if ( $amaniche )
    {
        $thumb = $amaniche;
    }
    else if ( $manual )
    {
        $thumb = $manual;
    }
    else if ( $pixrdefault )
    {
        $thumb = $pixrdefault;
    }
    else
    {
        $thumb = $nothumb;
    }
    return $thumb;
}

function pixr_azon_thumb( )
{
    global $post;
    $thumb = pixr_get_thumb( $post );
    echo $thumb;
}

function pixr_get_image( $post )
{
    $wprobot = get_post_meta( $post->ID, "pixr_wpr_image", true );
    $wpzon = get_post_meta( $post->ID, "amzn_LargeImageURL", true );
    $goliath = get_post_meta( $post->ID, "amazon-image-url-large", true );
    $reviewazon = get_post_meta( $post->ID, "ReviewAZON_LargeImage", true );
    $amaniche = get_post_meta( $post->ID, "image_large_1", true );
    if ( has_post_thumbnail( ) )
    {
        $image_id = get_post_thumbnail_id( );
        $image_url = wp_get_attachment_image_src( $image_id, "product-large", true );
        $image_url = $image_url[0];
    }
    $manual = $image_url;
    $pixrdefault = get_post_meta( $post->ID, "pixr_image", true );
    $nothumb = get_bloginfo( "template_directory" )."/assets/img/nothumb.png";
    if ( $wprobot )
    {
        $image = $wprobot;
    }
    else if ( $wpzon )
    {
        $image = $wpzon;
    }
    else if ( $goliath )
    {
        $image = $goliath;
    }
    else if ( $reviewazon )
    {
        $image = $reviewazon;
    }
    else if ( $amaniche )
    {
        $image = $amaniche;
    }
    else if ( $manual )
    {
        $image = $manual;
    }
    else if ( $pixrdefault )
    {
        $image = $pixrdefault;
    }
    else
    {
        $image = $nothumb;
    }
    return $image;
}

1 个回复
SO网友:Aumkar Thakur

首先,将此函数粘贴到函数上。php文件。

function catch_that_image() {
  global $post, $posts;
  $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)){ //Defines a default image
    $first_img = "/images/default.jpg";
  }
  return $first_img;
}
完成后,只需调用循环中的函数即可显示帖子中的第一幅图像:

echo catch_that_image();

结束

相关推荐