I have snippet to displaying atachment in single cpt, but i dont now how to get the atachment image size in the code, lets say.. i want to add a thumbnail size to the image, but i dont know how..
this is the code that i used
<?php $args = array(
'numberposts' => -1, // Using -1 loads all posts
'orderby' => 'menu_order', // This ensures images are in the order set in the page media manager
'order'=> 'ASC',
'post_mime_type' => 'image', // Make sure it doesn't pull other resources, like videos
'post_parent' => $post->ID, // Important part - ensures the associated images are loaded
'post_status' => null,
'post_type' => 'attachment',
);
$images = get_children( $args ); if($images){ ?>
<div id="projectGallery">
<?php foreach($images as $image){ ?>
<img src="<?php echo $image->guid; ?>" alt="<?php echo $image->post_title; ?>" title="<?php echo $image->post_title; ?>" />
<?php} ?>
</div>
<?php } ?>
please help.. thx
Please follow the code below. It will help you.
<?php
$size = thumbnail, medium, large or full
$args = array(
'numberposts' => -1,
'orderby' => 'menu_order',
'order'=> 'ASC',
'post_mime_type' => 'image',
'post_parent' => $post->ID,
'post_status' => null,
'post_type' => 'attachment',
);
$images = get_children( $args );
if($images){
?>
<div id="projectGallery">
<?php
foreach($images as $image){
wp_get_attachment_image($image->ID, 'thumbnail'); //you can give thumbnail, medium, large or full according to your choice
<?php } ?>
</div>
<?php } ?>
You can use
wp_get_attachment_thumb_url( $attachment_id );
as follow:
<img src="<?php echo wp_get_attachment_thumb_url( $image->ID ); ?>" alt="<?php echo $image->post_title; ?>" title="<?php echo $image->post_title; ?>" />
Related
I am using get_pages to fetch some data from the children pages of a parent in WordPress (like a custom loop) - but it doesnt work when trying to fetch some data as set by the Advanced Custom Fields plugin for some strange reason... Is there an alternative / better way to acheive what I want? Code below works apart from fetching the ACF field called 'job_title'.
<?php
$args = array(
'parent' => $post->ID,
'post_type' => 'page',
'numberposts' => -1,
'post_status' => 'publish',
'sort_order' => 'DESC',
'sort_column" => "post_name',
'orderby' => 'menu_order',
'order' => 'ASC'
);
$pages = get_pages($args); ?>
<div class="childrenFetchedLoopWrapper">
<?php foreach( $pages as $page ) { ?>
<div class="feedItemWrapper wpb_animate_when_almost_visible wpb_fadeInUp fadeInUp" style="background-image: url('<?php echo get_the_post_thumbnail_url($page->ID, 'full'); ?>')">
<a href="<?php echo get_permalink($page->ID); ?>" rel="bookmark" title="<?php echo $page->post_title; ?>">
<img src="/wp-content/themes/salient-child/images/aspectTrans.png" alt="*" title="*" />
<h3><?php echo $page->post_title; ?></h3>
<p><?php the_field('job_title'); ?></p>
</a>
</div>
<?php } ?>
</div>
Replace <?php the_field('job_title'); ?> with <?php the_field('job_title', $page->ID); ?>.
OR
You can use WP_Query for alternative solution.
or you can also get acf value using get_post_meta();
Try Out this code. I hope it helps.
<?php
$args = array(
'post_type' => 'page',
'posts_per_page' => -1,
'post_parent' => $post->ID,
'order' => 'ASC',
'orderby' => 'menu_order'
);
$parent = new WP_Query( $args );
if ( $parent->have_posts() ) : ?>
<?php while ( $parent->have_posts() ) : $parent->the_post();
$id = get_the_ID(); ?>
<p><?php the_field('job_title', $id); ?></p>
<?php endwhile; ?>
<?php endif; wp_reset_postdata(); ?>
I have a CPT called books and I am able to loop through it like this but I need to get the URL of the uploaded images into the post (not in Gallery). I tried to use WP wp_get_attachment_image_src() as below but I do not know what should I pass for $attachment_id as it is required
<?php
$loop = new WP_Query(array(
'post_type' => 'books',
'tax_query' => array(
array(
'taxonomy' => 'genre',
'field' => 'slug',
'terms' => 'romance'
)
)
)
);
while ($loop->have_posts()):
$loop->the_post();
$image_attributes = wp_get_attachment_image_src('', 'full' );
if ( $image_attributes ) : ?>
<img src="<?php echo $image_attributes[0]; ?>" width="<?php echo $image_attributes[1]; ?>" height="<?php echo $image_attributes[2]; ?>" />
<?php endif;
endwhile;
wp_reset_query();
?>
Apparently this is not returning any of images uploaded to the CPT URL. What am I doing wrong here?
Please Replace with Your Functon.
<?php
$loop = new WP_Query(array(
'post_type' => 'books',
'tax_query' => array(
array(
'taxonomy' => 'genre',
'field' => 'slug',
'terms' => 'romance'
)
)
)
);
while ($loop->have_posts()):
$loop->the_post();
//used "get_post_thumbnail_id($loop->ID)" perameter
$image_attributes = wp_get_attachment_image_src(get_post_thumbnail_id($loop->ID), 'full' );
if ( $image_attributes ) : ?>
<img src="<?php echo $image_attributes[0]; ?>" width="<?php echo $image_attributes[1]; ?>" height="<?php echo $image_attributes[2]; ?>" />
<?php endif;
endwhile;
wp_reset_query();
?>
I've been through other posts on here and elsewhere and compared my code to working examples I've done previously, but I can't find what the issue is. I;m using the following query to grab a featured article and store its ID:
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => 1,
'orderby' => 'date',
'order' => 'desc',
'post_status' => 'publish',
'cat' => 564,
);
$featured_latest = new WP_Query($args);
$fid = array();
if( $featured_latest->have_posts() ) : while( $featured_latest->have_posts() ) : $featured_latest->the_post(); ?>
<?php $fid[] = get_the_ID(); ?>
<div class="top-featured">
<?php if( has_post_thumbnail() ) { ?>
<?php $image = get_the_post_thumbnail_url(get_the_ID(),'large'); ?>
<?php } else { ?>
<?php $image = get_stylesheet_directory_uri() . '/assets/images/blog/no-article-image.jpg'; ?>
<?php } ?>
<a class="article-link lazy" href="<?php the_permalink(); ?>" data-src="<?php echo $image; ?>">
<div class="text">
<h2><?php the_title(); ?></h2>
<?php
$date = get_the_date();
$cdate = date( 'c', strtotime($date) );
?>
<time datetime="<?php echo $cdate; ?>"><?php echo $date; ?></time>
<div class="excerpt">
<?php echo get_excerpt(140); ?>
</div>
<span class="fake-link">Read article</span>
</div>
</a>
</div>
<?php endwhile; wp_reset_postdata(); endif; ?>
Then lower down the page I use this query:
<?php
if( !empty( $fid ) ){
$fid = $fid;
} else {
$fid = array();
}
$args = array(
'post_type' => 'post',
'posts_per_page' => 3,
'orderby' => 'date',
'order' => 'desc',
'post_status' => 'publish',
'cat' => 399,
'post__not_in' => $fid,
);
$top_reviews = new WP_Query($args);
if( $top_reviews->have_posts() ) : ?>
<div class="top-articles top-reviews">
<h2>Top product reviews</h2>
<div class="row">
<?php while( $top_reviews->have_posts() ) : $top_reviews->the_post(); ?>
<div class="col-12 col-md-4">
<?php if( has_post_thumbnail() ) { ?>
<?php $image = get_the_post_thumbnail_url(get_the_ID(),'large'); ?>
<?php $imgid = get_post_thumbnail_id( get_the_ID() ); ?>
<?php $alt = get_post_meta( $imgid, '_wp_attachment_image_alt', true); ?>
<?php } else { ?>
<?php $image = get_stylesheet_directory_uri() . '/assets/images/blog/no-article-image.jpg'; ?>
<?php $alt = 'No article image'; ?>
<?php } ?>
<a class="article-link" href="<?php the_permalink(); ?>" data-src="<?php echo $image; ?>">
<img class="lazy" data-src="<?php echo $image; ?>" alt="<?php echo $alt; ?>">
<div class="text">
<h3><?php the_title(); ?></h3>
<?php
$date = get_the_date();
$cdate = date( 'c', strtotime($date) );
?>
<time datetime="<?php echo $cdate; ?>"><?php echo $date; ?></time>
<div class="excerpt">
<?php echo get_excerpt(140); ?>
</div>
<span class="fake-link">Read article</span>
</div>
</a>
</div><!-- col -->
<?php endwhile; wp_reset_postdata(); ?>
</div><!-- row -->
</div>
<?php endif; ?>
$fid prints as an array with one item inside, but the second query does not exclude the post of that ID. I'm sure there's something glaringly obvious that I'm missing, but I can't for the life of me find it!
May be some other plugin or theme features overrides the array? var_dump and try what are the query_vars in the WP_Query object. then you can identify it. Or simply deactivate all the plugins and activate it one by one. then you can identify surely!
if any plugin overrides it, they should use a hook to do it. so search and see the priority of the hook. and then just you override it with high priority hook. example below.
add_action('pre_get_posts', 'your_callback_function_name', 999)
function your_callback_function_name($query){
$overridden_array = $query->get('post__not_in', array());
$overridden_array[] = $your_f_id;
$query->set('post__not_in', $overridden_array);
}
Thats all. Just rename those functions and variables as your wish.
You need to pass array in this format array('162','3074')
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'orderby' => 'date',
'order' => 'desc',
'post_status' => 'publish',
'post__not_in' => array('162','3074')
);
$top_reviews = new WP_Query($args);
Its working fine for me.. you can try in same way.
post__not_in (array) - use post ids. Specify post NOT to retrieve. If this is used in the same query as post__in, it will be ignored.
Change post__not_in value into Array.
$args = array(
'post_type' => 'post',
'posts_per_page' => 3,
'orderby' => 'date',
'order' => 'desc',
'post_status' => 'publish',
'cat' => 399,
'post__not_in' => array('YOUR_POST_ID1','YOUR_POST_ID2'),
);
For more help see this link : Click Here
When printing out the WP_Query resuls, I found that the following was happening:
WP_Query Object ( [query] => Array ( [post_type] => post [posts_per_page] => 3 [orderby] => date [order] => desc [post_status] => publish [cat] => 399 [post__not_in] => Array ( [0] => 8117 ) ) [query_vars] => Array ( [post_type] => post [posts_per_page] => 3 [orderby] => date [order] => DESC [post_status] => publish [cat] => 399 [post__not_in] => Array ( [0] => 7990 )
It was suggested to me by dineshkashera that the pre_get_posts() filter may have been the cause. As I was not using this myself, I troubleshooted for plugin conflicts by deactivating all those that were not regular ones I used. After reactivating them one at a time, I found that the culprit was the Woocommerce Point of Sale plugin. I have deactivated this plugin for now and will seek a solution from the developer.
I'm trying to lazy load a page of images in Wordpress - I've tried the plugins but none of them seem to work(I only want to lazy load image on some pages.)
So I'm now trying to do it with a plugin - http://www.appelsiini.net/projects/lazyload
This plugin requires the image width and height in the img tag.
<img data-original=“img/example.jpg” src=“img/grey.gif” width=“640” height=“480”>
I'm attaching the images from custom fields like
<img src="<?php echo get_post_meta($post->ID, 'img1', true); ?>">
Is it possible get the images width and height from the get_post_meta
I've looked at wp_get_attachment_image_src but I can't see how to use it get_post_meta
======
UPDATE
======
<?php
$attachments = get_children(
array(
'post_parent' => $post->ID,
'post_status' => 'inherit',
'post_type' => 'attachment',
'post_mime_type' => 'image',
'order' => ASC,
'orderby' => 'menu_order ID'
)
);
?>
<?php
foreach ( $attachments as $attachment_id => $attachment ) {
$image_attributes = wp_get_attachment_image_src( $attachment );
?>
<img src="<?php echo $image_attributes[0];?>" width="<?php echo $image_attributes[1];?>" height="<?php echo $image_attributes[2];?>">
<?php
}
?>
Its better to use Wordpress Media Gallery support. You can get all images attached to post with:
<?php $attachments = get_children(
array('post_parent' => $id,
'post_status' => 'inherit',
'post_type' => 'attachment',
'post_mime_type' => 'image',
'order' => ASC,
'orderby' =>
'menu_order ID') ); ?>
where $id is a current post ID. Then with the ID's of attached images, You can simply use:
<?php foreach ( $attachments as $attachment_id => $attachment ) {
(...)
}?>
to iterate throught image Id's, and
<?php wp_get_attachment_image_src($attachment_id, $size, $icon);>
which is described: http://codex.wordpress.org/Function_Reference/wp_get_attachment_image_src, that allows You not only get url of image, but also its width and height.
The whole code can be wrapped in a shortcode function for ease of use ;).
Having a little trouble here, I'm wanting to get all images from posts and display them in two separate locations, one location showing one image and the second location showing all the image.
If i use the Query it grabs all post image and with out using Query it grabs one image from the page. any ideas on how to get all images from page instead of posts???
<div id="fp_gallery" class="fp_gallery">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php $args = array( 'numberposts' => 1, 'post_type' => 'attachment', 'orderby' => 'menu_order', 'order' => 'ASC', 'post_mime_type' => 'image' ,'post_status' => null, 'post_parent' =>
$post->ID ); $attachments = get_posts($args); if ($attachments) { foreach ( $attachments as $attachment ) { ?>
<img src="<?php echo wp_get_attachment_url( $attachment->ID , false ); ?>" alt="" class="fp_preview" style=""/>
<?php }}?>
<?php endwhile; endif; ?>
<div class="fp_overlay"></div>
<div id="fp_loading" class="fp_loading"></div>
<div id="fp_next" class="fp_next"></div>
<div id="fp_prev" class="fp_prev"></div>
<div id="outer_container">
<div id="thumbScroller">
<div class="container">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php $args = array( 'numberposts' => 9999, 'post_type' => 'attachment', 'orderby' => 'menu_order', 'order' => 'ASC', 'post_mime_type' => 'image' ,'post_status' => null, 'post_parent' => $post->ID ); $attachments = get_posts($args); if ($attachments) { foreach ( $attachments as $attachment ) { ?>
<div class="content">
<div>
<a href="#" title="<?php the_title(); ?>">
<img src="<?php echo get_bloginfo('template_url');?>/js/timthumb.php?src=<?php echo wp_get_attachment_url( $attachment->ID , false ); ?>&h=120&w=150&zc=1" alt="<?php echo wp_get_attachment_url( $attachment->ID , false ); ?>" class="thumb"/>
</a>
</div>
</div>
<?php }}?>
<?php endwhile; endif; ?>
</div>
</div>
</div>
</div>
<div id="fp_thumbtoggle" class="fp_thumbtoggle" title="View Thumbs">↑</div>
Any help would be great : )
Make numberposts -1 it will collect all posts under category
$args = array( 'numberposts' => 1, 'post_type' => 'attachment', 'orderby' => 'menu_order', 'order' => 'ASC', 'post_mime_type' => 'image' ,'post_status' => null, 'post_parent' =>
$post->ID );
But i'm not sure about jQuery thing you are talking about.