• http://johnford.is/programmatically-pull-attachments-from-wordpress-posts/

    I want to place a random image from any of the listed categories. Would there be anyway possible you could help me achieve this?

    Example.
    I have several posts and they all are in categories.
    Categories consist of: Blog, Work, Extras, Tutorials,

    I want to be able to pull a single random image from “Work, Extras, Tutorials” and then place that one image in my sidebar.

    I’m just not sure how to create or modify the function to achieve this.
    Keep in mind that the image being pulled would then need to be the thumbnail version.

    Thanks

Viewing 1 replies (of 1 total)
  • Thread Starter JokerMartini

    (@jokermartini)

    So this works.

    <?php
    $args = array(
    'category' => 9,
    'numberposts' => 1,
    'orderby' =>
    'rand' );
    $rand_posts = get_posts( $args );
    foreach( $rand_posts as $post ) : ?>
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php endforeach; ?>

    But how would i make it so it selectes an image from either category 9 or 4. Not just 1??

    Then how do i return the image with the permalink?

Viewing 1 replies (of 1 total)

The topic ‘Random gallery image in sidebar’ is closed to new replies.