Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Boris Kamp
Boris Kamp
16,660 Points

bootstrap carousel and wordpress loop without posts/pages

Hi guys,

is it possible to setup a bootstrap carousel without pages or post their featured images?

I already have one of those, but now I want one carousel with just plain images, without any links or whatsoever. How would I do this?

11 Answers

Brian Goldstein
Brian Goldstein
19,419 Points

Do you need the images pulled in dynamically?
If not, you can always just hard code the images in to your template. It's not ideal but it would work.

Otherwise I suggest using Custom Post Type UI and Advanced Custom Fields to create a special post type for this purpose. You're pretty much unlimited in what you can do with ACF.

Boris Kamp
Boris Kamp
16,660 Points

Thanks for your suggestion about the ACF and CPT UI. I was thinking about this myself as well, just to create an custom post type called 'gallery' and create galleries with different categories. Then I can loop them with the gallery post type and specific category. This sounds like a pretty easy and good way of achieving what I want right? Anybody against this?

Lewis Cowles
Lewis Cowles
74,902 Points

Might I suggest an alternative?

You can literally query Media that is an image is part of a specific category.

I would suggest setting a limit for the total number of slides and ordering desc on ID.

Lastly I know you have said no text, but using alt text will allow your marketing to reach a wider audience and is considered basic accessibility. Give this a go! The Wordpress codex can give you the code for the loop and it's a bit better than hard-coding images as you are still using the back-end of Wordpress to administer and manage the images.

http://codex.wordpress.org/Template_Tags/get_posts http://codex.wordpress.org/Function_Reference/get_children

Additionally, if you were to go one step further and make a shortcode to output the carousel, you could make a shortcode capable of embedding anywhere that supports shortcodes and you can call do_shortcode within your template, giving you your desired functionality, a shortcode carousel ability (potentially accepting categories to search) which you can use all over your site.

Lewis Cowles
Lewis Cowles
74,902 Points

Hi Boris, Images are not custom post types they are built in.

I am telling you if you upload images they are stored as posts within wordpress unless I have grossly misunderstood how this works. I Do have several plugins I have authored working under this premise so I am pretty sure I am right, but check it out in my link below. http://codex.wordpress.org/Post_Formats

Because the images are stored in the posts table it means we can query them just like any other post type, so you can retrieve images by category to put inside your slider.

Boris Kamp
Boris Kamp
16,660 Points

Hi Lewis!

Ah like that! Thanks for clarifying! I will look into this tomorrow and let you know how it worked out!

Boris Kamp
Boris Kamp
16,660 Points

Thanks for your input! Unfortunately I don't get what you are trying to explain in the part before your links. Can you please clarify?

Lewis Cowles
Lewis Cowles
74,902 Points

Good Luck Boris, post an update here after you have tried ;)

Boris Kamp
Boris Kamp
16,660 Points

Thanks Lewis. I looked into it and unfortunately did not figure it out myself, here are my question:

  1. How can you put an image you upload into a specific category? I find no such option.

  2. I have this code to pull in my current dynamic carousel:

    <!-- Carousel -->
    <?php
    
    $args = array(
      'post_type'     => 'kookworkshop'
    );
    $the_query = new WP_Query( $args );
    

?>

<div id="carousel-example-generic" class="carousel slide" data-ride="carousel"> <!-- Indicators --> <ol class="carousel-indicators"> <?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

  <li data-target="#carousel-example-generic" data-slide-to="<?php echo $the_query->current_post; ?>" class="<?php if( $the_query->current_post == 0 ):?>active<?php endif; ?>"></li>

  <?php endwhile; endif; ?>

</ol>

<?php rewind_posts(); ?>

<!-- Wrapper for slides --> <div class="carousel-inner">

<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

<div class="item <?php if( $the_query->current_post == 0 ):?>active<?php endif; ?>">

  <?php
    $thumbnail_id = get_post_thumbnail_id(); 
    $thumbnail_url = wp_get_attachment_image_src( $thumbnail_id, 'thumbnail-size', true );
    $thumbnail_meta = get_post_meta( $thumbnail_id, '_wp_attachment_image_alt', true);                
  ?>
  <a href="<?php the_permalink(); ?>"><img class="carousel-image" src="<?php the_field('frontpagesliderimage'); ?>" alt="<?php echo $thumbnail_meta; ?>"></a>
  <div class="carousel-caption"><?php the_title(); ?></div>
</div>

<?php endwhile; endif; ?>

</div>

I do not have the knowledge to figure out how to fully adjust this to query the images we talk about.

p.s. how do you learn all this stuff, I mean I understand biggest part of the above code, but only after reading the GIVEN code. I could never put this together myself...

Thanks!
Boris Kamp
Boris Kamp
16,660 Points

