Balkhis - Vision For Success

How to create simple accordions in wordpress

Dear Readers, I would like to thank you for reading this post. I highly value your feedback and would like to know more about my readers. Follow me on Twitter

Hey guys,

I am Atif from Techytuts.com – Computer and Internet Tutorials and Resources this is my first guest blog ever. So I will write it short to avoid mistakes. First of all I personally think that this is something must have to show off your skills in your blog or website. This one has got a good interaction and good visual effects, And today basically I’m going to show you how to create accordions of your wordpress posts for some extra ordinary looks. I will show you up how you can set up the accordions, regarding styling you got to do it your self. We will go in 4 easy steps to accomplish this.

First off you need to download this small file accordian.pack.js and save it into your template directory.

Now open up a typical wordpress theme…and search for <?php wp_head(); ?>

One you find it, add this line just above it. To make it look like this. Make sure the accordian.pack.js file do exists in your template directory.

<script language=”javascript” type=”text/javascript” src=”<?php bloginfo(‘template_directory’); ?>/accordian.pack.js”></script>
<?php wp_head(); ?>

For the third step, go to the body tag and replace it with this one…

<body onload=”new Accordian(‘basic-accordian’,5,’header_highlight’);”>

And for the final step, which is a little tougher one… Search for <?php if (have_posts()) : ?> and add this line above it.

<?php $i = 0; ?>
<div id=”basic-accordian” > <!–Parent of the Accordion–>
<?php if (have_posts()) : ?>

Now search for while <?php (have_posts()) : the_post(); ?>
and add these lines below it

<?php while (have_posts()) : the_post(); ?>
<!– Start of each accordion item –>
<div id=”test<?php if (!($i == 0)) {echo $i;} ?>-header” class=”accordion_headings” ><?php the_title(); ?></div>
<div id=”test<?php if (!($i == 0)) {echo $i;} ?>-content”>
<div class=”accordion_child”>

Finally search for <?php endwhile; ?> and add these lines above it

</div>
</div> <!– End of each accordion item –>
<?php $i++; ?>
<?php endwhile;?>
</div>

Your job’s done. You can do the custom styling using css. You can see the working demo here

PS: Good color combination and best suitable time complexity according to the users you expect on your website will make this script useful an attractive and



To stay updated with the blog, please consider subscribing to my full feed RSS. You can also subscribe by Email and have new posts sent directly to your inbox. I hate spam as well, so I promise you that you will not be spammed.
Add to Social Bookmarks:
Add this Article to Digg Add this Article to Stumbleupon Add this Article to Del.icio.us Add this Article to Reddit Add this Article to Newsvine Add this Article to Technorati

RSS feed

20 Comments

Comment by Authority Directory
2008-10-10 11:31:05

It never ceases to amaze me with all the really cool things that can be done with wordpress. Very nice tip


 
Comment by Dr. Teeth
2008-10-10 13:55:16

Wow, that’s just awesome Atif.
I would probably need something cool like this on my blog too!
Thanks for the heads up


Comment by Mohd Atif
2008-10-11 01:13:06

Thanks for compliment Dr. Teeth


 
 
Comment by Dina Subscribed to comments via email
2008-10-10 16:04:28

I love seeing all the new wordpress themes, ideas and designs coming out. We’re working on updating the MLTV site, so we may have to try out some of your suggestions!


Comment by Syed Balkhi
2008-10-10 17:19:49

I can’t wait to see the new mltv design. O btw: I will be stopping by the office on Tuesday sometime …


Comment by Dina Subscribed to comments via email
2008-10-10 17:47:52

I’m excited to see you! Will you be joining us for lunch?

(Comments wont nest below this level)

Comment by Syed Balkhi
2008-10-11 05:28:14

What time is the lunch and I will make the plans accordingly ;)


 
 
 
 
Comment by Otooo
2008-10-10 21:06:27

So would this be used for recent posts or featured posts or what? It looks awesome, but what’s the best way to utilize it (where to place it and what feature to give it?)


Comment by Mohd Atif
2008-10-11 01:14:12

You can use it anywhere you feel like recent posts, recent comments, featured posts, post from a single category etc..


 
 
Comment by Otooo
2008-10-10 21:07:21

Sorry, I have just realized – I clicked on the accordion and see what it does not (slides down) :)


Comment by Mohd Atif
2008-10-11 01:17:37

You might have not followed the steps properly.. Also make sure you download and keep the accordian.pack.js exists in your bloginfo(‘template_directory’) i.e. your theme directory


 
 
Comment by Problogineer
2008-10-11 00:00:45

how it is useful in a wordpress blog ??


 
Comment by QuickPWN
2008-10-11 02:24:23

Great tutorial Atif! This really shows us how customizable WordPress is.


Comment by Mohd Atif
2008-10-11 08:04:16

Thanks QuickPWN

Btw I have a message for all of the readers…

As you can see above the double quotes (“) are shown as ” and single quote (‘) is shown as ’

Dont simple copy and paste the code into your theme.. It will not work..

First make sure you replace all ” and ’ with ” and ‘ respectively..

Thanks for reading my post everyone.. And thanks syed for posting it :)


 
 
Comment by Christopher Ross
2008-10-11 19:23:23

Syed, thanks for a great introduction to the accordion. I saw it on apple.com about a year ago and had to call a couple friends to go look at it. MooTool has an awesome JS tool for creating these effects!


 
Comment by Geoserv
2008-10-11 23:28:54

STUMBLED!

Sweet, thanks for the tutorial.


 
2008-10-13 08:43:12

[...] How to create a simple accordions in WordPress [...]


 
Comment by Downloadic
2008-10-19 10:02:09

Nice tutorial man. I like php very much…


 
Comment by Friso Subscribed to comments via email
2009-05-17 07:12:35

Thanks!

I would like to use the accordion in a step by step tutorial (6 steps). I have one question.

How – in a page- do I divide the title of each step from the contents of the step, so that the title is visible in the header of the accordion and the content is not?

Friso.


 
Comment by Steve Shearer Subscribed to comments via email
2009-09-12 22:56:53

I’ve been looking all over for this & finally found it. Too bad all links go to a 404 page. Guess my search is not over – or can you email me the accordian.pack.js?


 

Sorry, the comment form is closed at this time.

Subscribe to Balkhis via RSS
Subscribe to Balkhis via Email Pimp My Blog