wordpress Include Any File From Your Theme
<?php include (TEMPLATEPATH . '/your-file.php'); ?>
<?php include (TEMPLATEPATH . '/your-file.php'); ?>
There are built in conditional WordPress functions for testing for a page:
if ( is_page(2) ) {
// stuff
}
Or for testing if a page is a child of a certain page:
if ( $post->post_parent == '2' ) {
// stuff
}
But there is no built in function that combines these two things, which is a fairly common need. For example, loading a special CSS page for a whole "branch" of content. Like a "videos" page and all its children individual videos pages.
This function (add to functions.php file) creates new logical function to be used in this way:
function is_tree($pid) { // $pid = The ID of the page we're looking for pages underneath
global $post; // load details about this page
if(is_page()&&($post->post_parent==$pid||is_page($pid)))
return true; // we're at the page or at a sub page
else
return false; // we're elsewhere
};
if (is_tree(2)) {
// stuff
}
<?php
$url = explode('/',$_SERVER['REQUEST_URI']);
$dir = $url[1] ? $url[1] : 'home';
?>
<body id="<?php echo $dir ?>">
This would turn http://domain.tld/blog/home into "blog" (the second level of the URL structure). If at the root, it will return "home".
Here is an alternate method:
<?php
$page = $_SERVER['REQUEST_URI'];
$page = str_replace("/","",$page);
$page = str_replace(".php","",$page);
$page = str_replace("?s=","",$page);
$page = $page ? $page : 'index'
?>
This would turn http://domain.tld/blog/home into "domaintldbloghome", which is far more specific. It also will remove ".php" file extensions and the default WordPress search parameter.
function curr_virtdir($echo=true){
$url = explode('/',$_SERVER['REQUEST_URI']);
$dir = $url[1] ? $url[1] : 'home'; // defaults to this if in the root
$dir = htmlentities(trim(strip_tags($dir))); // prevent injection into the DOM through this function
if ($echo) echo $dir;
return echo $dir; // ie. curr_virtdir(false)
}
function get_curr_virtdir(){
curr_virtdir(false);
}
Returns the "middle" directory value:
On http://css-tricks.com it would return "home"
On http://css-tricks.com/snippets it would return "snippets"
On http://css-tricks.com/forums/viewforum.php?f=6 it would return "forums"
The strip_tags() and htmlentities() functions prevent malicious code from being insterted into the URL and ran, e.g.
<body id="foo"><script>alert("Booo");</script>
Usage for IDing the body:
<body id="<?php curr_virtdir(); ?>">
Other usage:
<?php
if ( get_curr_virtdir() == "store" ){
echo "Welcome to our awesome store !";
} elseif ( get_curr_virtdir() == "home" ){
echo "Welcome home :-)";
} else {
echo "Welcome on some other page";
}
?>
Might as well keep your header.php clean and insert the shim from the functions.php file.
// add ie conditional html5 shim to header
function add_ie_html5_shim () {
echo '<!--[if lt IE 9]>';
echo '<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>';
echo '<![endif]-->';
}
add_action('wp_head', 'add_ie_html5_shim');
The shim is to enable HTML5 elements to be able to be styled through CSS in Internet Explorer versions less than 9.
Let's say you wanted to use the post thumbnail feature of WordPress, but had a whole archive of posts that would take too much time to go through. For new posts, you can be specific and use the feature as intended. For old posts, you just want to use the first image it finds in the content for the thumbnail, or a default if none present.
Add this to functions.php or make a functionality plugin:
function catch_that_image() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches[1][0];
if(empty($first_img)) {
$first_img = "/path/to/default.png";
}
return $first_img;
}
To use it, use this code in the loop:
if ( get_the_post_thumbnail($post_id) != '' ) {
echo '<a href="'; the_permalink(); echo '" class="thumbnail-wrapper">';
the_post_thumbnail();
echo '</a>';
} else {
echo '<a href="'; the_permalink(); echo '" class="thumbnail-wrapper">';
echo '<img src="';
echo catch_that_image();
echo '" alt="" />';
echo '</a>';
}
I found that has_post_thumbnail wasn't as reliable as the logic above.
Add to functions.php file:
function get_ID_by_page_name($page_name) {
global $wpdb;
$page_name_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name = '".$page_name."' AND post_type = 'page'");
return $page_name_id;
}
Now you can use this function in templates when you need an ID of a specific post/page and all you have is the name.
Post thumbnails are pretty useful and pretty easy to use in WordPress. Simply add:
add_theme_support('post-thumbnails');
To a theme's functions.php file and you'll get a Featured Image module on the admin screen for posts which allows you to select one.
It is also very easy to output that image as an HTML <img>:
the_post_thumbnail();
But what if you just need the URL? Say, you're going to use it as a background-image on an element rather than a content image. Unfortunately there is no super easy/obvious function for that.
Within the loop, you'll have to do:
$thumb_id = get_post_thumbnail_id();
$thumb_url_array = wp_get_attachment_image_src($thumb_id, 'thumbnail-size', true);
$thumb_url = $thumb_url_array[0];
Then $thumb_url will be that URL.