WordPress Popular Snippets
Snippet to insert a .PHP file into a template file
This is very useful when you have a section that you want to repeat throughout site.
For example, I often create a PHP file just for a section of ads that are used throughout a site. Then in my template files I deploy the following code to call up the PHP file wherever I want the ads to show up; this way any changes I make to the PHP ads file will reflect throughout my site.
<?php include(TEMPLATEPATH."/YOUR_PHP_FILE_NAME.php"); ?>
Snippet to create a WordPress Page template
Duplicate either the “home.php” file or the “index.php” file in your template folder, then add this code at beginning of it. Rename the modified file.
<?php /* Template Name: PAGE TEMPLATE NAME */ ?>
Snippet to create a WordPress Post template
Use this with the the Custom Post Plugin of Simon Wheatley which enables sites to have multiple templates for posts.
Duplicate a template file, either the “home.php” or the “index.php” in your template folder, then add the following code at beginning.
<?php /* Template Name Posts: POST TEMPLATE NAME */ ?>
get_posts, WordPress’ most powerful template tag
What makes this tag more powerful than most is its endless customization possibility; I use it to produce contents of all pages under the cPanel header on the left, and the results are all different.
<?php
global $post;
$myposts = get_posts('CUSTOM_ARGUMENTS');
foreach($myposts as $post) :
setup_postdata($post);
?>
CUSTOMIZATION OF THE RESULT FOR EACH POST GOES HERE
?>
See full documentation on the get_posts tag.
