Wordpress - Including page title in 'wp get attachment image' - php

Okay, I've set up a bit of code which searching for all the pages which are a child of the ID 8, then outputs all the attachments (in the gallery) of these pages as unordered list items. You can see the effect so far here http://goo.gl/eq4UF.
The problem I'm having is that I need to include the title of each page before each so you can easily identify which images come below which page. Normally I would just add this in, but the list items al use masonry and are positioned all over the page using some JS so they never appear beside the first image in the list.
I therefore will add the title of the page to every
<li> in the <ul> which will allow the title to run with each image but I don't know how to include this in the wp get attachment image function. Both the_title and wp_title doesn't work inside this loop. apply_filters( 'the_title', $attachment->post_title ); obviously takes the image title, but is there any good to take the page title?
Thanks in advance and hope this made sense,
R
<?php $postslist = get_pages('number=9999&sort_order=DESC&sort_column=post_date&child_of=8');
foreach ($postslist as $post) :
setup_postdata($post); ?>
<ul class="main-projects-list">
<?php
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => null,
'post_parent' => $post->ID,
'orderby' => 'menu_order',
'order' => 'ASC',
);
$attachments = get_posts( $args );
if ( $attachments ) {
foreach ( $attachments as $attachment ) {
echo '<li class="each-image">';
echo wp_get_attachment_image( $attachment->ID, 'large' );
echo '<p>';
echo apply_filters( 'the_title', $attachment->post_title );
echo '</p></li>';
}
}
?>
</ul>
<?php endforeach; ?>

You can try this:
<?php $postslist = get_pages('number=9999&sort_order=DESC&sort_column=post_date&child_of=8');
foreach ($postslist as $post) :
setup_postdata($post); ?>
<ul class="main-projects-list">
<?php
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => null,
'post_parent' => $post->ID,
'orderby' => 'menu_order',
'order' => 'ASC',
);
$attachments = get_posts( $args );
if ( $attachments ) {
$post_title = get_the_title($post->ID); // We get the post title
foreach ( $attachments as $attachment ) {
$img_title = apply_filters( 'the_title', $post_title . ' - ' . $attachment->post_title ); // We create the image title with the 2 strings
echo '<li class="each-image">';
echo wp_get_attachment_image( $attachment->ID, 'large' , false, array('title' => $img_title));
echo '<p>';
echo $img_title;
echo '</p></li>';
}
}
?>
</ul>
<?php endforeach; ?>

Related

Get wordpress attachments ID's separated by commas of current post

i have created custom post type named "Person" and i need to get all attachments ID's separated by commas of current post in loop.
Here i have a code that loops the "person" post type posts and code that shows every posts attachments ID's, but thing is that that those ID returns like this "2713271227112710" i need it to return seperated by commas like this "2713,2712,2711,2710"
Here is my code:
<?php
$args = array(
'post_type' => 'person',
'post_status' => 'publish',
'posts_per_page' => 24,
'order' => 'ASC'
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="col-md-4">
<div class="person">
<?php if ( $post->post_type == 'person' && $post->post_status == 'publish' ) {
$attachments = get_posts( array(
'post_type' => 'attachment',
'posts_per_page' => -1,
'post_parent' => $post->ID,
'exclude' => get_post_thumbnail_id()
) ); ?>
<?php
if ( $attachments ) {
foreach ( $attachments as $attachment ) {
$thumbimg = wp_get_attachment_url( $attachment->ID, 'full', false );
$attachment_id = attachment_url_to_postid( $thumbimg );
echo $attachment_id; //here outputs the attachemnts ID's of current post
}
}
}
?>
<?php echo do_shortcode( '[vc_images_carousel images="'.$attachment_id.'" img_size="large" speed="3000" autoplay="yes" hide_pagination_control="yes" hide_prev_next_buttons="yes" wrap="yes"]' ); ?>
<h3><?php the_title(); ?></h3>
<?php the_content(); ?>
</div>
</div>
<?php endwhile;
wp_reset_postdata(); ?>
Use implode in combination with an array_map, to return an array of IDs, and then convert it to a string of IDs, separated by a comma.
So instead of this:
foreach ( $attachments as $attachment ) {
$thumbimg = wp_get_attachment_url( $attachment->ID, 'full', false );
$attachment_id = attachment_url_to_postid( $thumbimg );
echo $attachment_id; //here outputs the attachemnts ID's of current post
}
Try this:
$attachment_ids = implode(',', array_map(function($attachment){
return $attachment->ID;
}, $attachments));
echo $attachment_ids;

Retrieving post images from wordpress

my problem is that this code below retrieve images as attachments only for first four posts. For others it retrieves only post title. And each end every post is equal and stored same way in database and in backend of a wordpress site. And it retrieves first four and image of I think 16th post.
$myposts = get_posts(array(
'category' => $_POST["kategorija"],
'post_type' => 'post',
'posts_per_page' => -1
)
);
?>
<ul>
<?php
foreach ( $myposts as $post ) : setup_postdata( $post );
$title = $post->post_title;
$date = $post->post_date;
$content = $post->post_content;
$status = $post->post_status;
?>
<li>
<?php
$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, 'thumbnail' );
}
}
?>
<h2><?php echo $title; ?> </h2>
<form enctype="multipart/form-data" action="oglas.php" method="POST">
<input type="hidden" name="idKategorije" value="<?php echo $post->ID; ?>" />
<input type="submit" value="selektuj" />
</form>
</li>
<?php
endforeach; ?>
</ul>
Well since you already get the posts in your first get_posts, I don;t see why you need another one.
This is from this page in the Codex:
====
Show attachments for the current post[edit]
Do this inside The Loop (where $post->ID is available).
<?php
$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 ) {
echo apply_filters( 'the_title' , $attachment->post_title );
the_attachment_link( $attachment->ID , false );
}
}
?>

