Support » Plugins » Hacks » How to display and limit the custom image on sidebar?

Viewing 9 replies - 1 through 9 (of 9 total)
  • Moderator bcworkz

    (@bcworkz)

    I’m not sure I follow. I think you want to display the image1 image of 5 random pages that are not the current page in the sidebar. Am I correct?

    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.

    Can you please write the exact code for me?

    Hello bcworkz,
    Still waiting for your response…
    Can you please write the exact code for me?

    Moderator bcworkz

    (@bcworkz)

    Sorry, I’ve been missing replies right and left since the forums migrated and new replies no longer float to the top of the list. I wouldn’t say this is exact code because it’s untested, but outside of possibly some dumb typos it should be OK.

    <?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.

    Hello bcworkz,
    Thanks for your reply. I was quite frustrated googling my problem. Because of this problem my website is stuck. I don’t know how will I overcome this problem.

    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.

    Moderator bcworkz

    (@bcworkz)

    Nah, it only seems like big trouble. I was kind of hoping all of your pages had an image specified. Wishful thinking. No worries, if we do the query right we won’t need that if get post meta line anyway. Add the following right after ‘orderby’ => ‘rand’,:

    '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.

    Thankyou for your help and co-operation. I added your query as instructed and removed this portion ` if(get_post_meta($image_id, ‘image1’, true)){‘ but it displayed all 5 pages randomly in which image1 field is empty. I want to make you clear that my all pages includes image1 field but I just want to display only those pages where image1 field is not empty.

    Moderator bcworkz

    (@bcworkz)

    Ah, OK. Try this variant of the previous meta_query argument instead.

    '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.

    Aah at last it worked. I just removed meta from ‘meta_value’ and it worked. Thanks… 🙂

    • This reply was modified 4 years, 4 months ago by jklyn.
Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘How to display and limit the custom image on sidebar?’ is closed to new replies.