The CSS3 Carousel Experiment

When I first read about CSS Animation, I thought that they shouldn’t be messing with the separation of logic and style. In my mind, it should be clear that logic (animation, rotation, even the alternating row styles) should be in javascript, all the styles should be in CSS, and the document structure in HTML.

One time, while working on our internal project, I thought I’d try a little -webkit-transition-duration because I wanted the color of the links to gradually change to something else on hover. But when I hovered on our main navigation that was using a sprite image, the background scrolled from one background position to the next every time we hover on it because of the delay! It was really amusing.

CSS3 Carousel

I experimented a little bit with transition and descendant selectors, but apparently, p ~ p doesn’t work with :hover or :active or :focus very well (browser notes below) and came up with this CSS3 carousel experiment.

To do this, you should have the following markup:

<div class="carousel">
  <a href="#" class="first-image">1</a>
  <a href="#" class="second-image">2</a>
  <a href="#" class="third-image">3</a>
  <a href="#" class="fourth-image">4</a>
  <img src="main-background.jpg" alt="" />
</div>

It’s really rather plain, it’s because we’ll be choosing the image using CSS’ child selectors to move the image and it won’t work if we enclose the links in a div or list.

And this CSS:

  img { -webkit-transition: all 5s; -o-transition: all 5s; }
  .carousel { width: 500px; height: 372px; overflow: hidden; }
  a.first-image:hover ~ img { margin-left: 0; }
  a.second-image:hover ~ img { margin-left: -500px; }
  a.third-image:hover ~ img { margin-left: -1000px; }
  a.fourth-image:hover + img { margin-left: -1500px; }

New child selectors: the ~ symbol is used to select all fellow child elements, in this case, an image. If you add another image, it will be selected by this, too. The + sign on the other hand is to select a descendant that directly follows it. It’s really exciting, go nuts! 1

Some people would obviously rather use a div and apply the image as background, all you have to do is replace img with div or the class you assigned to it.

Browser (in)compatibilities:

  • For some reason, webkit-based browsers doesn’t interpret this very well, the second and third link can’t be triggered unless you hover on the fourth link first.
  • Firefox interprets the child selectors perfectly but not transition because it will only be implemented in Firefox 4.
  • The experiment works perfectly in Opera 10.6 and it gives me a warm fuzzy feeling inside. :D

Further Reading:

On creating themes and going out of your comfort zone

I have always been a WordPress girl, but you have to admit that there are instances where you just have to use something that specializes in that area.

I’ve recently overhauled Last Leaf and turned it into a lifestream. I have stopped blogging there since the start of the year and have always thought of turning it into just that but I haven’t because creating themes for another CMS seemed intimidating.

At first, I thought of writing my own lifestream, it should have been my way of learning more about RoR but until now, all I have in my folder is the basic install (which I have forgotten how I was able to produce) so I turned to the next best thing: Sweetcron. It’s been months since I have looked at Sweetcron, I remember that I found it so unfriendly to customize to one’s needs before. Turns out, all I really needed was to read a good documentation, a lot of focus and plenty of time to test things out. It’s a pleasant exercise, and it’s something I know I’ll do again.

And so, with the intro done and over with, I suggest that you try and do something you haven’t tried before once in a while, if you’re not yet ready to commit full-time to something really big (RoR for me) then do some little things (like theme customizations, I think I want to try Drupal next) and so, here are the notes I made myself remember while writing Last Leaf’s theme:

  • Base your first theme on one of the default ones

    Most of the time, you already have almost everything you need in the default theme, the developers won’t include that otherwise, I think. Trying to edit the defaults also make the learning curve more enjoyable because you know you won’t just break something because it acts as your guide in the process.

    My first WordPress theme was something based on WordPress Classic, I have little to no knowledge in CSS that time and that’s the best thing I could come up with. It helped me get familiar with the CMS’ templating system (man, I sound so pretentious) as well as taught me what this CSS shenanigan is. With Sweetcron, I used the Boxy Theme. As soon as you were able to stop the nauseous feeling whenever you see a PHP snippet, you’d realize that it’s pretty straightforward.

    Social Media I had a great time styling the individual boxes, it got a little confusing after a while though, what with all those accounts, so I made a separate file for each one of them like what you can see on the image, it made _activity_feeds.php less cluttered and made isolating the problem easier.

  • Experiment

    I wanted to get just the image within the post because I’m not planning on directing people into the single page of every items and all I really want to show sometimes are the interior decors I love. At first I used this: <?php echo $item->item_data[$item->get_feed_class()]['image']['m']?> which is something you need to call the images you uploaded in Flickr but won’t work anywhere else. Thankfully, somewhere in the _activity_feeds.php file is <?php echo $item->get_image()?> and that’s what I used for my Tumblr and Google Reader posts.

    However, I don’t post images there all the time so I need a way to retrieve the text if the image doesn’t exist and so, there goes the only thing I’m good at, if-else statement:

    <?php if ($item->get_image() == ''):?>
         <?php //display your text here ?>
    <?php else: ?>
         <?php //and this is for your photo ?>
    <?php endif; ?>
    
  • Search and then ask

    I would’ve probably given up on Sweetcron the second time have I not found that Nettuts tutorial, all you really need to do is keep your cool if you can’t find a solution to your problem. And if you can’t find the solution through searching, go to the usergroup/website of the CMS.

