【问题标题】:Wordpress Pinterest Pin It Button. Manual CodeWordpress Pinterest 将其固定按钮。手动代码
【发布时间】:2012-06-22 23:28:04
【问题描述】:

我有一些 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>

【问题讨论】:

    标签: wordpress pinterest


    【解决方案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=&lt;?php echo $thumb; ?&gt;

    <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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-15
      • 2012-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-28
      • 1970-01-01
      相关资源
      最近更新 更多