Posts Tagged ‘Wordpress’

Helpful WordPress Code Snippets

No Comments »

Theme / Template

A theme is the folder that holds all of your files. For example, the new WordPress theme is TwentyEleven.

Many people start with the theme that comes with the WordPress.org download and build upon that. You can rename the folder to be the name of your site, the version number, etc. At the top of your stylesheet style.css, you can change the name as well. This will help you identify what theme you’re using if you have several options.

A template is one file instead of a folder. A page that almost always has its own template is the homepage. To make a template, create a new file inside your theme. Avoid using home.php because WordPress has already set this page up in its functions. Use home-template.php, homepage.php, etc.

At the top of your template put this code:

<?
/*
Template Name: Home
*/
?>

The template name is commented out, but WordPress still pulls the name. In your dashboard, create a new page. On the right side, you will see a dropdown for template name. Choose the relevant option.

Image Source

Working in PHP you’ve probably created a variable that’s equal to the URL of your site. For example:

$SITE_URL = "http://www.onextrapixel.com";

And then you use the variable like this:

<img src="<?php echo "$SITE_URL"; ?>/images/myimage.jpg" alt="An Image" />

This functionality does not work in WordPress, so you have to use an alternative way to pull the location of the image:

<?php bloginfo('template_directory'); ?>

Example:

<img src="<?php bloginfo('template_directory'); ?> /images/myimage.jpg" alt="An image" />

Site URL Shortcut

Solution: Just like the previous example, WordPress has created a function that allows you to reference your site URL quickly. This is also helpful if one day you decide to change your domain or if you’re using a test site. By using this code, you won’t have to change the files in the future to the new domain; it will automatically pull.

<?php bloginfo('url'); ?>

Example:

<a href="<?php bloginfo('url'); ?>/about">About Our Company</a>

Conditional Statements

You will need to create a conditional statement in order to make the CSS change. For example, let’s say we want one stylesheet for the homepage and another for the about page. Let’s say you created templates (explained above) called about-template.php and home-template.php.

1. Call to the page template:

<?php if (is_page_template ('home-template.php')) { ?>
<link rel="stylesheet" type="text/css" media="all" href="<?php bloginfo('template_directory');?>/home.css" />
<?php } ?>
<?php if (is_page_template ('about-template.php')) { ?>
<link rel="stylesheet" type="text/css" media="all" href="<?php bloginfo('template_directory');?>/about.css" />
<?php } ?>

This particular code would go in your header.php file. You can do this two ways:

2. Call to the page name:

<?php if (is_page ('home')) { ?>
<link rel="stylesheet" type="text/css" media="all" href="<?php bloginfo('template_directory');?>/home.css" />
<?php } ?>
<?php if (is_page ('about-us')) { ?>
<link rel="stylesheet" type="text/css" media="all" href="<?php bloginfo('template_directory');?>/about.css" />
<?php } ?>

It’s useful to call to the page template when you have several pages using the same format. For example, if you had a website with a corporate site and smaller sites based on region, you could specify that the corporate site uses one style corporate-template.php and the microsites use a different style microsite-template.php. This saves you from having to call to every single page and instead use the template shared by all.

Post Recent Blogs

Place following code into your page:

<?php query_posts($query_string . '&showposts=10' ); ?>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <div class="story">
     <div class="story-content">
         <h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
        <?php the_excerpt(); ?>
     </div>
    </div>
<?php endwhile; endif; ?>

The section, &showposts=10 states to show ten of your most recent posts.

How to exclude a specific category:

<?php query_posts($query_string . '&cat=-6&showposts=10' ); ?> 

This means ten of your posts will show, but it will exclude anything in the category with the ID of 6.

To show posts in a specific category:

<?php query_posts($query_string . '&cat=6&showposts=10' ); ?>

This means six of your posts will show, but it will only show posts in the category with the ID of 6.