I had a look at this post which and copied some code which helped a little bit (the page isn't totally blank any more)
I'm trying to grab two URLs: The url to the full size image and the thumbnail version
From every post with a specific "field" from a custom taxonomy which I've created (I've created the taxonomy "art-type" and there are 3 options: Drawing, Print and Digital)
This is the code I've written so far:
<?php
$args = array(
'post_type' => 'art-piece', /* custom post type */
'taxonomy' => 'art-type', /* custom taxonomy */
'field' => 'slug',
'terms' => array('digital')
);
$gallery = get_posts ($args);
?>
<div class="zoom-gallery">
<div class="gallery-item">
<?php if (has_post_thumbnail( $post->ID ) ): ?>
<?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID), 'thumbnail' ); ?>
<img src="<?php echo $url ?>" />
<?php endif; ?>
</div>
</div>
From what I can see there's nothing in the loop that's using any of the $args I've specified. In theory this should grab all the featured images regardless or post type, taxonomy etc. but they're not coming up for me at all. The page is just totally blank
It looks to me that you're not looping through the results of the get_posts() call. Try changing the code to match the updated code I posted below.
What it does is it loops through each 'post' stored in the $gallery variable.
Apologies for not having enough time to give a more in-depth explanation however I'll come back and elaborate more if this works for you.
<?php
$args = array(
'post_type' => 'art-piece', /* custom post type */
'tax_query' => array(
array(
'taxonomy' => 'art-type', /* custom taxonomy */
'field' => 'slug',
'terms' => array('digital')
),
),
);
$gallery = get_posts($args); // removed space between function name and opening bracket
foreach ( $gallery as $post ) : setup_postdata( $post ); // added this loop
?>
<div class="zoom-gallery">
<div class="gallery-item">
<?php if (has_post_thumbnail( $post->ID ) ): ?>
<?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID), 'thumbnail' ); ?>
<img src="<?php echo $url ?>" />
<?php endif; ?>
</div>
</div>
<?php endforeach; // ending the loop
wp_reset_postdata(); // reset $post so we don't interfere with the main WP_Query
?>
Related
I am trying to develop a small WordPress plugin to fetch the posts, pages from the website and parse it as json to further use in mobile apps.
Right now I am achieving the goal via this method:
1) Created a file webservice.php on my current active theme eg. twentythirteen. So the location of the file is:
http://www.example.com/wp-content/themes/twentythirteen/webservice.php
2) I am posting the parameters on that URL to get a JSON response like this
http://www.example.com/wp-content/themes/twentythirteen/webservice.php?type=page&limit=10
The thing is I want to post parameters on the home page like this:
http://www.example.com?type=page&limit=10
I don't know how to do it but I have seen the JSON API plugin which is doing the same thing but I'm not able to find in the code how it's fetching the request from the home page and parse JSON on the same page. How can I do this?
I developed a WordPress plugin and I'm using it for my PhoneGap app, but it may also help you. This is the code for the callback last posts:
header('Content-Type: application/json');
require('../../../wp-load.php');
require('../../../wp-includes/pluggable.php');
$post = "";
$elementos = 5; //Number of Post
$yaCargados = 0;
global $wpdb;
if($_POST['num_post']!=0 or $_POST['num_post']!="NULL") {
$elementos = $_POST['num_post'];
$yaCargados = $_POST['paginacion'];
}
$args = array(
'posts_per_page' => $elementos,
'offset' => $yaCargados,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'suppress_filters' => true
);
$posts_array = get_posts( $args );
if(0 < $posts_array) {
foreach( $posts_array as $term) {
$res['posts'][] = $term;
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $term->ID ), 'medium' );
$res['images'][]['imagen'] = $image;
$custom_fields = get_post_custom($term->ID);
$res['custom_field'][] = $custom_fields;
}
echo json_encode($res);
}else{
}
Save archive in /wp-content/plugins/[YOUPLUGIN] and call and print post in JSON format.
Happy Coding!
I'm trying to pull the most recent post's featured image into a div's background css for the top of my site.
I currently have this, which I got from another post on here
<?php $src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), array( 5600,1000 ), false, '' );?>
<div class="case-hero" style="background-image: url('<?php echo esc_url( $src[0] ); ?>')">
Which is pulling the featured image of the current page. Looking for a solution so it finds the most recent.
Any help would be great!
:)
$recent = get_posts( array('numberposts' => 10) );
$src = false;
foreach($recent as $p){
if( has_post_thumbnail( $p->ID ) ){
$src = wp_get_attachment_image_src( get_post_thumbnail_id($p->ID), array( 5600,1000 ), false, '' );
break;
}
}
if(!$src){
$src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), array( 5600,1000 ), false, '' );
}
This should check last the 10 posts if featured image exists and if found set the $src to that image. If featured image is not found it sets it to this post featured image.
It helps if you search the WordPress Codex. You can use wp_get_recent_posts() to get the most recent post ID, and then use that to get_the_post_thumbnail().
Example:
$recent_posts = wp_get_recent_posts( array('numberposts' => 1,) );
$most_recent_post_thumbnail = get_the_post_thumbnail( $recent_posts[0]['ID'] );
// Do whatever you want with $most_recent_post_thumbnail
What I figured out so far:
I have a query like this:
$categories=get_category_by_slug('my_category_slug')
$posts_array = get_posts( array('category'=>$categories->cat_ID, 'numberposts' => -1 ));
foreach($posts_array as $post_array){
$queried_post = get_post($post_array->ID);
//I can get the source file link this way: wp_get_attachment_url( get_post_thumbnail_id($queried_post->ID))
}
But the source file is just to big. Functions like the_post_thumbnail( medium ) won't work for me because it's not just url. It's an url with image tag wrapper etc. So is there a way just to get the link to the medium (or small) size file?
It's also possible to set the post thumbnail size in the functions.php after the line with the theme support and post-thumbnail:
add_theme_support( 'post-thumbnails' );
set_post_thumbnail_size( 300, 300 );
I didn't try that but I don't want to set the size of all thumbnails.
Use wp_get_attachment_image_src(get_post_thumbnail_id($post_array->ID), 'medium').
This will return you an array with URL, width, height and cropping mode of this image.
EDIT: Updating to add the full code:
$categories = get_category_by_slug('my_category_slug');
$posts_array = get_posts( array('category' => $categories->term_id, 'numberposts' => -1 ));
foreach($posts_array as $post_array){
if( has_post_thumbnail($post_array->ID) ) {
$image_arr = wp_get_attachment_image_src(get_post_thumbnail_id($post_array->ID), 'medium');
$image_url = $image_arr[0]; // $image_url is your URL.
}
}
I am trying to create a list of PDFs (newsletters created each month). I have created a custom post type named 'newsletters' and restricted it to only supporting a 'title'.
I have then used the advanced custom fields plugin to add a file upload button to this post type. Therefore each post has a title and a button to upload the pdf.
I have then written the below function to output the list of attachments.
function list_newsletters(){
$args = array( 'post_type' => 'newsletters' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$permalink = get_permalink();
$title = get_the_title();
$id = get_the_ID();
$attachment = wp_get_attachment_url($id);
echo '<li><a href="'.$attachment.'">'.$title.'</li>';
endwhile;
}
However the wp_get_attachment_url($id) doesn't seem to work. I think this is because I am supposed to be supplying the attachment ID rather than the post ID. I have looked around online and cannot find a clear way of finding the attachment ID for a specific post.
Just to clarify each post will only contain one attached file.
Thank you in advance
This example taken from the get_posts() Codex page
$attachments = get_posts(array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' =>'any',
'post_parent' => $post->ID
));
if ($attachments) {
foreach ( $attachments as $attachment ) {
echo apply_filters( 'the_title' , $attachment->post_title );
the_attachment_link( $attachment->ID , false );
}
}
I have set up a custom post meta field for Thumbnail on each of my posts. This function is being called correctly, and everything works up until the very last line of update_post_meta
What's interesting is that I can echo out $imageURL and get the correct address, and the file uploads fine. I can even update_post_meta with any other value whether it be a string, or another variable within the function but as soon as I try to use $imageURL or $uploaded_file['url'] it just sets the post meta to a blank string.
I've used this snippet on projects that were developed with WordPress earlier than 3.1, but this one is 3.1. Could that have something to do with it? I kind of doubt it, since this seems to be one of those super weird bugs.
function tcr_save_thumbnail($post_id, $post) {
if ( !wp_verify_nonce( $_POST['eventmeta_noncename'], plugin_basename(__FILE__) )) {
return $post->ID;
}
if ( !current_user_can( 'edit_post', $post->ID ))
return $post->ID;
if(!empty($_FILES['tcr_thumbnail_meta']['name'])) { //New upload
require_once( ABSPATH . 'wp-admin/includes/file.php' );
$override['action'] = 'editpost';
$uploaded_file = wp_handle_upload($_FILES['tcr_thumbnail_meta'], $override);
$post_id = $post->ID;
$attachment = array(
'post_title' => $_FILES['tcr_thumbnail_meta']['name'],
'post_content' => '',
'post_type' => 'attachment',
'post_parent' => $post_id,
'post_mime_type' => $_FILES['tcr_thumbnail_meta']['type'],
'guid' => $uploaded_file['url']
);
// Save the data
$id = wp_insert_attachment( $attachment,$_FILES['tcr_thumbnail_meta'][ 'file' ], $post_id );
wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $_FILES['tcr_thumbnail_meta']['file'] ) );
$imageURL = $uploaded_file['url'];
update_post_meta($post->ID, "tcr_thumbnail_meta", $imageURL);
}
}
This is a functionality that has already been implemented in a plugin. try it http://wordpress.org/extend/plugins/taxonomy-images/ It is so complete!