Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Creating a loop to show sticky posts in a featured slider

From: WordPress: Building Responsive Themes

Video: Creating a loop to show sticky posts in a featured slider

In the previous movie, we used the FlexSlider JavaScript library to add an advanced responsive featured-content letter on our front page. But right it's still running our dummy content. The next and obvious step is to add featured content from our own site to the slider. The way we are going to do that is by creating a custom template file that has a separate loop that will loop through only the sticky posts inside WordPress and then display them in the FlexSlider.

Creating a loop to show sticky posts in a featured slider

In the previous movie, we used the FlexSlider JavaScript library to add an advanced responsive featured-content letter on our front page. But right it's still running our dummy content. The next and obvious step is to add featured content from our own site to the slider. The way we are going to do that is by creating a custom template file that has a separate loop that will loop through only the sticky posts inside WordPress and then display them in the FlexSlider.

So before we do that, we need to make some of our posts sticky. I'll go up to the backend, go to Posts > All Posts, and then just select some random posts here. I'll select these four, go to Bulk Actions and Edit and from here, I can set the Posts to Sticky. Sticky posts are supposed to always appear at the top of the index page, and you can also use them for things like a featured-content slider.

Now that I have set some sticky posts--and you can see that they are sticky because they say so right here on the index page--we can create a function that hooks into the sticky posts and then puts those posts into the featured slider. So first, I am going to create a new file. I'll save it inside my theme, and I'll call it featureflexslider.php. And I'll make sure that this is a PHP file. And then I am going to go to my code_snippets and copy out this custom code that I have created for you.

