StylizedWeb

Subscribe to our updates:

A Design, WordPress and Tutorials Blog.

Dedicated to helping you learn the art and science of the web.

Amazing WordPress hacks part 1

After a list of simple and really useful WordPress tricks i got amazing feedback which inspired me to write this one on amazingly cool WordPress hacks which are probably not often used but very inspiring and original and in my case showed me new possibilities of WordPress and a completely new way of use.

I've split it in two parts, because code might be long somewhere, so be sure you subscribe to be updated on second part. Also i won't comment the code, just start reading it and you'll see there's to need for it, but if you do have any questions feel free to post them in comments. Enjoy!

Forwarding section pages to their first sub-page
  1. <?php
  2. /*
  3. Template Name: Redirect To First Child
  4. */
  5. if (have_posts()) {
  6. while (have_posts()) {
  7. the_post();
  8. $pagekids = get_pages("child_of=".$post->ID."&sort_column=menu_order");
  9. $firstchild = $pagekids[0];
  10. wp_redirect(get_permalink($firstchild->ID));
  11. }
  12. }
  13. ?>
Grabs the textual content of a Post and style it
  1. /*
  2.  * Grabs the text content from a Page that is meant to be a blurb
  3.  * on another Page.
  4.  * 1. Create a Page and insert text to be used as the blurb.
  5.  * 2. Give the post slug a name.
  6.  * 3. Call get_blurb() from where the blurb should be inserted.
  7.  *
  8.  * @param pagename - post slug of the Page
  9.  * @param keeptags - tag list to allow in the text
  10.  * @return String - the blurb
  11.  */
  12. function get_blurb ($pagename, $keeptags='')
  13. {
  14. $q = new WP_Query('pagename='.$pagename);
  15. $content = '';
  16.  
  17. if ($content = $q->queried_object->post_content)
  18. {
  19. $content = strip_tags($content, $keeptags);
  20. }
  21. else {
  22. $content = 'This blurb does not exist. ';
  23. }
  24. return $content;
  25. }
  26.  
  27. function beautify_blurb ($content, $isIndex=FALSE)
  28. {
  29. if ($isIndex) {
  30. return preg_replace('/^<strong>/i', '<strong class="intro">',$content);
  31. }
  32. else {
  33. return preg_replace('/^<p><strong>/i', '<p class="intro"><strong>',$content);
  34. }
  35. }
Loads a stylesheet with dynamic values dependent on the Page view
  1. /*** file: header.php ******************************/
  2.  
  3. <link rel="stylesheet" type="text/css" media="screen" href="<?php
  4.  
  5. //Import stylesheet for the different pages
  6. if ( is_frontpage() || is_page('index') || preg_match( "#/index/#i" , $_SERVER['REQUEST_URI']) ) {
  7. print getdir().'/home.css';
  8. }
  9. else {
  10. echo getdir().'/sub.css.php?p=';
  11. if ( is_home() || is_single() || is_archive() || preg_match("#/blog/#i",$_SERVER['REQUEST_URI']) )
  12. print 'blog';
  13. else {
  14. $post_parent = get_post($post->post_parent);
  15. print $post_parent->post_name;
  16. }
  17. }
  18. ?>" />
  19.  
  20. /*** file: sub.css.php ******************************/
  21.  
  22. <?php header('Content-type: text/css'); ?>
  23.  
  24. div#sidebar_photo {
  25. background-image: url("images/sidebar_photo<?php
  26.  
  27. if ( isset($_REQUEST['p']) ) {
  28. switch ($_REQUEST['p'])
  29. {
  30. case 'about': print '4'; break;
  31. case 'blog': print '5'; break;
  32. case 'team': print '1'; break;
  33. case 'products': print '6'; break;
  34. case 'howtobuy': print '3'; break;
  35. case 'spreadtheword': print '2'; break;
  36. case 'join': print '7'; break;
  37. case 'share': print '8'; break;
  38. default: print '1'; break;
  39. }
  40. }
  41. ?>.jpg");
  42. }
