【问题标题】:Display Thumbnails Description with Multiple Post Thumbnails显示带有多个帖子缩略图的缩略图描述
【发布时间】:2017-01-06 17:07:06
【问题描述】:

我使用多个帖子缩略图插件向我的 wordpress 网站添加了多个特色图片。我正在尝试将它们全部显示在带有描述的内容下方。我可以为主要特色图片做到这一点,没问题。我可以毫无问题地显示其余的特色图像,但是每当我尝试通过它添加描述时,它是页面描述而不是图像描述。

这就是我添加主图像和描述的方式。

<?php the_post_thumbnail( 'product-thumbnail' ); 
echo get_post(get_post_thumbnail_id())->post_content; ?>

剩下的图片是这样添加的:

<?php MultiPostThumbnails::the_post_thumbnail(get_post_type(), 'secondary-    
image', NULL,  'product-thumbnail');
?>

等等(第三,第四)..

有人可以帮忙添加其余的描述吗?

【问题讨论】:

    标签: php jquery html wordpress


    【解决方案1】:

    要显示带有标题的帖子缩略图,只需将以下代码粘贴到循环中:

    <?php the_post_thumbnail();
    echo get_post(get_post_thumbnail_id())->post_excerpt; ?>
    

    您还可以通过在 post 循环中添加以下代码来显示整个图像描述:

    <?php the_post_thumbnail();
    echo get_post(get_post_thumbnail_id())->post_content; ?>
    

    以上代码用于单张图片,如果您使用多张图片,请使用以下代码

    <?php
            $the_post_images = get_children( array(
                'post_parent' => $post->ID,
                'post_type' => 'attachment',
                'post_mime_type'=> 'image'
            ) );
    
        foreach ($the_post_images as $the_post_image) {
            // SHOW FEATURED IMAGE TITLES
            echo get_the_title( $the_post_image->ID );
            //SHOW IMAGE DESCRIPTION OR CAPTIONS
            echo apply_filters( 'get_the_excerpt', $the_post_image->post_excerpt );
        }
    ?>
    

    【讨论】:

    • 是的,我确实对主图像这样做了,因为您会在我的问题中看到确切的代码,但我使用了多个特色图像和插件多个帖子缩略图。它正在添加我想弄清楚的其他缩略图描述。
    • 我也使用 LightGalley 滑块,我创建了这个代码来显示特色图像标题和图像描述。显示上面的附加代码
    【解决方案2】:

    要获取图片,它在多个帖子缩略图中的标题,

    `

      echo $secondimgPath =  MultiPostThumbnails::get_post_thumbnail_url( get_post_type(), 'second-featured-image', NULL);
    
      echo $secondimgIdAttachment = abcd_get_attachment_id_by_url($secondimgPath);
    
      $size = array( 854,395, 'bfi_thumb' => true, 'quality' => 100);
    
      echo $secondLargeImage[0] = wp_get_attachment_image( $secondimgIdAttachment,$size );
    
      echo get_post( $secondimgIdAttachment )->post_excerpt;
    

    endif; ?>`

    在functions.php中,使用以下 -

    function abcd_get_attachment_id_by_url( $url ) {
    // Split the $url into two parts with the wp-content directory as the separator
    $parsed_url  = explode( parse_url( WP_CONTENT_URL, PHP_URL_PATH ), $url );
    // Get the host of the current site and the host of the $url, ignoring www
    $this_host = str_ireplace( 'www.', '', parse_url( home_url(), PHP_URL_HOST ) );
    $file_host = str_ireplace( 'www.', '', parse_url( $url, PHP_URL_HOST ) );
    // Return nothing if there aren't any $url parts or if the current host and $url host do not match
    if ( ! isset( $parsed_url[1] ) || empty( $parsed_url[1] ) || ( $this_host != $file_host ) ) {
        return;
    }
    // Now we're going to quickly search the DB for any attachment GUID with a partial path match
    // Example: /uploads/2013/05/test-image.jpg
    global $wpdb;
    $attachment = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->prefix}posts WHERE guid RLIKE %s;", $parsed_url[1] ) );
    // Returns null if no attachment is found
    return $attachment[0];
    

    }

    【讨论】:

      猜你喜欢
      • 2013-07-02
      • 1970-01-01
      • 2017-08-02
      • 1970-01-01
      • 2011-02-26
      • 2016-06-20
      • 1970-01-01
      • 2011-03-15
      • 1970-01-01
      相关资源
      最近更新 更多