Wordpress Pinterest Pin It按钮。手动代码

时间:2012-06-22 23:28:04

标签: wordpress pinterest

我有一些Pinterest代码在我的wordpress帖子上显示Pin it按钮,但它只抓取帖子缩略图。我想要的是获取第一个帖子内容图片,如果帖子中没有图片,只抓取缩略图!??

已尝试了大量示例,但没有任何作用。

<a href="http://pinterest.com/pin/create/button/?url=<?php the_permalink(); ?>&media=<?php $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'thumbnail' ); echo $thumb['0']; ?>&description=<?php the_title(); ?>" class="pin-it-button" count-layout="horizontal">Pin It</a>

1 个答案:

答案 0 :(得分:1)

您可以尝试这样做,只需在functions.php

中粘贴以下功能即可
function get_first_image()
{
    global $post, $posts;
    $first_img = '';
    $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
    $first_img = $matches [1][0];
    if(empty($first_img)) $first_img = false;
    return $first_img;
}

Pinterest代码获取图片之前

$thumb=get_first_image();
if(!$thumb)
{
    $thumb=wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'thumbnail' );
    $thumb=$thumb[0];
}

然后在Pinterest链接media=<?php echo $thumb; ?>

<a href="http://pinterest.com/pin/create/button/?url=<?php the_permalink(); ?>&media=<?php echo $thumb; ?>&description=<?php the_title(); ?>" class="pin-it-button" count-layout="horizontal">Pin It</a>
相关问题