Wordpress - display Media Library items based on category

Just like the title says, I'm trying to display only the items in the Media Library that are under a particular category. Whether they're attached to anything or not.
Currently I can get all images, but I'm not sure how to narrow it down to certain categories.
Here's what I have so far:
<select name="event-dropdown" onchange="document.location.href=this.options[this.selectedIndex].value;">
<option value=""><?php echo esc_attr(__('Select Event')); ?></option>
<?php
$args = array(
'hide_empty' => 0,
);
$categories = get_categories($args);
foreach ($categories as $category) {
$option = '<option value="?cat='.get_cat_ID($category->cat_name).'">';
$option .= $category->cat_name;
$option .= ' ('.$category->category_count.')';
$option .= '</option>';
echo $option;
}
?>
</select>
<?php
$query_images_args = array(
'post_type' => 'attachment',
'post_mime_type' =>'image',
'post_status' => 'inherit',
'posts_per_page' => -1,
);
$query_images = new WP_Query($query_images_args);
if($_GET['cat']){
// not sure what to do here yet
}else{
// this part works fine
foreach ( $query_images->posts as $image) {
echo wp_get_attachment_image($image->ID);
}
}
?>
Can someone enlighten me on how/if this can be done. All I've been able to find is stuff relating to attached images or post images. I just want to pull them directly from the Library.
EDIT Tags would work too. It doesn't have to be category.
unless you're adding a custom meta tag to each image in your media library, your query won't work.
A possible solution
e.g. create a post, name it something relevant, tag all the categories you want this post to be associated with, now dump all the images you want to show into this post.
you can use this query to bump out the post & category terms you want to show, along with the images attached to the post(s).
<?php
$args = array (
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => 'your-cat-name-here'
)
)
);
$custom_query = new WP_Query( $args );
if ( $custom_query->have_posts() ):
while ( $custom_query->have_posts() ) :
$custom_query->the_post();
// Do stuff with the post content.
$attachments = get_posts( array(
'post_type' => 'attachment',
'posts_per_page' => -1,
'post_parent' => $post->ID,
'exclude' => get_post_thumbnail_id()
) );
if ( $attachments ) {
foreach ( $attachments as $attachment ) {
$class = "post-attachment mime-" . sanitize_title( $attachment->post_mime_type );
$thumbimg = wp_get_attachment_link( $attachment->ID, 'thumbnail-size', true );
$image_title = $attachment->post_title;
$caption = $attachment->post_excerpt;
$description = $image->post_content;
//print_r($attachment);
echo '<div class="thumbnail"><figure><img src="'.wp_get_attachment_url($attachment->ID).'" /></figure></div>';
}
//End
echo '</div>';
}
endwhile;
else:
// we can insert something if Nothing found.
echo "<h2>Sorry, but there's nothing here.</h2>";
endif;
wp_reset_query();
?>

How can I get the image url in WordPress?

I'm using this code to get all images uploaded to a page and it's working fine but I'd like to use the image urls for a lightbox function and currently I'm getting an img object. Is there any way I can get the image url specifically? I'd like to think it's part of an array. Here's the code I'm using:
<?php
$images = get_children( array(
'post_parent' => $post->ID,
'post_type' => 'attachment',
'post_mime_type' => 'image',
'orderby' => 'menu_order',
'order' => 'ASC',
'numberposts' => 999 ) );
if ( $images ) {
//looping through the images
foreach ( $images as $attachment_id => $attachment ) {
?>
<li>
<a href="*the_img_url*" data-lightbox="lightbox-1" data-title="<?php echo $attachment->post_excerpt; ?>" ><?php echo wp_get_attachment_image( $attachment_id, 'full' ); ?>
</a>
</li>
<?php
}
}
?>
Thank you.
Have you tried
<?php echo wp_get_attachment_url( $attachment_id ); ?>
// $attachment_id is The ID of the desired attachment

Displaying posts thumbnails in Wordpress show only first thumbnail

I want to display on the sidebar the latest posts title and thumbnail.
So far I'm getting the posts title and only one thumbnail duplicated.
You can see the result here.(only the first/oldest post image displaying)
Here is my code:
$rps = wp_get_recent_posts($params);
foreach($rps as $rp) :
$args = array(
'post_type' => 'attachment',
'numberposts' => 1,
'post_status' => null,
'post_parent' => $post->ID
);
$attachment = current(get_posts( $args ));
?>
<?php echo $rp['post_title'];?><?php echo wp_get_attachment_image( $attachment->ID, 'thumbnail' );?>
<?php endforeach; ?>
Thanks for any tips/assistance given.
Replace 'post_parent' => $post->ID with 'post_parent' => $rp['ID'] . That's it.
What you are doing is, you are passing current post's ID in $args for all posts.
run 2 queries
one outputs the first post. The second outputs everything else excluding the first
<?php $args = array( 'post_type' => 'attachment', 'posts_per_page' => 1, 'post_parent' => $post->ID);
$first = new WP_Query( $args );
while ( $first->have_posts() ) : $first->the_post(); ?>
<?php the_title();?><?php echo wp_get_attachment_image( $first->ID, 'thumbnail' );?>
<?php endwhile; wp_reset_postdata(); ?>
<?php $args2 = array( 'post_type' => 'attachment', 'posts_per_page' => 1, 'post_parent' => $post->ID, 'offset' => 1);
$rest = new WP_Query( $args2 );
while ( $rest->have_posts() ) : $rest->the_post(); ?>
<?php the_title();?><?php echo wp_get_attachment_image( $rest->ID, 'thumbnail' );?>
<?php endwhile; wp_reset_postdata(); ?>

Categories