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

时间:2013-10-19 07:18:23

标签: php wordpress

我正在使用数据库获取id = 1,例如 - location = image url

就像那样

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

我试图通过使用帖子ID获取附件图片网址,例如

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'];

我总是得到这个消息“创建formpost数据失败”

如何在

中获取附件图片网址
$PicLocation =$result['location'];

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

2 个答案:

答案 0 :(得分:0)

<?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

答案 1 :(得分:0)

如果您使用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
    }
}

P.S。 如果你想创建批处理脚本或者某些东西,但是可以访问所有WordPress“magic”,可以在开头添加:

define('BASE_PATH', dirname(__FILE__).'/');
define('WP_USE_THEMES', false);

if ( !defined('ABSPATH') ) {
    require_once(BASE_PATH.'wp-load.php');
}

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