WordPress.org

China 简体中文

Support

Support » 意见建议 » wp v3.9.2附件url问题

wp v3.9.2附件url问题

  • 我上个月有上传图片作为header-image,uploads是我自定义的二级域名url,组织形式是年-月,当然这些都不重要,我的问题是wp-include/post.php中获取附件的url的方式好像有点问题:
    wp-include/post.php line 4785:
    function wp_get_attachment_url( $post_id = 0 ) {
    $post_id = (int) $post_id;
    if ( !$post = get_post( $post_id ) )
    return false;

    if ( ‘attachment’ != $post->post_type )
    return false;

    $url = ”;
    if ( $file = get_post_meta( $post->ID, ‘_wp_attached_file’, true) ) { //Get attached file
    if ( ($uploads = wp_upload_dir()) && false === $uploads[‘error’] ) { //Get upload directory
    if ( 0 === strpos($file, $uploads[‘basedir’]) ) //Check that the upload base exists in the file location
    $url = str_replace($uploads[‘basedir’], $uploads[‘baseurl’], $file); //replace file location with url location
    elseif ( false !== strpos($file, ‘wp-content/uploads’) )
    $url = $uploads[‘baseurl’] . substr( $file, strpos($file, ‘wp-content/uploads’) + 18 );
    else
    $url = $uploads[‘baseurl’] . “/$file”; //Its a newly uploaded file, therefor $file is relative to the basedir.
    }
    }

    if ( empty($url) ) //If any of the above options failed, Fallback on the GUID as used pre-2.7, not recommended to rely upon this.
    $url = get_the_guid( $post->ID );

    /**
    * Filter the attachment URL.
    *
    * @since 2.1.0
    *
    * @param string $url URL for the given attachment.
    * @param int $post_id Attachment ID.
    */
    $url = apply_filters( ‘wp_get_attachment_url’, $url, $post->ID );

    if ( empty( $url ) )
    return false;

    return $url;
    }

    既然后面的$url = get_the_guid( $post->ID );可以获取附件的url,前面是不是显得有点多余?而且$uploads[‘basedir’]和$uploads[‘baseurl’]的内容都是最新的url,比如这个月是xxx/xxxx/xxx/2014/09/,但我的附件是在2014/08/上传的,必然造成完整的url出问题,请技术人员三思

  • The topic ‘wp v3.9.2附件url问题’ is closed to new replies.