Sweetcron is still new and AFAIK, there’s only one developer so it’s understandable if there’s no documentation on the site like: is there a way to retrieve the tags I added in my starred items in Google Reader? How do I truncate the title? (<?php echo word_limiter($item->get_title(), 20) ?> doesn’t work :() Is it possible that I could host the images in my own server instead of relying on other sites? However, the community behind it is very, very friendly and you’re sure to find answers you’re looking for there, unless you’re too shy to ask for it. :P

Random CSS tips and tricks

I wrote (as in handwritten) this a couple of weeks ago when I was having trouble sleeping, chances are you already know these things but, for the benefit of my rusty memory, I’ll still post it here. Besides, solving these problems took me hours of frustration, so I better chronicle it. :(

  • There are times when list items occupy a huge space in IE even though you’re sure that you haven’t specified excessive paddings and margins to it. Setting the list item’s display value to inline-block will solve this (I’m a little surprised that IE 6 can interpret this value because I’ve always thought that it only knows block and inline, that’s how little I think of it).

    Inline doesn’t allow paddings and margins at the top and bottom area of the elements, however, it also means losing the width and layout of the element. In inline-block, we achieve just that, lose the excess top and bottom spaces while still being able to maintain the layout of the element. (That is, based on my understanding)

    And in some bizarre cases, inline will do and yes, it will still look like it’s a block item and will just take out the excess margin, weird, yes? I wish I have an explanation for this.

  • There are numerous times and reasons why we want to set list-style to none. What sucks is that after you’ve turned off the list-style to the parent list item, you’d realize that you want the bullets to show in the children element.

    All hell breaks lose when no matter what head-banging you do, it just won’t show up. Thankfully, display: list-item is there to restore the bullets and is working in IE too!

  • List items are supposed to line up neatly even when a float is used to an image before it. However, there are times when that’s not the case in, you guess it! IE 6. Standards-aware browsers will be solved by adding:
    { overflow:hidden; list-item-position:inside; }

    The result may be that the bullet may be a little too close to the text but at least it’s not below the list item just like in IE. The trick is to use display:inline-block too.

  • Not all of the CSS problems are in IE. Sometimes the great Fx 2 has some quirks too. It usually happens in the useful, albeit a little used, autocomplete function. What usually happens is that the autocomplete items go under the div elements below it.

    Here’s what I usually do:

    HTML

    <div class="parent_element">
      <div class="autocomplete">
        <ul>
          <li>Value here</li>
        </ul>
      </div>
    </div>

    CSS

    .parent_element { position:relative; z-index:99; overflow:visible; }

    What my understanding of this is that once the z-index is set, then it lifts the entire div and everything within it above every other element in the page therefore eliminating the problem.

Presenting videos using WordPress

And without using a plugin.

I thought of creating this post when I was on my way home from the recently concluded WordCamp Philippines. There was a woman who was in Karla Redor‘s talk who asked on how she can host the video in her own blog and use WordPress to file it for her. She can use a plugin that will let her upload the videos in her blog or do it the “harder” way. Something that I’m very fond of doing.

Seriously, I’m the type of person who would only resort to using plugins if:

  1. I’m lazy at the time to think of another way without using a plugin
  2. My head is aching and I’m getting frustrated because it just won’t follow what I’m telling it.

The reason is because I am not a developer and it’s really hard to be dependent on a plugin specially if that plugin is what’s keeping your blog alive. Also, if the developer of the plugin suddenly stops supporting the plugin and no one else is willing to take it up from there. What’s worse is if it’s not compatible with the newer version of WordPress. Then I will be lost.

So anyway, on to the “how”.

  1. Convert you video to a .swf file, you can use Hey!Watch, it has a fee though but considering how it will make your life easier, I think it’s worth it. Besides it will convert a video for $0.10 so I don’t think that’s too much.
  2. Next in your code (you’d want to add this in your single.php page), call for the video custom field (which you’ll be adding later on). Note that this should be within the loop. Also, note that the snippet is taken from a WordPress support forum and changed to suit the topic.
    <?php $video = get_post_meta($post->ID, 'video', true); ?>
    <?php if (!empty($video)) {;?>
    <object type="application/x-shockwave-flash" data="<?php echo $video; ?>" width="400" height="300">
    <param name="movie" value="<?php echo $video; ?>" />
    </object>
    <?php }; ?>

    What does this mean? The first line sets the $video variable to the value of the video key (of the custom field). Next it checks if the video has a value attached to it (in that particular post). If it does, it inserts the value in the data and value properties of object and param. If the video doesn’t have any values then it won’t display anything.

    This way, it saves you the time to copy and paste the entire code for the video. Now if you want the lazier way, you will have to adhere to some restrictions. For example, you can set the value of data and value to:

    <?php bloginfo('url') ?>/videos/<?php echo $video; ?>

    This means that you will only be able to upload videos within the videos folder within your root. That way, you’ll only put the filename of the video in the custom field value.

  3. Once you’ve uploaded your video to your server (within the specified folder of course), go to the admin panel of your blog and then create a post (or a page) in your custom fields area, do what’s illustrated in the image below:

    As you can see, I decided that I want to have another folder within my videos folder. My videos will be categorized according to shows, so the value of my video key is unang_hirit/episode_100.swf. Once published, the post’s HTML will appear like this:

    <object type="application/x-shockwave-flash" data="http://domain.com/videos/unang_hirit/episode_100.swf" width="400" height="300">
      <param name="movie" value="http://domain.com/videos/unang_hirit/episode_100.swf" />
    </object>

Featured Video

If for some reason, you want to create a section that is called the “Featured Video” section on your sidebar, you can call it by using the following lines of code:

<?php query_posts('category_name=Featured Videos&showposts=1'); ?>
  <?php while (have_posts()) : the_post(); ?>
    <?php $video = get_post_meta($post->ID, 'video', true); ?>
    <?php if (!empty($video)) {;?>
    <object type="application/x-shockwave-flash" data="<?php echo $video; ?>" width="400" height="300">
    <param name="movie" value="<?php echo $video; ?>" />
    </object>
    <?php }; ?>
  <?php endwhile(); ?>

If you’d notice, we recycled the code from above. The only difference is the query_posts tag before the loop. What the query_posts tag does is that it takes a single, recently added post (showposts=1) from a category called “Featured Videos”. And it’s the same business as what was discussed above.

Further reading:

min-height in IE 6

Would you look at this, while everyone has finally decided that they will be dropping support for IE 6, that’s when I decided to create a tutorial for implementing min-height for the browser. Anyway, onto the post.

We all know that the min-height property doesn’t work in IE 6. Instead, the height property acts as both the element’s height and min-height. Its role will ultimately rely on the value of your overflow property.

If the overflow property is set to hidden then the height of the element is its max-height. If set on visible, however, then it will be its min-height.

Overflow: Visible

The element will take up the specified height and when the content of the element exceeds the height then the element will just expand vertically (or horizontally, if it needs to). You can define whether the it will expand vertically of horizontally by specifying the value for overflow-y (vertically) or overflow-x (horizontally) (your CSS document won’t validate for CSS 2.1 though, but it’s valid once tested against CSS 3).

So your CSS (for IE 6) will look something like this:

div.parent-element { height:300px; overflow-y:visible; overflow-x:hidden; }

The Complexities that hasLayout Brings

There are some instances when you have to define the height and overflow properties of an element (usually applicable to lists) to meet the demands of the hasLayout curse in IE. In these instances, you will have to add a bogus height and overflow:hidden thinking that it will not bring harm to your design.

That is until you wanted to add a min-height to the parent div.

If your bogus height is set to 1%, you element will collapse. Setting the height to auto will render your layout worse than before because that means that your element “will not have a layout” (ie, your hasLayout is nonexistent in IE).

Setting the height to 100%, however, will then expand your element to the height of its parent div (or element).

You can solve this dilemma by changing your overflow value from hidden to visible and setting the height of your element (usually, list items) to 1% just to retain the bogus height.

Appending the title of the post in “Read more” links for WordPress

One of the guidelines in usability is that you should provide only one way that will lead them to a certain action. For example, if someone sees 2 option buttons, chances are that they might think that each option button does separate tasks when in fact their functions are the same. Also, we have to make sure that the links are descriptive enough and if not, then we should provide a title for the link so when a user hovers over it then they’ll know where the link will take them.

On a similar note, accessibility advocates encourages people to use unique name for every link that we use on a page (kinda like what I was saying before only have a different reason), meaning countless “Read more of this entry” is not really the best practice because you’re using the same text over and over and these texts are leading the user to different pages.

I was looking for a way to add that in <?php the_content(); ?> tag in wordpress before but I couldn’t do it. What I did then was append the the_title(); within the_content(); so it looked like this –

<?php the_content('Read more of ' . the_title() . ' & raquo;' ?>

The problem though is that it does echo the “Read more of” but the title is no where in sight! I was so ready to find a way to make it show by hacking away through function.php, thankfully I remembered to check the_content page in the codex first.

What I should’ve done in the first place is to add parameters to the the_title() tag so it will show like this –

<?php the_content('Read more of ' . the_title('', '', false)) ?>

This follows the same parameter as the the_title(); template tag.

Parameters

before
(string) Text to place before the title. Defaults to ”.

after
(string) Text to place after the title. Defaults to ”.

display
(Boolean) Display the title (TRUE) or return it for use in PHP (FALSE). Defaults to TRUE.

So you may use it like this:

<?php the_content('Read more of ' . the_title('&ldquo ;', '&rdquo ; &raquo ;', false)) ?>

So it will echo as Read more of “Title of the Post” ».

When ‘is_home()’ doesn’t work

Note: This has been written a long time ago, some of the stuff here might not work anymore. I’d recommend using <?php is_frontpage(); > when using static frontpage instead. You can also visit the WordPress Codex for more information or contact me and I’ll try my best to help you.

You hacked your WordPress homepage because you don’t intend to make a blog out of it and then all of a sudden the very useful conditional tag <?php if(is_home()) ?> no longer works. It happened to me twice and for that 2 occurrences, I couldn’t find a solution until a couple of days ago.

I’m working on this project where I don’t need to show my posts anywhere aside from the category archive and single, not even in the homepage. What the client wants is that the home page should show all of the categories in the site, no matter if it’s empty or not and a certain page content must be pulled into it. Because I’m lazy in creating a page template just for that and assigning that page as the home page, I modified the main index template instead.

Those things are actually easy to do. All I have to do is to write this within the main index template:

<?php wp_list_categories('order_by=name&hide_empty=0&title_li='); ?>

so it’ll show all of the categories ordered by the category name whether it has posts or not. And for the page to be pulled into the main index, query_post is the answer:

<?php query_posts('page_id=ID'); while (have_posts()) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_content(); endwhile; ?>

so it’ll show the title and the contents of the page ID.

It worked just as expected and I was so proud of myself until the client asked that the sidebar for the home page be different from the rest of the site. That’s when I knew that the is_home() doesn’t work.

I searched high and low for in the codex and support forum but couldn’t find anything. I can’t tell the client that I can’t fix this so I tried my hunch if it works. I’m not the type who is a big fan of plugins because I’m afraid that if the site rely heavily on plugins and and if and when that certain plugins’s developer goes AWOL, the site might go berserk due to incompatibility and all I could do was search again for a plugin that could replace it.

I thought that the home page is being treated by wordpress as if it’s a page because I did pull the contents of a page into it. Turns out that I was right!

When I put <?php if(is_page('ID')) { ?> in the sidebar, it worked just as I wanted it to.

So now, I realized that once you’ve changed the content of your main index template, know that you are also taking out it being your “home” because it will start acting like a page (if you pulled a page into it).