StylizedWeb is a blog about web trends and tutorials, KnowHow community forum designed and maintained by Dejan Cancarevic who also runs Serbian IT job board at itPoslovi.info

Categories Archive

Popular Posts
Advertisement
My Site is Ready, What Next??? Advertise on Stylized Web Najbolje ponuda IT poslova u Srbiji na itposlovi.info Advertise on Stylized Web
Amazing WordPress hacks part 1
add to Save to Delicious Save to Stumble Save to Digg

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

What do you think?





28 Responses so far

By Adam Kayce
on July 24, 2008

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.

By Andreas
on July 11, 2008

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!

By Dejan Cancarevic
on June 27, 2008

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

By Thomas Milburn
on June 27, 2008

@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);

By Dejan Cancarevic
on June 27, 2008

Thomas,
looks ok, did you try it?

By Thomas Milburn
on June 27, 2008

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; ?>

By Thomas Milburn
on June 27, 2008

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

By Dejan Cancarevic
on June 26, 2008

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!

By mirc
on June 26, 2008

Thanks Best Regards

By Shawn Adrian
on June 26, 2008

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.

By Scott
on June 26, 2008

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!

By mirc
on June 26, 2008

Thanks you very much

By James Kurtz III
on June 25, 2008

I love your blog design!

By Devon Young
on June 25, 2008

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

By Dejan Cancarevic
on June 25, 2008

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; ?>

By Thomas Milburn
on June 25, 2008

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!

By John
on June 25, 2008

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

  1. Jul 29, 2008: The Giant List of Wordpress Hacks
  2. Jul 15, 2008:   Table Of Contents Of Wordpress Tutorials, Helps, Tips and Tricks by aComment.net
  3. Jul 14, 2008: links for 2008-07-14 | the markfr ditherings
  4. Jul 13, 2008: Amaizing WordPress hacks part 2 | StylizedWeb.com
  5. Jul 2, 2008: Skylog » Blog Archive » links for 2008-07-02
  6. Jul 1, 2008: Websites you shouldn’t have missed in JUNE 2008
  7. Jun 27, 2008: Trucos y hacks en PHP para tu blog de WordPress - elWebmaster.com
  8. Jun 27, 2008: Rss VURSAKnoktacom » Blog Arşivi » Links for 2008-06-26 [del.icio.us]
  9. Jun 26, 2008: links for 2008-06-26 — SOJo: Student of Online Journalism by Megan Taylor
  10. Jun 25, 2008: Amazing WordPress Hacks Part 1 : Freelance Folder
  11. Jun 25, 2008: Amaizing WordPress hacks part 1