Code Snippets

Include tracking code in your header file.
This code also keeps from tracking any admin related activity in WordPress. In the example below, pages that include “preview” or “wp-admin” are excluded. As well, two IP addresses are being excluded.
Located near top of header.php.


<?php 
global $wp_query;
$page_id = $wp_query->post->ID;

$url= $_SERVER['PHP_SELF']; 
$ip= $_SERVER['REMOTE_ADDR']; 
$preview = (@$_GET['preview']) ? $_GET['preview'] : false; 
$isWPAdmin = strstr($url, "wp-admin");
$isfhcrc= strstr($ip, "000.111.000.111"); 
$ishome = strstr($ip, "111.000.000.111"); 

if((!$isWPAdmin) && (!$preview) && (!$isfhcrc) && (!$ishome))
{ 
?> 

insert javascript here

< ?php } ?>

 

Display post title following by blog name in bar of browser (great for bookmarking).
Located near bottom of header.php.


< title>
<?php wp_title(''); print_r(" | ");
 bloginfo('name'); ?>
</title>

 

Show category title and description for posts belonging to a category.
If the post is not in a category, the blog title and tag line will display. Located at bottom of header.php.


<h1><?php wp_title(''); ?></h1>
  < div class="description">

<?php
if( !is_page() )
 {
  $category = get_the_category();
  echo $category[0]->cat_name;
  print_r("
");
  echo $category[0]->category_description;
 }
else
{bloginfo('description');}
?>

</div>

 

Include the sidebar for single posts.
In SinglePost.php

<div id="content" class="narrowcolumn">

add the sidebar() above the footer at the bottom:

<?php get_sidebar(); ?>
<?php get_footer(); ?>

 

Includes not working?
If you’re having problems getting an include to work:

include "somefile.php";

Try this instead:

require_once(dirname(__FILE__) . '\\somefile.php');

 

Display something depending on the category you are in.
Change 3 to which ever category you are interested in.

<?php if ( in_category('3') ) { ?>
do something
?>

 

Place a news flash at the top of each posts in a certain category
Add this to the bottom of your header.php file. Then create a PHP file to hold news flash content and upload into your themes\yourtheme folder. The PHP file can be plain text. Using a PHP file for content now allows you to edit the content from Presentation | Theme Editor. Continue adding ORs to the first IF statement for additional categories. Then inside the main IF, add IF statements to handle each category.

<?php if(in_category('3')) { ?>
<div style="background-color: white;
	margin: 5px auto;
	padding: 5px;
	width: 660px;
	border: 1px solid #666666;
	font-size: 1.2em">
<?php if(in_category('3')){ 
$content = file_get_contents(TEMPLATEPATH . '/flashSomeCatName.php');
echo $content; }
?>
</div>
<?php } ?>

 

Remove >> from the title bar and preceding descriptions
Replace occurances of wp_title(); with
wp_title('’);


Categories:
  • Microsoft Certifications (12)
  • On WordPress (2)
  • Software and Mathematics (3)
  • 2 Responses to “Code Snippets”

    1. igor Says:

      Hi,
      How did you achieve expandable category list?
      Thanks

    2. Brett Romero Says:

      Hi Igor,

      This is the expand category code for the sidebar. Hope it helps.

      
      <!-- start expand categories -->
      <li>
      <h2>Categories</h2>
      <ul>
      <?php
      $categories = $wpdb->
      get_results("SELECT * FROM $wpdb->categories WHERE category_count > 0 
      ORDER BY cat_name DESC"); 
      // query database for categories with at least one post;
      
      $a = 1;
      
      foreach($categories as $category) : 
      // start loop to display category list
      ?>
      <li>
      <a href="<?php echo get_category_link($category->cat_ID); ?>">
      
      <?php if (!is_category() || is_page() || $category->cat_ID === $cat){ 
      echo $category->cat_name;} 
      else { echo"<font color=939392>" ;
      ?>
      
      <?php echo $category->cat_name; ?>
      </font>
      <?php } ?>
      
      </a>
      
      (<?php 
       echo $category->category_count; 
      ?>)
      
      <?php if( is_category() && ($category->cat_ID === $cat) ) : 
      // current category query is same as category presently in the loop
      ?>
       
      <ul>
      <?php
      
      $a = 1;
      $catq = new WP_Query("cat=$cat&showposts=-1"); 
      // query posts from current category
      
      while($catq->have_posts()) : $catq->the_post(); 
      // start posts in category loop
      ?>
      
      <li>- <a href="<?php the_permalink(); ?>">
      
      <?php
        if (($a % 2) == 1)
        { echo "<font color=3366ff>" ;}
        if (($a % 2) == 0)
        { echo "<font color=ff0000>" ;}
        $a = $a + 1;
        ?>
      
      <?php the_title(); ?></a> </li>
      <?php
                      endwhile; 
      // end posts in category loop
      ?>
      
      </ul>
      <?php endif; // end current category query test ?></li>
      <?php endforeach; // end loop to display category list ?> 
      
      </ul>
      <!-- stop expand categories -->
      

    Leave a Reply

    You must be logged in to post a comment.