Post Expiration time and date
  1. ***BEFORE LOOP
  2. <?php
  3. $expirationtime = get_post_custom_values('expiration');
  4. if (is_array($expirationtime)) {
  5. $expirestring = implode($expirationtime);
  6. }
  7. $secondsbetween = strtotime($expirestring)-time();
  8. if ( $secondsbetween > 0 ) {
  9. ?>
  10. ***LOOP HERE***
  11.  
  12. ***AFTER LOOP***
  13. <?php
  14. }
  15. ?>

Leave a comment on Stylized Web Have some feedback? Leave a comment

48 Comments So Far

  1. Pingback: Amaizing WordPress hacks part 1

  2. By John posted on June 25, 2008 at 6:48 pm

    oh, we can do all that! :D
    can’t wait for part two, thanks a lot man!

  3. These are some great hacks which I haven’t come across yet and might refer back to when I need to build a WordPress template. Using PHP with CSS is really powerful. You could use it to change the entire style of the blog depending on the page you are visiting.

    What I’d really like a solution for is how to put posts into columns on an archive page so that each column has a balanced number of posts. Similarly for comments in columns. Anyone know how to do that?

    I’m looking forward to part 2!

  4. Pingback: Amazing WordPress Hacks Part 1 : Freelance Folder

  5. By Dejan Cancarevic posted on June 25, 2008 at 8:45 pm

    Thomas,
    Are you looking for something like my friends list in sidebar?

    To print links of latest posts use this

    <?php $recent = new WP_Query(”showposts=5″); while($recent->have_posts()) : $recent->the_post();?>
    <li><a href=”<?php the_permalink() ? rel=”nofollow”>” rel=”bookmark” title=”Permanent Link to <?php the_title(); ?>”><?php the_title(); ?></a></li>
    <?php endwhile; ?>

  6. They don’t seem all that practical, but they’re interesting. I like the one to style a blurb.

  7. I love your blog design!

  8. Thanks you very much

  9. Thank you for sharing your expertise. I can see I will have to post a link on this great post too.

    Quick Question. What does this do?: ‘Forwarding section pages to their first sub-page’

    Thanks again!

  10. Hey man :) Beautiful design here.

    I noticed most of your posts are about WordPress, which is cool, being it’s a blog about blogging and all, but I thought I might introduce you to a new (beta) blogging / cms platform called Viviti (www.viviti.com) I’ve been designing on.

    I can send you a beta invite if you like. I’d love to get your feedback.

  11. Pingback: links for 2008-06-26 — SOJo: Student of Online Journalism by Megan Taylor

  12. Thanks Best Regards

  13. By Dejan Cancarevic posted on June 26, 2008 at 10:26 am

    Scott,
    Great, I’m really glad to hear that :D

    About your question it Redirects page to First Child (for example if you are using drop down menus etc..)

    Shawn,
    Sure, i would be happy to try it, it looks really good!

  14. Pingback: Rss VURSAKnoktacom » Blog Arşivi » Links for 2008-06-26 [del.icio.us]

  15. I think I’ve figured out a solution to my own question.

    To put posts into two columns of balanced length you can use the following code.

    post_count;
    $left_column = ceil($total_posts/2);
    $right_column = floor($total_posts/2); ?>

    <?php for ($i = 0; $i

    <?php for ($i = 0; $i

    I think you can do the same kind of thing for comments as well

  16. Sorry about the previous comment I forgot that HTML is enabled and WordPress ate my code!

    This should work:

    <?php if (have_posts()):
    $total_posts = $wp_query->post_count;
    $left_column = ceil($total_posts/2);
    $right_column = floor($total_posts/2); ?>
    <div id=”left”>
    <?php for ($i = 0; $i < $left_column; $i++) {
    the_post();
    the_content();
    }?>
    </div>
    <div id=”right”>
    <?php for ($i = 0; $i < $right_column; $i++) {
    the_post();
    the_content();
    }?>
    </div>
    <?php endif; ?>

  17. By Dejan Cancarevic posted on June 27, 2008 at 1:29 pm

    Thomas,
    looks ok, did you try it?

  18. @Dejan
    Yes I did test my little hack and it worked on WordPress 2.5.

    You can do the same for comments using $total_comments = count($comments);

  19. By Dejan Cancarevic posted on June 27, 2008 at 2:18 pm

    sure, i’m glad that’s solved now ;)

  20. Pingback: Trucos y hacks en PHP para tu blog de WordPress - elWebmaster.com

  21. Pingback: Websites you shouldn’t have missed in JUNE 2008

  22. Pingback: Skylog » Blog Archive » links for 2008-07-02

  23. I find giving the body-element an ID corresponding to the page you’re on a little handier than including different files:

    <body id=”<?php echo isset($_REQUEST['p']) ? $_REQUEST['p'] : ‘index’; ?>”-page>

    And then referencing that ID from your CSS when you want to style things differently depending on the page:

    #index-page #sidebar {
    display: none;
    }

    It’s also better for performance to only include one CSS-file and not to include different files on different pages.

    Beautiful design!

  24. Pingback: Amaizing WordPress hacks part 2 | StylizedWeb.com

  25. Pingback: links for 2008-07-14 | the markfr ditherings

  26. Pingback:   Table Of Contents Of Wordpress Tutorials, Helps, Tips and Tricks by aComment.net

  27. The first one, “Forwarding section pages to their first sub-page”? Could that be used to link to a category page, for example, and instead of getting the category.php file, you get the latest post in that category instead?

    (that’s something I’m trying to figure out, in case this doesn’t do it, but you know how it can be done!)

    Thanks.

  28. Pingback: The Giant List of Wordpress Hacks

  29. just thanks

  30. Pingback: The Giant List of Wordpress Hacks | Freelance Advice and Resources - Freelancer Magazine

  31. Pingback: Table Of Contents Of Wordpress Tutorials, Helps, Tips and Tricks | DevWebPro

  32. thanks admin exciting blog
    information is the most beautiful treasures

  33. thanks you
    You will have to crawl very nice,owe you gratitude..

  34. Pingback: Websites you shouldn’t have missed in JUNE 2008 | Serradinho

  35. perfect admin

  36. Thomas,
    looks ok, did you try it?

  37. Thanks for ones marvelous posting! I really enjoyed reading it, you will be a great author.
    I will remember to bookmark your blog and will come
    back sometime soon. I want to encourage you to continue your great writing, have a nice morning!

  38. This is the perfect site for anybody who would like to find out about this topic.

    You know a whole lot its almost hard to argue with you (not that
    I personally will need to…HaHa). You definitely
    put a new spin on a topic which has been written about for decades.
    Wonderful stuff, just wonderful!

  39. Hello there! I could have sworn I’ve been to your blog before but after looking at many of the articles I realized it’s new to me.

    Nonetheless, I’m certainly happy I came across it and I’ll be
    bookmarking it and checking back frequently!

  40. Incredible points. Great arguments. Keep up the amazing
    work.

    my web-site … Immigration Lawyers in kingston upon thames

  41. Useful information. Lucky me I discovered your web site
    accidentally, and I am surprised why this accident did not happened
    in advance! I bookmarked it.

  42. Hey there just wanted to give you a quick heads up.
    The text in your post seem to be running off the screen in Opera.
    I’m not sure if this is a format issue or something to do with internet browser compatibility but I thought I’d post to let you know.
    The style and design look great though! Hope you get the issue fixed soon.

    Cheers

  43. My brother recommended I might like this web site.

    He used to be entirely right. This put up actually
    made my day. You cann’t believe simply how much time I had spent for this info! Thank you!

  44. We stumbled over here coming from a different website and
    thought I may as well check things out. I like what I see so now i am following
    you. Look forward to looking into your web page yet again.

  45. It is in reality a nice and useful piece of info. I am happy that you shared this useful info with us.
    Please keep us informed like this. Thank you for
    sharing.

  46. Its like you read my thoughts! You appear to understand a lot
    about this, such as you wrote the book in it or something.
    I believe that you just could do with a few % to drive the message house a little bit, but instead
    of that, this is magnificent blog. A great read. I’ll certainly be back.

  47. My brother recommended I might like this website. He was
    entirely right. This post truly made my day.
    You cann’t imagine simply how much time I had spent for this info! Thanks!

  48. Every weekend i used to pay a quick visit this web page, because i wish for enjoyment, since this this
    website conations in fact good funny information too.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>