This question is about tidying up code and better management of said code butI'm a complete novice when it comes to PHP so would appreciate a little help.
I have this code:
<?php
$thumb_id = get_post_thumbnail_id(get_the_ID()); // gets the post thumbnail ID
$args = array(
'order' => 'ASC',
'orderby' => 'rand',
'post_type' => 'attachment',
'post_parent' => $post->ID,
'post_mime_type' => 'image',
'post_status' => null,
'numberposts' => 1,
'exclude' => $thumb_id
);
$attachments = get_posts($args);
if ($attachments) {
foreach ($attachments as $attachment) {
echo wp_get_attachment_image($attachment->ID, 'full', false);
}
}
?>
What it does isn't important for reference, the above code gets random images from a Wordpress post randomly generates one of them in a DIV. I want this functionality across many templates but I don't want to cram my PHP files with it as my files will get messy and inefficiently large.
2 questions.
Do I need to change the code above in order to put it within functions.php?
How can I reference the above code (that will be within my functions.php) using a short one liner that I can reuse across many different templates?
You may like to try putting the snippet in to a function, inside functions.php, and passing it a few parameters, allowing the usage to be more flexible.
Untested, but this takes an array of options, and overwrites the default values (e.g for changing the order or orderby etc on a per-use basis). As an optional parameter you can pass a post_id, in case you want to query a post that isn't the current one.
It also returns an array of rather than outputs them directly, which can be seen as the preferred way of working with functions.
// functions.php
function get_random_post_image($options=array(), $post_id=NULL) {
if($post_id != NULL) :
$thumb_id = get_post_thumbnail_id($post_id);
else :
$thumb_id = get_post_thumbnail_id(get_the_ID());
endif;
$default_args = array(
'order' => 'ASC',
'orderby' => 'rand',
'post_type' => 'attachment',
'post_parent' => $post->ID,
'post_mime_type' => 'image',
'post_status' => null,
'numberposts' => 1,
'exclude' => $thumb_id
);
// merge custom options
$args = array_merge($default_args, $options);
$attachments = get_posts($args);
if ($attachments) {
$images = array();
foreach ($attachments as $attachment) {
$images[] = wp_get_attachment_image($attachment->ID, 'full', false);
}
return $images;
}
return false; // or, return default image/placeholder
}
// and within your template/posts:
if(function_exists('get_random_post_image')) :
$images = get_random_post_image(array('order'=>'DESC')); // overwrite `ASC`
if($images) :
foreach($images as $img) {
echo '<div class="post-img"> ' . $img . '</div>';
}
else :
echo 'No images!';
endif;
endif;
not perfect; but you could extend it easily enough.
1. You need to get out the <?php and ?> if you already put your code inside those tags
example functions.php:
<?php
blah blah
....
// your code here
$thumb_id = get_post_thumbnail_id(get_the_ID()); // gets the post thumbnail ID
$args = array(
'order' => 'ASC',
'orderby' => 'rand',
'post_type' => 'attachment',
'post_parent' => $post->ID,
'post_mime_type' => 'image',
'post_status' => null,
'numberposts' => 1,
'exclude' => $thumb_id
);
$attachments = get_posts($args);
if ($attachments) {
foreach ($attachments as $attachment) {
echo wp_get_attachment_image($attachment->ID, 'full', false);
}
}
// be careful, only one "?>" in the file, no nested "<?php ?>" blocks
?>
2. Use include(), include_once(), require() or require_once() and save your function in another file, so you can reference that file.
random_img.php
<?php
$thumb_id = get_post_thumbnail_id(get_the_ID()); // gets the post thumbnail ID
$args = array(
'order' => 'ASC',
'orderby' => 'rand',
'post_type' => 'attachment',
'post_parent' => $post->ID,
'post_mime_type' => 'image',
'post_status' => null,
'numberposts' => 1,
'exclude' => $thumb_id
);
$attachments = get_posts($args);
if ($attachments) {
foreach ($attachments as $attachment) {
echo wp_get_attachment_image($attachment->ID, 'full', false);
}
}
?>
functions.php
<?php
blah blah
....
include ("random_img.php")
?>
Related
I have this code
function display_categoria($args) {
$query = new WP_Query(array(
'post_type' => 'job_listing',
'post_status' => 'publish',
'posts_per_page' => 5
));
while ($query->have_posts()) {
echo $query->the_post();
$id=get_the_id();
echo $query1=get_permalink();
}
wp_reset_query();
}
add_shortcode( 'este', 'display_categoria' );
in theory i can solve it placing in the loop
if ( has_post_thumbnail() ) {
the_post_thumbnail();
}
but many entries not have a thumbnail (featured images), Can understand?
This should retrieve the url of the first image for each post. Insert it after the "$id=get_the_id();" line
$args = array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'posts_per_page' => -1,
'post_status' => null,
'post_parent' => $id
);
$images = get_posts( $args );
if ( $images ) {
$first_image_id = $images[0];
//do something with the image
wp_reset_postdata();
}
I am adding an image from media in every single post that are created.
To get the attached image and url of that post, I am implementing this code:
$args = array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'numberposts' => 5,
'post_status' => null,
'post_parent' => 'any', // any parent
);
$attachments = get_posts($args);
if ( $attachments ) {
foreach ( $attachments as $attachment ) {
setup_postdata($attachment);
$v =$attachment->ID;
$imageurl = wp_get_attachment_url($v);
$postlink = get_permalink($v);
}
The above code works fine for retrieving image url. My question is how do I pass post ID in get_permalink() to make sure that I get link of that post. I know that passing $v in get_permalink() is wrong.
There is a code spinet to print attachment posts.
$args = array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'posts_per_page' => 5,
'post_status' => 'inherit',
'post_parent' => 'any', // any parent
);
$loop = new WP_Query($args);
while ( $loop->have_posts() ) : $loop->the_post();
global $post;
echo '<pre>';
print_r($post);
echo '</pre>';
endwhile; wp_reset_query();
Its depending which ID you are looking for inside the loop
$post-ID : That return the post ID for attachment post.
$post->post_parent : It returns the current attachment post parent post ID.
I'd like some assistance modifying this code (source):
<?php
$args = array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'numberposts' => 4,
'post_status' => null,
'post_parent' => any, // any parent
);
$attachments = get_posts($args);
if ($attachments) {
foreach ($attachments as $post) {
setup_postdata($post);
the_attachment_link($post->ID, false);
}
}
?>
Right now, it's getting querying all the image attachments from my WP install and displaying the latest 4 as their thumbnail versions. My problem is that the_attachment_link only allows for it to link to either the full-size image or the attachment page of the image. I'd like it to link to the post (and I can guarantee that each image is only attached to 1 post, if that would be an issue/concern).
I've tried using other functions like wp_get_attachment_image and wp_get_attachment_link but they end up displaying nothing (and no error either). Any help please?
Here is your solution:
<?php
$args = array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'numberposts' => 4,
'post_status' => null,
'post_parent' => 'any', // any parent
);
$attachments = get_posts($args);
if ($attachments) {
foreach ($attachments as $post) {
setup_postdata($post);
?>
<?php echo wp_get_attachment_image($post->ID) ?>
<?php
}
}
?>
Make sure $post->post_parent has a value.
I´m trying to get the post thumbnail and another attached image from the last post of a custom post type (called parceiros-e-links).
What I got is to get all posts with images and show them... but I need to show only the last post and show it with two images (the_post_thumbnail and the wp_get_attachment_image)
Here is my current code:
<?php
$query = new WP_Query( array( 'post_type' => 'parceiros-e-links', 'posts_per_page' => -1, 'orderby' => 'date', 'order' => 'DESC' ) ); //the first loop where I filter the posts from custom post type and by date
if( $query->have_posts() ){
while($query->have_posts()){
$query->the_post();
$image_query = new WP_Query( array( 'post_type' => 'attachment', 'post_status' => 'inherit', 'post_mime_type' => 'image', 'posts_per_page' => 2, 'post_parent' => get_the_ID() ) ); //the second loop where I filter the posts with attachments and limit to two
while( $image_query->have_posts() ) { $image_query->the_post();
//code below prints the two attachments: thumbnail and another one.
if(has_post_thumbnail()){
the_post_thumbnail('home-parceiros');
}
echo wp_get_attachment_image( get_the_ID(), 'home-parceiros-foto' );
}
}
}
?>
I already spent many hours searching similar situations and trying to filter this code but I'm without ideas... Because if I limit the query's first posts_per_page to 1, if the most recent post doesn't has an attachment, no images will be printed!
Any clues about how to limit the number of posts with attachments inside a custom post type?
Thanks!
Your mistakes was at the_post_thumbnail('home-parceiros'); and echo wp_get_attachment_image( get_the_ID(), 'home-parceiros-foto' ); Read about the correct attributes given for this two functions here:
http://codex.wordpress.org/Function_Reference/the_post_thumbnail
http://codex.wordpress.org/Function_Reference/wp_get_attachment_image
Here is my suggestion:
$query = new WP_Query(
array(
'post_type' => 'parceiros-e-links',
'posts_per_page' => 1,
'orderby' => 'date',
'order' => 'DESC'
)
);
if($query->have_posts()) :
while($query->have_posts()) : $query->the_post();
if(has_post_thumbnail()) : the_post_thumbnail(); endif;
$args = array(
'post_type' => 'attachment',
'numberposts' => 1,
'post_status' => null,
'post_parent' => $post->ID
);
$attachments = get_posts( $args );
if ( $attachments ) {
foreach ( $attachments as $attachment ) {
echo wp_get_attachment_image( $attachment->ID, 'full' );
}
}
endwhile;
endif;
Please let me know :)
Example #2 updated:
$query = new WP_Query( array( 'post_type' => 'parceiros-e-links', 'posts_per_page' => 1, 'orderby' => 'date', 'order' => 'DESC' ) ); //the first loop where I filter the posts from custom post type and by date
if( $query->have_posts() ){
while($query->have_posts()){
$query->the_post();
$image_query = new WP_Query( array( 'post_type' => 'attachment', 'post_status' => 'inherit', 'post_mime_type' => 'image', 'posts_per_page' => 2, 'post_parent' => get_the_ID() ) ); //the second loop where I filter the posts with attachments and limit to two
while( $image_query->have_posts() ) { $image_query->the_post();
//code below prints the two attachments: thumbnail and another one.
if(has_post_thumbnail()){
the_post_thumbnail('home-parceiros');
}
echo wp_get_attachment_image( get_the_ID(), 'home-parceiros-foto' );
}
}
}
How would I retrieve attachments from all subpages of a specific Page ID?
Example:
SPECIFIC PAGE
Child (with attachments)
Child (with attachments)
Child (with attachments)
I'm currently using this code to retrieve all attachments site-wide, however I would like to limit this to only pull images from all children of a specific Page.
<?php
$args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null, 'post_parent' => null );
$attachments = get_posts( $args );
if ($attachments) {
foreach ( $attachments as $post ) {
setup_postdata($post);
the_title();
the_attachment_link($post->ID, false);
the_excerpt();
}
}
?>
Almost there using this code as per Nick's suggestion below:
<?php
$mypages = get_pages('child_of=19');
foreach ( $mypages as $mypage ) {
$attachments = get_children(array('post_parent' => $mypage->ID, 'numberposts' => 1, 'post_type' => 'attachment', 'post_mime_type' => 'image', 'orderby' => 'rand'));
if ($attachments) {
foreach ( $attachments as $post ) {
setup_postdata($post);
the_title();
the_attachment_link($post->ID, false);
the_excerpt();
}
}
}
?>
However, there are two remaining issues:
Limiting the amount of total photos pulled. Using 'numberposts' only limits the amount of images pulled from each post
Randomization. Orderby => rand only randomizes the images within each post. I would like to randomly shuffle the order for everything.
Try using get_pages( $args )
<?php $args = array(
'child_of' => 'SPECIFIC PAGE',
'parent' => 'SPECIFIC PAGE',
'post_type' => 'attachment',
'post_status' => 'publish',
'numberposts' => -1
); ?>
Using child_of will get all children and grandchildren.
parent will limit this to just the children that have this as a parent. No grandchildren.
See here for more details. http://codex.wordpress.org/Function_Reference/get_pages