WordPress Development: Common Functions analysis

June 1st, 2012 by Laeeq | 1 comment

WordPress is a popular blogging and CMS platform. Its a open source free php plateform where you can create a beautiful website or blog. It is easy to use and customize, and there’s basically nothing you can’t do with it.

In this article, we will take a look at some common functions in WordPress.

WordPress has many useful PHP functions. Some of the functions, known as Template Tags that are defined especially for use in WordPress Themes
These functions are an essential component of WordPress theme development. once you understand how they work, it’s easy to create your own custom WordPress themes.

WordPress Navigation functions

WordPress is popular for its navigation-related functions like wp_list_pages and wp_nav_menu function

We’ll talk about these two functions, starting with wp_list_pages.

Listing All Pages

If You want to list all of the pages , there’s a simple function for that called wp_list_pages. When used without any parameters, it will list all of your pages in alphabetical order.

  1. <?php wp_list_pages(); ?>

Listing Specific Pages

wp_list_pages function that lists all pages, also takes several parameters. For example, the include parameter allows you to list specific pages by referencing their page IDs, separated by commas (,). The following example will only list two pages with IDs of 4 and 5.

  1. <?php wp_list_pages(‘include=4,5′); ?>

Excluding Specific Pages from a List

You can also exclude specific pages using the exclude parameter:

  1. <?php wp_list_pages(‘exclude=4,5′); ?>

Pages Sorting

In WordPress the default sorting order of wp_list_pages is alphabetical. You can, however, change the order of the listing using the sort_column parameter. The sort_column parameter can have 1 of 7 values:

  • post_title – Sort alphabetically (default value)
  • menu_order – Sort by page order
  • post_date – Sort by date of creation
  • post_modified – Sort by time last modified
  • ID – Sort by page ID
  • post_author – Sort by the page author ID
  • post_name – Sort alphabetically by post slug

Here is the code for sorting by creation date instead of the default alphabetical order:

  1. <?php wp_list_pages(‘sort_column=post_date’); ?>

Specifying the Depth

In wordpress pages can have subpages, and subpages can have subpages. What if you only wanted to list top-level pages but exclude their subpages? Controlling the depth works great when using it to generate dropdown menus with submenus.

You can use the depth parameter like so:

  1. <?php wp_list_pages(‘depth=1′); ?>

Showing Blog Information

Thre are many wordpress functions are availble for getting and printing your blog information called bloginfo. This is a good function to use for themes. There are many parameters you can use for bloginfo. You can read the bloginfo WordPress Codex documentation.

Getting the Site’s URL

Let’s say your site’s URL is http://example.com. If you want to print this out in the source, you can use the url parameter.

  1. <?php bloginfo(‘url’); ?>

it works perfect for absolute link references. For example, if you wanted to reference your logo that is in a directory called images, you would do the following:

  1. <img src=“<?php bloginfo(‘url’); ?>/images/logo.png” />

The above outputs:

  1. <img src=“ http://example.com/images/logo.png” />

Getting the URL to the Current Theme

You can use the template_url parameter for getting theme directory path. it will make your WordPress themes more flexible so that when you change the domain name or use it in multiple domain names you don’t have to worry about changing anything that references the theme’s location. You can use this parameter a number of ways, such as for referencing custom external stylesheets, images, and JavaScript libraries that are inside the theme directory.

  1. <?php bloginfo(‘template_url’); ?>

Getting the URL of Your RSS Feed

The bloginfo function can also be used for getting other URLs. For example, if you want to grab the RSS feed URL for your site, you can use the ‘rss2_url’ parameter:

  1. <?php bloginfo(‘rss2_url’); ?>

If you wanted to create a link to your RSS feed, you could use the following:

  1. <a href=“<?php bloginfo(‘rss2_url’); ?>”>Link to RSS feed</a>

Working with Content

The WordPress loop is used to display your posts. A basic loop looks like this:

  1. <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?><?php endwhile; endif; ?>

Not much to look at right now, but it’s what’s going to go inside the loop that really matters.

Querying Posts

query_posts is the most important function of wordpress . query_posts only needs to be used if you want to display information from another page, post or category than the one the user is currently on. For example, on the front page index.php, home.php, or front-page.php theme template files, you can use query_posts to show the three newest posts.

You use query_posts not only to show different kinds of content, but how much content matches the query as well.

Displaying Posts in a Category

Keeping with our Featured category example above, here’s how we’d show the three newest posts from the Featured category:

  1. <?php query_posts(‘category_name=Featured&posts_per_page=3′); ?>

functions with more than one parameter, you can pass multiple parameters all at once by separating them with an ampersand (&). You can pass as many parameters as you want. Doing so allows you to increase the specificity of your desired outputs.

Excluding Posts

Similar to wp_list_pages, query_posts has a way to exclude items from being displayed. To do so, you just place a minus character (-) in front of the ID of the items you want to exclude.

For example, if you would like to list all posts except posts from two categories with category IDs of 97 and 34. You could use the cat parameter for query_posts:

  1. <?php query_posts(‘cat=-97,-34′); ?>

Displaying Common Information

WordPress have many functions we can use inside the loop. Many of these functions can only be used inside the loop and may not work if used outside of it.

Display the title of the post:

  1. <?php the_title(); ?>

Display the URL of the post:

  1. <?php the_permalink(); ?>

Display the content of the post:

  1. <?php the_content(); ?>

Display the excerpt of the post:

  1. <?php the_excerpt(); ?>

Display the category of the post:

  1. <?php the_category(); ?>

Display the tags used in the post:

  1. <?php the_tags(); ?>

Display the time the post was published (uses PHP date formatting as a parameter):

  1. <?php the_time(); ?>

Working with Custom Fields

We can also custom fields in wordpress. actually wordpress allow users to add custom name/value pairs typically used for post metadata. For example, users can add a post_thumbnail_url key that has a URL value pointing to the thumbnail image.

Users can add custom fields while they are creating posts and pages in Posts > Add new or Pages > Add new.

Using Custom Fields to Display Image Thumbnails

Let’s say you want to display an image thumbnail in a post.

First, you must create a new post . Under the Custom Fields fieldset, type Thumbnail into the Name field and a URI for the Value field. Then publish the post.

Wherever in the loop you want the URI to be displayed, simply use the echo statement to output the result of get_post_meta (which is a function for getting custom fields).

  1. <?php echo get_post_meta($post->ID, ‘Thumbnail’, true); ?>

To use get_post_meta outside the loop, change $post->ID to the ID of the post. For example, here’s how to print the URI of the thumbnail image for the post with ID of 6.

  1. <?php echo get_post_meta(6, ‘Thumbnail’, true); ?>

Since we want to display an image, what we actually need to do is use the echo statement inside the src attribute of an img element:

  1. <img src=“<?php echo get_post_meta(6, ’Thumbnail’, true); ?>” />

The code from above should output the following:

  1. <img src=“/images/thumbnail.jpg” />

Inserts posts

WordPress have have functions to insert post in to the database. This function checks and sanitizes variables fills in missing variables like date/time, etc. the function takes an object as its argument and returns the post ID of the created post

  1. <?php wp_insert_post( $post, $wp_error ); ?>

You can subscribe to PHPZAG.COM posts by Email

 

Related Topics:

 

 

  1. angelinataylorgb
    October 3rd, 2012 at 12:30 | #1

    hello!,I like your writing so much! percentage we be in contact extra about your post on AOL? I require a specialist on this space to unravel my problem. Maybe that’s you! Looking forward to look you.

  1. No trackbacks yet.