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.
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.
Excluding Specific Pages from a List
You can also exclude specific pages using the exclude parameter:
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:
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:
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.
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:
The above outputs:
Getting the URL to the Current Theme
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:
If you wanted to create a link to your RSS feed, you could use the following:
Working with Content
The WordPress loop is used to display your posts. A basic loop looks like this:
- <?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.
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:
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.
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:
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:
Display the URL of the post:
Display the content of the post:
Display the excerpt of the post:
Display the category of the post:
Display the tags used in the post:
Display the time the post was published (uses PHP date formatting as a parameter):
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).
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.
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:
The code from above should output the following:
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