如何在WP中进行查询以获取附件图像URL


how to make query to get attachment image url in wp

>例如,我正在使用数据库来获取id=1 - 位置=图像网址

是这样的

include 'config.php';
$GetPicId = $_GET["pid"]; // Picture ID from Index page
$query=mysql_query("SELECT * FROM fbcover WHERE id=$GetPicId") or die(mysql_error());
$result=mysql_fetch_array($query);
$PicLocation =$result['location'];

现在我想使用WordPress

例如,IAM 正在尝试使用帖子 ID 获取附件图像 URL

include 'config.php';
$GetPicId = $_GET["pid"]; // Picture ID from Index page
$query=mysql_query("SELECT * FROM wp_posts WHERE id=$GetPicId") or die(mysql_error());
$result=mysql_fetch_array($query);
$PicLocation =$result['guid'];

我总是得到这个消息" 创建表单帖子数据失败"

如何在 中获取附件图像 URL

$PicLocation =$result['location'];

我真的需要帮助...谢谢

<?php
remove_all_filters('posts_orderby');
query_posts('showposts=3&post_type=image&orderby=rand');
global $more; $more=0;?>
<?php if (have_posts) : while (have_posts()) : the_post(); global $more; $more=0;?>
<?php
$args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null, 'orderby' => 'rand', 'post_parent' => $post->ID );
            $attachments = get_posts($args);
            if ($attachments) {
                echo '';
                // count number of available images and change if less than the specified limit
                foreach ($attachments as $post) {
                        setup_postdata($post);
                        $image = wp_get_attachment_image_src( $post->ID, 'thumbnail', false );
                        echo '<span class="media"><a href="'.get_permalink().'" title="'.get_the_title().'" style="background-image: url('.$image[0].');">'.get_the_title().'</a></span>';;
                }
                echo '';
            }
            ?>
<?php endwhile; endif; ?>

友情提供:http://wordpress.org/support/topic/wordpress-query-for-attachments

如果你使用Wordpress,那么你应该使用内置函数。如果您有附件 ID 使用 wp_get_attachment_url($att_id) 获取链接或 wp_get_attachment_image_src($att_id) 获取文件的路径。

在这里,您有使用 get_posts 实现所需内容的良好示例:http://codex.wordpress.org/Template_Tags/get_posts

显示所有附件

$args = array( 'post_type' => 'attachment', 'posts_per_page' => -1, 'post_status' => 'any', 'post_parent' => null ); 
$attachments = get_posts( $args );
if ( $attachments ) {
    foreach ( $attachments as $post ) {
        the_attachment_link( $attachment->ID , false ); //for url
            $path = wp_get_attachment_image_src($attachment->ID, 'your-size'); //for direct path to image
    }
}

显示附件特定帖子

$args = array( 'post_type' => 'attachment', 'posts_per_page' => -1, 'post_status' =>'any', 'post_parent' => $post_id ); 
$attachments = get_posts( $args );
if ( $attachments ) {
    foreach ( $attachments as $attachment ) {
        the_attachment_link( $attachment->ID , false ); //for url
            $path = wp_get_attachment_image_src($attachment->ID, 'your-size'); //for direct path to image
    }
}

附言如果您想在站点外创建批处理脚本或其他内容,但可以访问所有WordPress"魔术",请在开头添加以下内容:

define('BASE_PATH', dirname(__FILE__).'/');
define('WP_USE_THEMES', false);
if ( !defined('ABSPATH') ) {
    require_once(BASE_PATH.'wp-load.php');
}

。您将可以访问我上面提到的所有功能。查找 wp-load.php 文件的路径。