thanks admin exciting blog
information is the most beautiful treasures
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!
<?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 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); } }
/*** 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"); }
***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 } ?>
Have some feedback? Leave a commentthanks admin exciting blog
information is the most beautiful treasures
just thanks
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.
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!
sure, i’m glad that’s solved now
@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);
Thomas,
looks ok, did you try it?
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; ?>
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
Scott,
Great, I’m really glad to hear that
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!
Thanks Best Regards
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.
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!
Thanks you very much
I love your blog design!
They don’t seem all that practical, but they’re interesting. I like the one to style a blurb.
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; ?>
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!
oh, we can do all that! ![]()
can’t wait for part two, thanks a lot man!
on February 2, 2010
thanks you
You will have to crawl very nice,owe you gratitude..