Whats wrong with the code in my post? I wrapped it in ``` on the first and last line

Lewis Cowles
Lewis Cowles
74,902 Points

Hi Boris!

Apologies I forgot to mention using the following code (we have this in our code, feel free to use, redistribute etc. (this goes within the functions.php of your theme / child-theme.

function add_categories_to_attachments() {
    register_taxonomy_for_object_type( 'category', 'attachment' );
}
add_action( 'init' , 'add_categories_to_attachments' );

this one is quite a simple script, it works based upon the post_type attribute within the database table [wp_]posts (this is equal to 'attachment' and the wp_ is in brackets because it represents the table prefix). This will make sure the categories comes up in the media. You can actually search and add categories programmatically anyway, but this makes it a little user friendly.

As for your code, lets have a walk through it briefly

You have two loops which initially just output the indicators by cycling through each post from your custom loop (results of pages / posts based upon a query, which is like a question).

$args = array( 'post_type' => 'kookworkshop' ); 

should be 'attachment' not 'kookworkshop' for images instead of the kookworkshop custom post type, and should have two additional parameters for category / categories by category id and limiting the number of posts (http://codex.wordpress.org/Class_Reference/WP_Query Does a great Job of explaining the Query). Alternatively if you have an interface etc and would rather use 'kookworkshop' than 'attachment', then update my functions.php edit to use 'kookworkshop' instead of 'attachment' and rename the function ;)

As for the image, using the attachment ID, which is the posts ID you can use

<?php echo get_the_title( $attachment_id ); /* Gets the title of the image or media (to be used within alt) */ ?>

combined with

<?php echo wp_get_attachment_url( $attachment_id ); /* Get's the url of the image or media */ ?>

As for how I know, I wouldn't panic, I am on Treehouse, mainly to review it and keep up to date, as I have two trainee staff that need to go over everything from 0 and in-house training would take too long. I Have been a developer working on the web and in traditional programming for over 12 years. Mostly my Wordpress knowledge comes from the codex.wordpress.org, Hope this helps and good luck!

Boris Kamp
Boris Kamp
16,660 Points

Thanks Lewis! Great help from you, I really appreciate it! I guess Im almost there but it's still not working. I got this now:

<!-- Carousel -->
    <?php

      $args = array(
        'post_type'     => 'attachment',
        'category__in'  => '14'
      );
      $the_query = new WP_Query( $args );

    ?>

  <div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
      <!-- Indicators -->
      <ol class="carousel-indicators">
        <?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

        <li data-target="#carousel-example-generic" data-slide-to="<?php echo $the_query->current_post; ?>" class="<?php if( $the_query->current_post == 0 ):?>active<?php endif; ?>"></li>

        <?php endwhile; endif; ?>
    </ol>

    <?php rewind_posts(); ?>

    <!-- Wrapper for slides -->
    <div class="carousel-inner">

      <?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

      <div class="item <?php if( $the_query->current_post == 0 ):?>active<?php endif; ?>">
        <a href="#"><img class="carousel-image" src="<?php echo wp_get_attachment_url( $attachment_id ); /* Get's the url of the image or media */ ?>" alt="<?php echo get_the_title( $attachment_id ); /* Gets the title of the image or media (to be used within alt) */ ?>"></a>
        <div class="carousel-caption"><?php the_title(); ?></div>
      </div>

      <?php endwhile; endif; ?>

    </div>

    <!-- Controls -->
    <a class="left carousel-control" href="#carousel-example-generic" data-slide="prev">
      <span class="glyphicon glyphicon-chevron-left"></span>
    </a>
    <a class="right carousel-control" href="#carousel-example-generic" data-slide="next">
      <span class="glyphicon glyphicon-chevron-right"></span>
    </a>
  </div><!-- End Carousel -->

My custom post type is called 'info' and the category ID of the images is '14'

What am I doing wrong? btw, how do I limit the number of posts? do I just have to enter

'post_limits' => '10' 

or something in the array?

Thanks again man!

Lewis Cowles
Lewis Cowles
74,902 Points

Hi Boris,

If you have a custom post type of info for your carousel items, it sounds like your not following. If by chance you are trying to get HTML slider elements, then please find and replace "attachment" with "info" (quotes show exact text to find and replace please do not include quotes). You will need different code to get the featured image url as well; Otherwise do not worry about the "info" custom post type (if it just holds the carousel). Please let me know if you are trying to include images by their category id in a custom post type (which would not matter), or if you are trying to include a custom post type as carousel items in wordpress.

;)

Boris Kamp
Boris Kamp
16,660 Points

Hi Lewis,

Sorry for my late reply, im very busy these days unfortunately! I created a custom post type called 'info' with the above code in the single-info.php code to pull up the carousel on every 'info' post type post by default. Im trying to pull in the pictures by category id. Is this the info you need? I think we had a misunderstanding.

Thanks again! really appreciate it!

Boris Kamp
Boris Kamp
16,660 Points

Lewis, You think you can help me out with this issue? we're getting close right?