Regardless, if you want images from pages besides the current, you first need to query for the pages with get_posts()
. You can specify the query only return published pages that are not a particular ID (of the current page). The query can be limited to returning 5 pages and ordered randomly.
Once you have the 5 pages, your code will work with some minor adjustment of adding a loop to step through the 5 pages, get the meta value of each which in turn allows you to get the image src URL for output.
]]><?php
$query = get_posts( array(
'post_type' => 'page',
'post__not_in' => array( get_the_id(),), //should be ID of main query page
'posts_per_page' => 5,
'orderby' => 'rand',
));
global $post;
foreach ( $query as $post ) {
setup_postdata( $post );
// your code goes here (get_the_id() returns local $post ID here)
// don't include the first and last <?php ?> tags - no nesting of code blocks allowed
wp_reset_postdata();
}
I’m adjusting my work flow, hopefully I won’t miss anymore replies.
]]>Actually, I just want to display those pages which contains image on field named “image1”. This is my sidebar. I used your code like this:
<div class="destinations">
<ul class="news">
<?php
$query = get_posts( array(
'post_type' => 'page',
'post__not_in' => array( get_the_id(),), //should be ID of main query page
'posts_per_page' => 5,
'orderby' => 'rand',
));
global $post;
foreach ( $query as $post ) {
setup_postdata( $post );
$id = get_the_id();
$image_id = get_post_meta($id, 'image1', true);
if(get_post_meta($image_id, 'image1', true)){
$attachment = wp_get_attachment_image_src($image_id, 'full', false); // it gives array having first parameter source, second parameter width and third parameter height with image sizes: thumbnail, medium, large and full.
if(count($attachment) > 0) {
?>
<li class="button"><a href="<?php the_permalink(); ?>"><?php echo get_the_title(); ?></a>
<li class="dropdown active">
<a href="<?php the_permalink(); ?>"> <div class="destinations-grid">
<img src="<?php echo $attachment[0]; ?>" class="img-responsive" alt=""/> </div>
</a>
</li>
</li>
<?php
}
}
wp_reset_postdata();
}
?>
</ul>
</div>
But things didn’t work. It sometimes show one image while refreshing. If it had showed 5 images randomly then it would have become great.
But you know I just tried by removing this line
if(get_post_meta($image_id, 'image1', true)){
then it displayed 5 random pages but not worth because it also displayed home page and other pages which doesn’t contain ‘image1’ field.
Oh I am really in a big trouble. Please guide me to come out of this.
]]>'meta_query' => array( array(
'key' => 'image1',
'compare' => 'EXISTS',
),),
That should take care of things. If not let me know. FYI, I’m currently traveling so my replies may be even more erratic than before. Be assured that I’m not ignoring you.
]]>'meta_query' => array( array(
'key' => 'image1',
'meta_value' => '',
'compare' => '!=',
),),
I think that should do it, but I’ve been wrong so far ??
If it doesn’t work, hang in there, we’re zeroing in on it. Thank you for your patience.