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;
Related
I am trying to get the categories in a post like this example if the post is added in cate1, cate2, and cate3 then the post-show 3 categories like cate1, cate2, cate3 if the post is posted in one category then just show the cate1 without a comma. But my code $slug = $category->slug; only shows one category in the post. here is my full code.
<?php
$mi_args = array(
'post_status' => 'publish',
'posts_per_page' => '99999',
'post_type' => 'blog'
);
$mi_query = new WP_Query($mi_args);
if($mi_query->have_posts()):
while ($mi_query->have_posts()): $mi_query->the_post();
$portfolio_url = get_post_meta( get_the_ID(), 'portfolio_url_portfolio-url', true );
$hover_color = get_post_meta( get_the_ID(), 'hove_color_hover-color', true );
// Getting the category slug
$work_category = get_the_terms( get_the_ID(), 'blog_categories' );
foreach ($work_category as $category)
{
$slug = $category->slug;
?>
<div class="portfolio-item <?php echo $slug; ?>" data-category="<?php echo $slug; ?>">
<?php
}
?>
<?php endwhile; endif; wp_reset_postdata(); ?>
If I understand your question, you want to take an array of category slugs, and put them as a comma separated list?
If that's right, then you want to push your slugs to an array, and then implode it to a list.
<?php
$mi_args = array(
'post_status' => 'publish',
'posts_per_page' => -1,
'post_type' => 'blog',
);
$mi_query = new WP_Query( $mi_args );
if ( $mi_query->have_posts() ) :
while ( $mi_query->have_posts() ) :
$mi_query->the_post();
$portfolio_url = get_post_meta( get_the_ID(), 'portfolio_url_portfolio-url', true );
$hover_color = get_post_meta( get_the_ID(), 'hove_color_hover-color', true );
// Getting the category slug.
$work_category = get_the_terms( get_the_ID(), 'blog_categories' );
$slug = array();
foreach ( $work_category as $category ) {
$slug[] = $category->slug;
}
$slug_list = implode( ',', $slug );
?>
<div class="portfolio-item <?php echo esc_attr( $slug_list ); ?>" data-category="<?php echoesc_attr( $slug_list ); ?>">
<?php
endwhile;
endif;
wp_reset_postdata();
?>
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 );
}
}
?>
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();
?>
I'm trying to get the attachments of a specific term (in its archive page).
But the results are showing the resulting images 5 times instead of one.
I have multiple loops in this page - one to show related posts, another to show related products (custom post), and this one to show related images. Custom posts and posts are working nicely, but I can't show the attachments in the right way. :S
<?php $queried_object = get_queried_object();
$term_id = $queried_object->term_id;
$args = array(
'post_status' => 'inherit',
'numberposts' => 0,
'post__not_in' => array_merge($do_not_duplicate,get_option( 'sticky_posts' )),
'post_type' => 'attachment',
);
$args['tax_query'] = array(
array(
'taxonomy' => 't-arte',
'terms' => $term_id,
'field' => 'id',
),
); ?>
<?php $t = $data['t-arte'];
$array = explode(" ", $t);
$array = array_unique($array);?>
<?php $media_query = array_unique($array); ?>
<?php $media_query = get_posts($args);
if( !empty( $media_query ) ) :
foreach ($media_query as $media_query) :
global $post; $post = $media_query;
setup_postdata($media_query);
?>
<div id="archivespage-media-item">
<?php $attachments = get_posts( $args );
if ( $attachments ) {
foreach ( $attachments as $attachment ) {
echo '<div id="imagem">';
the_attachment_link( $attachment->ID, true );
echo '</div>';
}
}?>
</div>
<?php endforeach;else :?>
<p>Ainda não temos nenhuma imagem relacionada :(</p>
</div>
<?php endif; ?>
<?php wp_reset_query();?>'
I've done these and its working!
The result will show all attachments in a specific term inside term's archive page.
<?php $queried_object = get_queried_object();
$term_id = $queried_object->term_id;
global $wp_query;
$original_query['tax_query'] = array(
array(
'taxonomy' => 't-arte',
'terms' => $term_id,
'field' => 'id',
),);
$original_query = (array) $wp_query;
$attach_query = array(
'post_type'=> array( 'attachment' ),
'post_status' => array( null ));
$args = array_merge($original_query['query_vars'], $attach_query);
$media_query = new WP_Query( $args )?>
<?php if($media_query->have_posts()) :
while ($media_query->have_posts() ) : $media_query->the_post();
if( $post->ID == $do_not_duplicate ) continue; ?>
<div id="archivespage-media-item">
<div id="imagem">
<?php echo wp_get_attachment_link($attachment->ID, 'bigger-thumb');?>
</div>
</div>
<?php endwhile; else: ?>
//do stuff
</div>
https://wordpress.stackexchange.com/questions/29635/how-to-create-an-attachments-archive-with-working-pagination
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; ?>