WordPress.org

Ready to get started?Download WordPress

Forums

post type and category not working together in query (2 posts)

  1. gavsiu
    Member
    Posted 3 years ago #

    What I am trying to do is be able to find a random attachment in the category.php page belonging to one of the posts in that category and also be able to find a random attachment in the single.php belonging to that post.

    What I have been able to get so far is to find a random attachment in a single post.

    $images =& get_children( array(
    	'post_parent' => $post -> ID,
    	'post_type' => 'attachment',
    	'post_mime_type' => 'image',
    	'orderby' => 'rand',
    	'numberposts' => 1
    ) );
    foreach ( (array) $images as $attachment_id => $attachment ) :
    	echo wp_get_attachment_url( $attachment_id ) . '<br>';</script>';
    endforeach;

    I tried adding 'cat' => 1, but it doesn't display anything.

    I tried following the example http://codex.wordpress.org/Template_Tags/get_posts#Random_posts and output the titles to test it. I set post_type to attachment and it shows all the attachments in all categories. I set cat to 1 and it does not show anything (I know I have 1 attachment in 1 of the posts there). I delete post_type and it shows the post titles for cat 1.

  2. gavsiu
    Member
    Posted 3 years ago #

    Ignore the </script>'; There was more to the code and I missed it when I deleted the rest.

Topic Closed

This topic has been closed to new replies.

About this Topic