我已使用多个缩略图插件向我的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');
?>
等等(第三,第四)..
有人可以帮忙介绍如何为其余部分添加说明吗?
答案 0 :(得分:0)
要显示带有标题的帖子缩略图,只需将以下代码粘贴到循环中:
<?php the_post_thumbnail();
echo get_post(get_post_thumbnail_id())->post_excerpt; ?>
您还可以通过在帖子循环中添加此代码来显示整个图像说明:
<?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 );
}
?>
答案 1 :(得分:0)
获取图片,其标题为多个帖子缩略图,
`
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; ?&GT;`
在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];
}