PHP wordpress-如果特色图片没有';不存在
这可能是一个重复的问题,但这些类似的问题中没有一个涉及到get_attached_media部分,这正是我需要帮助的地方 如果我在帖子中没有任何特色图片,我想使用贴在帖子内容中的图片作为特色图片 以下是我尝试和失败的地方:PHP wordpress-如果特色图片没有';不存在,php,wordpress,Php,Wordpress,这可能是一个重复的问题,但这些类似的问题中没有一个涉及到get_attached_media部分,这正是我需要帮助的地方 如果我在帖子中没有任何特色图片,我想使用贴在帖子内容中的图片作为特色图片 以下是我尝试和失败的地方: <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>> <?php if(get_post_thumbnail_id() == TRUE) {
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php
if(get_post_thumbnail_id() == TRUE) {
$image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'backstretch' );
}
else {
$image_url = wp_get_attachment_image_src( get_attached_media('image'), 'backstretch');
}
?>
<header class="backstretch-target backstretch-header <?php echo crisp_filter_img() ?>" data-background="<?php echo $image_url[0]; ?>">
这清楚地表明它以url的形式获取图像属性。我在谷歌上搜索并使用了这些代码,但我就是找不到问题所在
我真的很感谢你的帮助
另外,如果你仍然发现这是一个重复,我道歉 嗯,我解决了这个问题。我尝试使用流行的“获取图像”插件,但没有解决我的问题。一天结束时,我扔掉了连接的媒体,并使用了这个功能
函数crisp\u catch\u that\u image(){
$image\u url=wp\u get\u attachment\u image\u src(get\u post\u thumbnail\u id(),'backstretch');
如果(!empty($image\u url)){
返回$image_url[0];
}
全球$员额,$员额;
$first_img='';
ob_start();
ob_end_clean();
$output=preg_match_all('//i',$post->post_content,$matches);
if(空($matches[1])){
返回“”;
}
返回$matches[1][0];
返回$first\u img;
}
我使用此功能从WordPress帖子中获取所有可用图像:
function get_post_images( $post = null ) {
if ( $post === null ) {
global $post;
}
/* Get the featured image of the post */
if ( has_post_thumbnail() ) {
$images[] = get_post_thumbnail_id( $post->ID );
}
/* If the post contains galleries, get all images from them */
if( has_shortcode( $post->post_content, 'gallery' ) ) {
$galleries = get_post_galleries( $post, false );
foreach ( $galleries as $gallery ) {
$ids = explode( ',', $gallery['ids'] );
$images = array_merge( $images, $ids );
}
}
/* Get all single images in the post */
preg_match_all("/wp-image-(\d+)/", $post->post_content, $imgs );
foreach ($imgs[1] as $img) {
$images[] = $img;
}
/* get all images attached to the post
* not sure if this is a good idea, there might be images attached
* which were not supposed to be published */
$post_images = get_posts( array(
'post_parent' => $post->ID,
'post_type' => 'attachment',
'numberposts' => -1,
'post_mime_type' => 'image',
'exclude' => $images ) );
foreach ( $post_images as $image ) {
$images[] = $image->ID;
}
/* As a fallback, add a predefined default image */
if ( sizeof( $images ) == 0 && $this->default_image != null) {
$images[] = $this->default_image;
}
/* remove duplicated images */
$images = array_unique( $images );
return $images;
}
该函数返回一个数组,其中包含与给定帖子相关的所有图像ID,如果您只需要一个,则可以轻松提取:
$images = get_post_images();
echo wp_get_attachment_image( array_shift( $images ) );
function crisp_catch_that_image() {
$image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'backstretch' );
if (!empty($image_url)) {
return $image_url[0];
}
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
if (empty($matches[1])) {
return '';
}
return $matches[1][0];
return $first_img;
}
function get_post_images( $post = null ) {
if ( $post === null ) {
global $post;
}
/* Get the featured image of the post */
if ( has_post_thumbnail() ) {
$images[] = get_post_thumbnail_id( $post->ID );
}
/* If the post contains galleries, get all images from them */
if( has_shortcode( $post->post_content, 'gallery' ) ) {
$galleries = get_post_galleries( $post, false );
foreach ( $galleries as $gallery ) {
$ids = explode( ',', $gallery['ids'] );
$images = array_merge( $images, $ids );
}
}
/* Get all single images in the post */
preg_match_all("/wp-image-(\d+)/", $post->post_content, $imgs );
foreach ($imgs[1] as $img) {
$images[] = $img;
}
/* get all images attached to the post
* not sure if this is a good idea, there might be images attached
* which were not supposed to be published */
$post_images = get_posts( array(
'post_parent' => $post->ID,
'post_type' => 'attachment',
'numberposts' => -1,
'post_mime_type' => 'image',
'exclude' => $images ) );
foreach ( $post_images as $image ) {
$images[] = $image->ID;
}
/* As a fallback, add a predefined default image */
if ( sizeof( $images ) == 0 && $this->default_image != null) {
$images[] = $this->default_image;
}
/* remove duplicated images */
$images = array_unique( $images );
return $images;
}
$images = get_post_images();
echo wp_get_attachment_image( array_shift( $images ) );