如果没有附件,如何隐藏图片url?

时间:2012-04-13 作者:Demilio

如果没有附件,如何隐藏img标签?

(函数来自本教程:http://wp.tutsplus.com/tutorials/automagic-post-thumbnails-image-management/)

<img src="<?php get_attachment_picture();?>" />
我需要这样的东西:

<?php if ( get_attachment_picture()) { ?>
<img src="<?php get_attachment_picture();?>">
<?php } else { ?>
show nothing, not even av default image
<?php } ?>
以下是我使用的函数:

<!?php 

/* Function to process your thumbnail & image
   Copy and paste the code below to your functions.php */

function get_attachment_picture(){
    global $post, $posts;
    $related_thumbnail =  get_post_meta($post->ID, \'image\', $single = true);                            //read post meta for image url

    if($related_thumbnail == ""):
        $attachments = get_children( array(
                                    \'post_parent\'    => get_the_ID(),
                                    \'post_type\'      => \'attachment\',
                                    \'numberposts\'    => 1,
                                    \'post_status\'    => \'inherit\',
                                    \'post_mime_type\' => \'image\',
                                    \'order\'          => \'ASC\',
                                    \'orderby\'        => \'menu_order ASC\'
                                    ) );
        if(!empty($attachments)):                                               //check if there an attachment or not
            foreach ( $attachments as $attachment_id => $attachment ) {
              if(wp_get_attachment_image($attachment_id) != ""):
                  $related_thumbnail = wp_get_attachment_url( $attachment_id );
              endif;
            }
        else:                                                           // if no attachment
            $first_img = \'\';
            ob_start();
            ob_end_clean();
            $output = preg_match_all(\'/<img.+src=[\\\'"]([^\\\'"]+)[\\\'"].*>/i\', $post->post_content, $matches);
            $first_img = $matches [1] [0];

            if(!empty($first_img)):
                $related_thumbnail = $first_img;
            else:
                $related_thumbnail = "images/default_thumbnail.jpg";                            //define default thumbnail, you can use full url here.
            endif;
        endif;
    endif;  

    echo $related_thumbnail;
} ?>

1 个回复
最合适的回答,由SO网友:Tom J Nowell 整理而成

假设您正在学习本教程:

http://wp.tutsplus.com/tutorials/automagic-post-thumbnails-image-management/

将get\\u attachment\\u picture末尾的echo语句替换为return,然后更改图像代码。

这是您的新图像代码:

// get the URL of the image
$src = get_attachment_picture();

// if the url is not empty, display the image
if(!empty($src)){
    ?>
    <img src="<?php echo $src; ?>" />
    <?php
} else {
    // the url was empty, do not display the image
}
下面是修改后的函数:

function get_attachment_picture(){  
    global $post, $posts;  
    $related_thumbnail =  get_post_meta($post->ID, \'image\', true);                          //read post meta for image url  

    if($related_thumbnail == ""):
        $attachments = get_children( array(  
                                    \'post_parent\'    => get_the_ID(),  
                                    \'post_type\'      => \'attachment\',  
                                    \'numberposts\'    => 1,  
                                    \'post_status\'    => \'inherit\',  
                                    \'post_mime_type\' => \'image\',  
                                    \'order\'          => \'ASC\',  
                                    \'orderby\'        => \'menu_order ASC\'  
                                    ) );  
        if(!empty($attachments)):                                               //check if there an attachment or not  
            foreach ( $attachments as $attachment_id => $attachment ) {  
              if(wp_get_attachment_image($attachment_id) != ""):  
                  $related_thumbnail = wp_get_attachment_url( $attachment_id );  
              endif;  
            }  
        else:                                                           // if no attachment  
            $first_img = \'\';  

            $output = preg_match_all(\'/<img.+src=[\\\'"]([^\\\'"]+)[\\\'"].*>/i\', $post->post_content, $matches);  
            $first_img = $matches [1] [0];  

            if(!empty($first_img)):  
                $related_thumbnail = $first_img;  
            else:  
                $related_thumbnail = "";
            endif; 
        endif; 
    endif;   

    return $related_thumbnail; 
} 

结束

相关推荐

使用htaccess将wp-Content/Themes/Name/Images掩蔽为仅图像目录

我正在尝试一些我认为非常简单的方法来屏蔽我的url,但似乎无法使其正常工作。我希望能够链接到我的img标签中的图像,而不必键入完整的url。i、 e。Current url: http://server.com/wp-content/themes/standard/images/img.jpg or <img src = \"http://server.com/wp-content/themes/standard/images/img.jpg\" /> 然而,在我的