WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Problem Building Image Slider (5 posts)

  1. seansean11
    Member
    Posted 1 year ago #

    Yea, I know I can just download a plug-in, but I'm trying to improve my development skills. What I'm trying to do is set up an image slider of just pictures from posts that belong to a certain category. I started out using this tutorial: Building A Custom Image Slider. I am using the code from this tutorial which I believe is pretty basic:

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post();
    
    						$args = array(
    						 'post_type' => 'attachment',
    						 'numberposts' => -1,
    						 '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 ) {
    						 echo wp_get_attachment_image($attachment->ID , 'full' );
    						 }
    						}
    						endwhile; endif; ?>

    I thought that I could just take this code and add the 'category' parameter for the category of the posts that I want to pull the images from. In this case it would be 'category' => 9. This is the part that's got me very confused... without the category parameter all of the attached images from all posts (I believe) are being displayed. When I add the category (which is the correct id #), no images are displayed.

    Maybe I'm using the wrong technique, but I'd appreciate any sort of feedback to give me more direction.

    Thank You,
    Sean

  2. datdesignguy
    Member
    Posted 1 year ago #

    Are you attempting to build a featured posts style slider or a slider that will display only images attached to a specific page?

    The tutorial you linked to above is intended to demonstrate how to build an image slider for display on a specific post/page by building a query (using the code above) that pulls the attachments specifically for that page.

    The reason the above code is displaying nothing when you attempt to add a category argument is most likely because attachments don't get category assignments.

    If you would like to build a slider for a home page, where it displays the featured images for a select group of posts, you'll need different code, I'm afraid.

    Let us know, and I'll see if I can help you out

    -greg

  3. seansean11
    Member
    Posted 1 year ago #

    Thanks so much @datdesignguy. My ideal situation would be to make a featured image slider from posts in a specific category. I just assumed, seeing that "category" was a potential parameter for the get_posts array, that I could pull all of the attachment images from posts of a certain category using this query (despite the fact that the tutorial is for a single page/post).

    So, I ideally would like to have a featured image slider. I'm probably going to duplicate this code on the same index.php file because I've created a one page portfolio with various sections. There will be 2 sliders. The first will be just a feature image slider (for the posts of one category), the second will be content, featured image and title (for the posts of one category).

    I'll be spending the day working on wordpress loop tutorials/studying Rockstar WordPress to see if I can get a start on this. Please get back to me if you see the message.

    Thanks Again,
    Sean

  4. seansean11
    Member
    Posted 1 year ago #

    Ok so where I'm at now I am now getting what I want. A simple output of featured images from a certain category. Here is my code

    <?php
    						query_posts(array('category_name' => 'las-bufandas', 'posts_per_page' => 3));
    						if(have_posts()) :
    						    while(have_posts()) : the_post();
    						?>
    						  <li>
    							<?php the_post_thumbnail(); ?>
    							<p><?php the_excerpt(); ?></p>
    						  </li>
    						<?php
    						    endwhile;
    						endif;
    						wp_reset_query();
    						?>

    I know that since this isn't the main loop of the page (I actually don't have one as it is a 1-page theme with various sections and acts more as a cms than a blog), that I should be using WP_query instead of query_posts with a reset. I haven't really figured that out yet, but at least what I have is working and it is so simple!

  5. datdesignguy
    Member
    Posted 1 year ago #

    hi Sean,

    Great Work finding what you needed.

    For the instance you described, it is totally permissible for you to simply use query_posts... as you said, there are no other loops on this page.

    However, I definitely recommend you run through the paces of using WP_Query as well. It's not only super-handy, but sometimes, absolutely necessary for more advanced WordPress situations.

    Sorry it took a while to get back to you! When you read this, could you please mark the problem as resolved?

    Thanks,

    Greg

Topic Closed

This topic has been closed to new replies.

About this Topic