Reputation: 3
i was using database to get id=1 for example - location = image url
it was like that
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'];
now i would like to using wordpress
iam trying to get attachment image url by using post id for example
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'];
and i always get this msg " failed creating formpost data "
how can i get attachment image url in
$PicLocation =$result['location'];
i really need help ... thanks
Upvotes: 0
Views: 2890
Reputation: 1562
If you using Wordpress then you should use build in function for it. If you have attachment id use wp_get_attachment_url($att_id) to get link or wp_get_attachment_image_src($att_id) to get path to file.
Here you have good examples of using get_posts
to achieve what you want:
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. If you want to create batch script or something out of site but with access to all WordPress "magic" add this at beginning:
define('BASE_PATH', dirname(__FILE__).'/');
define('WP_USE_THEMES', false);
if ( !defined('ABSPATH') ) {
require_once(BASE_PATH.'wp-load.php');
}
...and you will have access to all functions I mention above. Lookout on paths to wp-load.php file.
Upvotes: 0
Reputation: 6156
<?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; ?>
Courtesy: http://wordpress.org/support/topic/wordpress-query-for-attachments
Upvotes: 1