Paste it into the document, save it, and then I'll quickly explain what's going on. At the top here, we have a comment. It's put inside php bracket so that it won't appear inside the site itself, and then we are creating this < div class="flexslider"> and the

    . You will notice this matches exactly what we had in our dummy content here,
    ,
      . Then we create a loop. First, we tell WordPress to track all the sticky posts and put them into a variable we call sticky.

      Then we say we want six posts maximum and we only want the posts that appear inside that variable, sticky, that we got previously. Then we go to WordPress and say we now want to have a new query, and a query is basically when you go to the database and say give me all your posts and I'll do some stuff with them. Then we say, for each of the results that come out of this query, do the following. If it has a post_thumbnail--so that would be the featured image--create a list item, and within that list item create a link that goes to the post for that thumbnail, then show us the thumbnail and put in the caption with the title of that post. Close the link, close the list item, close for each, and then the loop goes back to the top.

      It says, are there more posts? Okay, and it goes through the process again. The reason why we have this if ( has_post_thumbnail () is because this being a featured content slider requires an image; if we don't have an image, it will look really strange. Then the function ends with a closing of the unordered list and a closing of the div. But this wasn't placed inside a file that we are using, so now we have to call this function inside our index file. So I'll go back to my index file and remove this demo content, because we don't need it anymore, and in place of the demo content, we are going to call in this featureflexslider.php file.

      So I'll use a function that comes with WordPress. It is called get_template_part, and it'll automatically look through the entire folder for the current template or theme for a file with whatever name we put in here. So this file is called featureflexslider, and I'll close the php delimiters. Now when I save this and reload the front page, you will see that we now have the featured images up here at the top.

      However, you'll also see they look really strange. That's because we are currently calling an image size that wasn't there when we originally added the content. This is a common problem you see a lot in WordPress where you switch to a new theme and then all of a sudden all your images have different weird shapes. So before we go any further, we need to use a plug-in to resize these images to fit with the current theme. So I'll go to my Dashboard, go to Plugins > Add New Plugins, I'll search for Regenerate Thumbnails, click Install Now, to install the plugin, activate the plugin, and once it's installed, I'll go up to Tools > Regenerate Thumbnails and run the plugin.

      The plugin will now go through all the images on my site and resize them to fit with all the predefined sizes we have inside the theme. One of those predefined sizes I've already set to the size we need for the FlexSlider, which means all those images will now be resized to fit in the FlexSlider, and we'll see a much better display once the process is done. The process is complete, and I go back to my front page and you now see that the FlexSlider looks much better because it has the properly sized images.

      And if you click on any of these, you jump directly to that post. But there's one more thing I want to do. You will notice that if we go down to the bottom here and we navigate to page 2, we again get the FlexSlider, and I only want the feature slider to appear on the front page. So if you go back to the code_snippets file, you will see that, here, instead of just having the call to get_template_part, I have created a function that you can use instead.

      So if you copy out that function and paste it into your index.php file, what this function does is it goes and checks, is this the front page, and if it is the front page, is it not paged, meaning is it not page 2 or 3 or 5 or 10? It has to be page 1. Only if both these conditions are accurate will it show the template part. If we save that, reload page 2, we no longer see the FlexSlider, but go back to page 1 and here we have the featured-content slider.

Show transcript

This video is part of

Image for WordPress: Building Responsive Themes
WordPress: Building Responsive Themes

39 video lessons · 16969 viewers

Morten Rand-Hendriksen
Author

 
Expand all | Collapse all
  1. 4m 5s
    1. Welcome
      1m 4s
    2. Using the exercise files
      1m 27s
    3. What you should know before watching this course
      1m 34s
  2. 8m 36s
    1. What is responsive design?
      4m 16s
    2. The different faces of WordPress on mobile
      1m 55s
    3. Exploring the finished Anaximander theme
      2m 25s
  3. 9m 38s
    1. Getting and installing the Anaximander theme
      1m 53s
    2. Configuring basic theme options
      7m 45s
  4. 23m 51s
    1. Deciding what screen sizes to design for
      4m 11s
    2. Thinking responsively: Designing for many different screen sizes
      6m 23s
    3. Visualizing content realignment for better markup
      4m 35s
    4. Designing menus
      4m 52s
    5. Adding responsive images and videos
      3m 50s
  5. 26m 8s
    1. What are media queries and how do they work?
      4m 18s
    2. Exploring CSS3, progressive enhancement, and graceful degradation
      3m 27s
    3. Understanding best practices for media queries
      3m 57s
    4. Creating a responsive frame
      5m 12s
    5. Customizing media queries with the Chrome Developer Tools
      5m 28s
    6. Taking device width into account
      3m 46s
  6. 11m 1s
    1. Resizing the site title and the description
      8m 22s
    2. Adding media queries to the header
      2m 39s
  7. 11m 22s
    1. Making the menu responsive
      3m 35s
    2. Creating a different menu design for small screens
      7m 47s
  8. 19m 22s
    1. Making a responsive single-post layout
      6m 11s
    2. Making images responsive
      4m 37s
    3. Making videos responsive by including FitVids
      8m 34s
  9. 7m 45s
    1. Making the sidebar responsive
      5m 10s
    2. Hiding sidebars on mobile
      2m 35s
  10. 7m 28s
    1. Dealing with footer widgets
      5m 11s
    2. Adding navigation links that return to the top of the page
      2m 17s
  11. 12m 54s
    1. Using FlexSlider to create a responsive slider
      6m 2s
    2. Creating a loop to show sticky posts in a featured slider
      6m 52s
  12. 24m 37s
    1. What is jQuery Masonry?
      3m 41s
    2. Installing jQuery Masonry
      4m 45s
    3. Configuring the index page with Masonry
      7m 0s
    4. Using CSS to finalize the Masonry layout
      6m 17s
    5. Adding media queries to the Masonry index
      2m 54s
  13. 9m 11s
    1. Exploring hidden features of the Anaximander theme
      5m 51s
    2. Where to go from here
      3m 20s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed WordPress: Building Responsive Themes.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

  • new course releases
  • newsletter
  • general communications
  • special notices

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

  • new course releases
  • newsletter
  • general communications
  • special notices

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.