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
<?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
}
?>
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.