Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Theme
Today I'm going to take you through integrating a responsive
jQuery slider into your WordPress theme or site. It's not a
groundbreaking tutorial but it's one that's rarely done right, so I'd
like to try and fix that. In this tutorial we're going to cover every
step from creating a custom post type to hold our slides, to
actually using the slider within our site.
We're going to be using the lovely FlexSlider 2 by WooThemes as
it's a very well-coded responsive slider that's licensed under the
GPLv2 License. If you're interested, you can take a look at the code
for FlexSlider in its GitHub Repository.
Before we do anything, we're going to take a step back and think
about:
What files the slider requires
What files we require
The first thing we're going to do is download FlexSlider.
After you've done that, go ahead an unzip it.
images
js
Let's put flexslider.css in
the css folder, bg_direction_nav.png in the images folder
and jquery.flexslider-min.js in the, you guessed it, js folder.
Note: Normally I would place these in css/images/js folders
throughout the theme's directory with other files but in order to
make this tutorial 'universal', we're organising our files this way. If
you're experienced with WordPress theme development you may
want to manually organise your files.
Now we're going to create 2 more files in the slider folder:
slider.php - creates the slider's template & loads the slider's
files
slider_post_type.php - creates the slider post type
You should now have a slider folder that looks something like this:
2 r_post_type.php' );
3 require( get_template_directory() . '/inc/slider/slide
4 r.php' );
function register_slides_posttype() {
$labels = array(
'name'
general name' ),
'singular_name'
0
7 type singular name' ),
0
'add_new'
0
9
'add_new_item'
1
0
Slide' ),
'edit_item'
1
1
'new_item'
1
2
'view_item'
1
3
'search_items'
1 Slides' ),
4
'not_found'
1
5
1
6
1
7
1
8
'menu_name'
);
1
9
2
0
2
1
2
$taxonomies = array();
$supports = array('title','thumbnail');
$post_type_args = array(
2
3
'labels'
2
4
'singular_label'
2
5
'public'
2
6
'show_ui'
2
7
2
8
=> __('Slide'),
=> true,
=> true,
'publicly_queryable'=> true,
'query_var'
=> true,
'capability_type'
2
9
'has_archive'
3
0
'hierarchical'
3
1
'rewrite'
3
2
=> $labels,
=> 'post',
=> false,
=> false,
3
3
'menu_position'
3
4
'menu_icon'
=> $supports,
=> 27,
=>
get_template_directory_uri() . '/inc/slider/images/ic
3
5 on.png',
3
6
3
7
3
8
'taxonomies'
);
3
9
register_post_type('slides',$post_type_args);
4
0
4
1
=> $taxonomies
}
add_action('init', 'register_slides_posttype');
4
2
4
3
Custom Post Type added! Below that we'll add the metabox where
there's a field for the URL that the slide should link to. We're now
going to copy the following big wall of code:
0
0
1
0
0
2
0
0
3
0
0
$slidelink_2_metabox = array(
'id' => 'slidelink',
'title' => 'Slide Link',
'page' => array('slides'),
0
0
5
0
0
6
0
0
7
0
0
8
array(
0
0
9
0
1 l',
0
'desc'
=> '',
'id'
=> 'wptuts_slideur
'type'
0
1
2
=> 'wptuts_slide
=> 'text',
'rich_editor'
0 0,
1
3
'class'
0
1 url',
1
0
1
4
'name'
'max'
),
=> 0
=>
1
5
)
);
0
1
6
0
1
7
add_action('admin_menu', 'wptuts_add_slidelink_2_
meta_box');
0
1
8
function wptuts_add_slidelink_2_meta_box() {
0
1
9
global $slidelink_2_metabox;
0
2
0
foreach($slidelink_2_metabox['page'] as $page
0
2) {
1
add_meta_box($slidelink_2_metabox['id'],
0
2
2 $slidelink_2_metabox['title'],'wptuts_show_slidelink_2
_box', $page, 'normal', 'default', $slidelink_2_metab
0
2 ox);
3
0
2
4
0
2
5
}
}
0
2
6
function wptuts_show_slidelink_2_box()
0
2
7
global $post;
0
2
8
global $slidelink_2_metabox;
global $wptuts_prefix;
0
2
9
global $wp_version;
0
3
0
0
3
1
foreach ($slidelink_2_metabox['fields'] as $f
0
3
7
$meta = get_post_meta($post-
0
>ID, $field['id'], true);
3
8
0
3
9
echo '<tr>',
0
'<th style="width:20%"><label
4
0 for="', $field['id'], '">',stripslashes($field['name'
0 ]), '</label></th>',
4
1
'<td
0 class="wptuts_field_type_' . str_replace('
4
', '_', $field['type']) . '">';
2
0
4
3
0
4
4
switch ($field['type']) {
case 'text':
echo '<input type="text"
0
4
8
}
echo
0
4
9
'</tr>';
0
5
0
0
5
1
0
5
2
'<td>',
echo '</table>';
}
0
5
3
0
5 );
4
add_action('save_post', 'wptuts_slidelink_2_save'
0
5
5
function wptuts_slidelink_2_save($post_id) {
0
5
6
global $post;
global $slidelink_2_metabox;
0
5
7
0
5
8
if (!
0 wp_verify_nonce($_POST['wptuts_slidelink_2_meta_box_no
5
9 nce'], basename(__FILE__))) {
0
6
0
0
6
1
0
6
2
return $post_id;
}
if (defined('DOING_AUTOSAVE') &&
0
DOING_AUTOSAVE) {
6
3
return $post_id;
0
6
}
4
0
6
5
0
6
6
0
6
7 id)) {
if ('page' == $_POST['post_type']) {
if (!current_user_can('edit_page', $post_
0
6
8
0
6
9
return $post_id;
}
} elseif (!current_user_can('edit_post', $pos
0 t_id)) {
7
0
0
7
1
return $post_id;
0
7
2
foreach ($slidelink_2_metabox['fields'] as $f
0 ield) {
7
3
0
7
4
0
7
5
0
7
6
$old =
get_post_meta($post_id, $field['id'], true);
$new = $_POST[$field['id']];
0
7
7
0
7
8
if($field['type'] == 'date') {
$new = wptuts_format_date($new);
update_post_meta($post_id, $field
0 ['id'], $new);
7
9
0
8
0
} else {
0
8
1
if(is_string($new)) {
$new = $new;
0
8
2
0
8
3 ['id'], $new);
update_post_meta($post_id, $field
0
8
4
0
8
5
0
8
6
0 '], $old);
8
7
0
8
8
0
8
9
0
9
0
0
9
}
}
1
0
9
2
0
9
3
0
9
4
0
9
5
0
9
6
0
9
7
0
9
8
0
9
9
1
0
0
1
0
1
1
0
2
1
0
3
1
0
4
1
0
5
1
0
6
1
0
7
1
0
8
1
0
9
1
1
0
1
1
1
1
1
2
Phew. That's over. Go to your Dashboard and you'll see a new shiny
'Slides' Custom Post Type.
function wptuts_slider_scripts() {
0
4
wp_enqueue_script( 'jquery' );
0
5
wp_enqueue_style( 'flex-style',
0 get_template_directory_uri() . '/inc/slider/css/flexs
6
lider.css' );
0
7
0
8
wp_enqueue_script( 'flex-script',
get_template_directory_uri() . '/inc/slider/js/jquer
0
9 y.flexslider-min.js',array( 'jquery' ), false,
1 true );
0
}
1
1
add_action( 'wp_enqueue_scripts', 'wptuts_slider_
1
scripts' );
2
While we're doing this, there's something you need to do. Due to
our file structure, the flexslider.css needs some editing so it
knows where to find the arrow image. Open it up and do a search
and replace for:
images with ../images
Step 4 Initialize Slider
Now we need to add some jQuery to our <head> in order to
initialize the slider. Let's add in the following lines
to slider.phpbelow the code we just added!
0
1
0
2
0
3
0
4 8">
0
5
jQuery(window).load(function() {
0
6
jQuery('.flexslider').flexslider({
0
7
animation: "fade",
0
8
direction: "horizontal",
slideshowSpeed: 7000,
0
9
animationSpeed: 600
1
0
});
1
1
1
2
});
</script>
1
3
<?php }
1
4 );
1
5
One of the reasons I chose to use FlexSlider is because of its
flexability. There are quite a few parameters you can tinker with,
but I just included four important ones above. Try
function wptuts_slider_template() {
0
3
0
4
$args = array(
0
5
0
6
'posts_per_page' => 5
0
7
0
);
8
0
9
1
0
11
1
2
if ( $the_query->have_posts() ) {
1
3
1
4
1
5
<div class="flexslider">
<ul class="slides">
1
6
1
7
1
8
<?php
while ( $the_query->have_posts()
1 ) : $the_query->the_post(); ?>
9
2
0
2
1
2
2
<li>
<?php
if ( get_post_meta( get_the_i
<?php }
2
7
2
8
echo the_post_thumbnail();
2
9
3
0
if ( get_post_meta( get_the_i
3
1 d(), 'wptuts_slideurl', true) != '' ) { ?>
3
2
3
3
3
4
3
5
3
6
3
</a>
<?php } ?>
</li>
<?php endwhile; ?>
7
3
8
3
9
4
0
4
1
4
2
<?php }
4
3
4
4
4
5
4
6
wp_reset_postdata();
}
4
7
4
8
4
9
Step 6 Using the Slider
Well, we've finally made the slider! Now it's time to actually use it.
You can open up any template file, such as index.php, and echo
the wptuts_slider_template function to display the slider.
So if we wanted to show the slider in Twenty Eleven right after the
header on the home page, we would open up index.php and just
beneath get_header(); ?> , add the following:
But what if you're making this for a client or someone who just
doesn't want to touch template files and PHP? We should probably
create a shortcode for them, so they can just use the slider with
a [slider] shortcode.
Add this at the bottom of slider.php:
1
2
function wptuts_slider_shortcode() {
ob_start();
4
5
wptuts_slider_template();
$slider = ob_get_clean();
return $slider;
8
9
}
add_shortcode( 'slider', 'wptuts_slider_shortcode'
);
The slider can now be used within posts, pages or anywhere else
that accepts a shortcode!
Download
If you'd like, you can download the slider folder, which contains
everything we went through in this tutorial. You just need to drop it
in your theme's inc folder (or somewhere else is fine, but be sure
to adjust the code below) and add the following to
yourfunctions.php:
installation
and provide links as I go along.
Unless you have your own theme you're working on, you'll also
need the starting code for the tutorial. You can see my theme
athttp://rachelmccollin.co.uk/nettuts-wpresponsive-tutorial/ and
download the code for the theme at various stages of its
development from xxx [to be added based on nettuts+ downloads
system].
Before We Start: What's Mobile First?
Mobile First is a term which was first coined by Luke Wroblewski. It
refers to the
strategy of turning the way we design websites and themes on its
head. Instead of starting with a desktop design and then adjusting
it for mobile devices, we work in the other direction - we start by
designing and coding for mobile devices and then add what's
needed for larger screens. This has a few advantages:
It's faster - content or styling which isn't needed on small
screens isn't sent to them. This can be faster than just hiding
or overriding it.
It changes the way we plan content - by focusing on small
screens, we focus on what's really important rather than on
what we're including because w have the space. Designing in
this way can have a drastic impact on the eventual desktop
design.
1. Our Starting Theme
The starting theme has minimal styling applied to it, and no layout
styling at all yet - that will be added as we work through the
tutorial.
The elements which the theme includes are shown below:
But I still need to add some layout styling for small screens, which
is the next step.
3. Style the Main Layout Blocks for Small Screens
I'll start by adding some widths and floats for the main elements,
to ensure that they stretch across the screen. I add some widths
and a clear:both declaration to the relevant elements:
width: 100%;
3
clear: both;
4
}
Next, to avoid the design looking squeezed into the small screen,
I'll add padding to some elements:
2
3
Note that these are the only horizontal values I'll be defining in
pixels, and I do this because I don't want the padding to become
too big as the screen size increases.
Now the site looks less crowded and the main elements are
displayed beneath each other:
Next I'll tidy up the widget areas and improve the navigation.
4. Add Additional Styling for Small Screens
The navigation needs to span the width of the screen and I'd like to
centre each menu item, so I'll add some styling for the menu:
nav.main {
1
background: #3394bf;
overflow: auto;
3
4
text-align: center;
5
}
The widget areas are all quite close together and it's difficult to see
where one ends and another begins. I'll add some borders and
padding to address that:
.sidebar .widget {
padding: 10px 0;
padding: 10px 0;
3
4
You may have spotted that my media queries aren't all based on
the width of a specific device. This is because as the range of
devices and screen widths expands, targeting specific devices
becomes less reliable than setting media queries at the point at
which the layout benefits from them. I've tested the layout by
resizing my browser window and at 600px wide it looks like this:
In my view, the design looks wrong at this width and can benefit
from some layout changes.
So, I start by adding my media queries at the end of the
stylesheet:
1
2 @media screen and (min-width: 600px) { }
3
4
5
6
7 @media screen and (min-width: 1025px) { }
8
Note that I've used min-width , not max-width as you would if
you were styling desktop first and writing media queries for smaller
screens.
Next I'll move on to styling for tablet devices.
6. Add Styling for Tablet Devices in Portrait Mode
I want to improve my layout to make better use of the screen
space on these larger screens, and I'll also add some CSS gradients
to make the design a bit more polished.
Firstly, the layout. Inside my media query for screens 600px and
larger, I add:
01 footer {
overflow: auto;
02 }
03
.sidebar .widget,
04
05
footer aside {
06
float: left;
07
width: 49%;
08
margin-right: 2%;
09
10 }
11
.sidebar .widget:nth-child(even),
12
13
footer aside:nth-child(even) {
margin-right: 0;
14
}
This adds floats and some margins to make better use of the
screen width:
I'm now going to alter the navigation, as the current layout leaves
a lot of empty space next to each item and takes up a lot of room. I
add the following styling to create a horizontal navigation bar:
nav.main {
01
text-align: left;
02
padding: 0 10px;
03
04
05
nav.main li {
06
float: left;
07
margin: 0;
08
09
10
}
nav.main li a {
11
padding: 0 20px;
12
}
I'll also add a gradient to the menu for this screen size and above:
1 nav.main, nav.main a {
2
3
background: #183c5b;
5
6
7
8
01
.content {
02
width: 65%;
03
float: left;
04
05
06
}
.sidebar {
07
width: 33%;
08
09
float: right;
10
clear: right;
11
12
13
}
.sidebar .widget {
14
width: 100%;
15
float: none;
16
}
.sidebar .widget:first-child {
border-top: none;
}
working with it last. You may have noticed by now that this makes
things very efficient - in my experience, removing desktop styling
in media queries aimed at mobile takes much more work than
tweaking my mobile layout for desktop.
I'm going to adjust the footer layout to have all four footer widget
areas next to each other, and add a background image to the
header. This image isn't crucial to the content but is there for
decoration, so I'm comfortable with the fact that it isn't in my
markup.
First, the layout. As well as adjusting footer layout, I'll add a maxwidth value to the body tag to avoid stretching the layout
across the full width of very wide screens:
01
body {
02
width: 95%;
03
04
max-width: 960px;
05
margin: 0 auto;
06
07
08
footer aside {
09
float: left;
10
width: 23.5%;
11
margin-right: 2%;
12
13
footer aside:nth-child(even) {
margin-right: 2%;
14
15
16
footer aside.fourth {
margin-right: 0;
}
The styling above goes in the media query for screens with
a min-width of 1025px, and does the following:
Adds max-width to the body
Adjusts the width and right hand margin on
the aside elements in the footer
Adjusts the margin for even numbered widget areas to 2% in
line with the other widget areas
Using the class assigned to the fourth widget area in the
theme ( .fourth ), reduces its margin to 0. I've deliberately
used a class here instead of nth-child as I need this to
work across browsers
So, what does my theme look like in the desktop?
header {
1
background: URL(images/banner-
4
}
The advantage of using this approach is that this image will only be
downloaded by desktop machines. As it's not in the core styling or
the media queries for smaller devices, it won't slow them down.
There are downsides to using images as backgrounds though,
especially relating to accessibility - so it's important to only use
this technique when the image is for design only and isn't part of
the content. Alternatively, I could have added a smaller version of
the image in my markup and used CSS to hide that and display a
larger version of the same image as a background for larger
screens.
Summary
I now have a responsive, Mobile First theme. By starting with small
screens and working my way up I reduced the amount of code
needed to create my responsive design and was able to ensure
that a large image isn't sent to mobile devices.
As more developers and site owners require their sites to be
responsive, being able to build responsive WordPress themes will
become an invaluable skill. In this tutorial I've shown that it
needn't be an onerous process and shouldn't take too long. Of
course, you can take the process further and add mobile-specific
content or additional styling targeted at specific devices, but that's
something for another day!
This idea is not new. This approach used to be common for raster
icons. With the permeation of vector imagery on the web, we as
icon designers mistook scalability for legibility. So what makes
See demo
Web-fonts and element-query polyfills
As previously mentioned, adjusting icons based on screen width is
not the best approach to ensure legibility. Icons need to respond to
their own display dimensions. Unfortunately, we do not yet have
the ability to create element queries within CSS. The good news is
that people are thinking about this and creating workarounds. The
demo below uses element queries and web fonts for a dynamicallyadjusting icon based on its display size.
See demo
SVG breakpoints
There's been some great thinking in this space, specifically
from Andreas Bovens who has been talking about SVG
breakpointsfor quite some time now. This approach relies on the
fact that media queries within an SVG file referenced as
an img , embed or object use the display dimensions of the
SVG. Meaning a media query of min-width:400px within the SVG
would trigger if the image was 400 pixels or moreregardless of
screen width. The demo below shows that functionality in action.
See demo
There's currently one problem with this approach. Those internal
breakpoints no longer work when the SVG file is injected in DOM.
SVG DOM-injection is vital to other features of Iconic, so this will be
something we'll need to figure out in the near future.
Conclusion
An illegible icon fails in its most important duty. We need to stop
thinking of vector imagery as the catch-all for icon legibility. We
need to design icons that accommodate different size ranges and
we need tools that display icons at the proper fidelity based on
their display size. None of the current opinions are perfect. But
they're a step forward in the right direction.
Back Iconic on Kickstarter
The goal of Iconic is to help provide new approaches to
iconography.