Important WordPress PHP coding shortcuts for a Beginner

Wordpress

If you are a WordPress beginner, it is an absolute requirement that you understand how to shorten your PHP source code when you are editing your template. A lot of beginner mistakes are the inefficiency in coding. They simply do not know that WordPress already has lots of short coding methods to provide that certain functionality. The WordPress Codex is an immense library of information and since beginners often do not know what to search and when to begin. Preliminary important information would be presented in this tutorial. Once you get familiar with the basics you can do the rest by yourself.

WordPress Conditional tags

Purpose: Coding shortcuts for determining whether the page loaded is a WordPress post, archive, tags, post pages, search result, categories, 404 page and many more.

Requirement: WordPress must be loaded before you can use these conditional tags.

Example:

Supposing you would like to display a text “The quick brown fox jumps over the lazy dog” in the header section of your site but would like to appear ONLY in your WordPress homepage. In this implementation you can use the conditional tag:

is_front_page()

This conditional tag would return true if the page loaded is the WordPress homepage. And as always use PHP IF to test the condition:

<?php
if (is_front_page()) {
echo 'The quick brown fox jumps over the lazy dog';
}
?>

And there are tons of WordPress conditional tags that you can use as shown in this list:

Below are the most common uses of conditional tags in WordPress:

1.) Show Adsense Banners at the header section of the site only in the blog posts but not in the homepage or any other pages in the site.
2.) Show customized widget or content in the sidebar only if it’s the homepage.
3.) Formulating unique title tags which are different for the homepage, posts, category, etc.
4.) Loads JavaScript on the header or footer for specific type of pages only as needed for faster website loading.

It is recommended to look into the WordPress conditional tags list when writing a PHP code for WordPress because it can help your efficient at coding.

the_permalink() and get_permalink

Have you ever thought of retrieving any URL in your WordPress blog? Well there are obvious PHP coding solutions that are outside of WordPress functions but it’s too hacky to incorporate in your WordPress coding projects.

Purpose: Coding shortcuts for retrieving any URL in your WordPress page.
Requirement: WordPress must be loaded before you can use these functions.
Example:
The best way to use the_permalink() and get_permalink functions is to look at the following examples.

1.) Try this in your WordPress site (local host installation highly recommended). In your header.php template just after <head>, put this code:

<?php the_permalink(); ?>

2.) Then in your WordPress footer, just before </body> copy and paste the code below:

<?php
global $wp_query;
$thePostID = $wp_query->post->ID;
$url=get_permalink($thePostID);
echo $url;
?>

3.) Save both templates and then reload any WordPress URL. You will notice that the results of the two different codes above are the same. It is because they have the same function – retrieving the URL of the loaded WordPress page. What makes the two different in their applications?

First, the_permalink(); can be used within the WordPress loop. If for some reason this does not provide any output, it is because you might be using the function outside the WordPress loop so you need to try get_permalink approach.

Name of author, date, post title and many more

Purpose: WordPress functions providing all information relating to the post which can be obtained readily instead of querying the database the whole time with custom code.

Requirement: WordPress must be loaded before you can use these functions. These functions should be used within the loop.

Example: WordPress already has a lot of pre-defined functions that you can use if you are going to obtain any information relating to the blog post. You do not need to write a complicated PHP code just to obtain that information from the WordPress MySQL database. The most common examples:

1.) Obtaining the name of the author:

This great post was made possible by <?php the_author(); ?>.
If for some reasons there are no values outputted on this function, you are using this function outside of WordPress loop. It is suggested to use the one below:

<?php
global $post;
$id=$post->post_author;
$parameter='nickname';
?>

This great post was made possible by <?php the_author_meta($parameter,$id ); ?>.

It is possible to display not only by nickname but my firstname, lastname. You can refer to the complete list of parameters here.

2.) Obtaining the WordPress post title

<?php
//Display WordPress title of the loaded page
wp_title();
?>

More options here:

3.) Date of the post

<?php
the_date('F j, Y');
?>

This will output like March 14, 2012

4.) Loading WordPress header in non-Wordpress page

Supposing you are creating a custom page (which is not created or published by WordPress) but you would like WordPress to be loaded in order to use the template tags and functions. Put this code on top of the PHP script:

<?php
require('/path/to/your/wp-load.php');
//load wordpress header
get_header();
?>


Discover more from Hosting Mate

Subscribe to get the latest posts sent to your email.

Similar Posts