Working outside the loop in WordPress

If you want to manipulate some information about the currently displayed post outside of the Loop or want to access the generic post information. The WordPress facilitate some functions to operate on sets of posts for even finer-grain control over post display. Global variables with access, so WordPress has a functions to return the generic information and it not only for specific to a single post, or the post currently displayed.

Below is a list of commonly used functions when working outside the Loop and Web Development London often use this:

➤wp_list_pages() — This use for displays a list of pages as links

➤wp_list_categories() — This use for displays a list of categories as links

➤wp_list_bookmarks() — This use for displays links saved in the Links SubPanel

➤wp_tag_cloud() — This use for displays the tag it cloud from all tags

➤get_permalink() — This use for returns the permalink of a post

➤next_posts_link() — This use for link to display previous posts

➤previous_posts_link() — This use for link to display next posts

The previous articles you have already seen that how you could create the navigational links using previous_posts_link() and next_posts_link() in the custom Loop example. Let’s now explore some of these functions to get that how these function works.

The wp_list_pages() function is use to display a list of pages in WordPress.

The pages will be in a list form if you use this function, so good practice is that you should to use this function under the <ul> tags, as you can below examples:

<ul>

<?php wp_list_pages( ‘title_li=’ ); ?>

</ul>

Above code would generate a list of pages from WordPress with links also. The parameter title_li use to nothing, it eliminates the default title displayed for your pages. This function will generate your menu list like that:

<ul>

<li class=”page_item page-item-1″>

<a href=”http://intylestyle.co.uk /about/” title=”About”>About</a>

</li>

<li class=”page_item page-item-2″>

<a href=”http:// intylestyle.co.uk/port-folio/” title=”Port Folio”>Port Folio</a>

</li>

<li class=”page_item page-item-3″>

<a href=”http:// intylestyle.co.uk/contact/” title=”Contact”>Contact</a>

</li>

</ul>

The wp_page_menu() can also use this function to generate or create a page menu. Many advantage of use this page listing function in the code. A Home link to automatically be added to the list of pages after use this and you also don’t have to remove the title using title_li as done in the preceding code.

A wrapper div also create around the menu so it is facility to set a class and set alignment which you want.

Below is an example of this function:

<?php wp_page_menu( ‘show_home=1&menu_class=my-menu&sort_column=menu_order’ ); ?>

For creating or generating the links another function wp_list_categories() is use. By this function the lists of your categories, and subcategories, in a list as well. See the following example:

<ul>

<?php wp_list_categories( ‘title_li=&depth=4&orderby=name&exclude=8,16,34’ ); ?>

</ul>

Above code will generate a list of the categories with links. In previous code, you are setting your title to nothing, rather than the default Categories title.

The order of categories set by their names. Categories can also exclude by the their id s link 36,38 and 57. After the loop these functions next_posts_link() and previous_posts_link() are typically used directly.

Above two functions will generate or displayed the previous and next links for viewing more posts on your website. The next_posts_link() function actually creates your previous posts. The reason of this function is that the WordPress assumes your posts are displaying in reverse order, it means that the next posts would actually be posts from earlier in the timeline post generate recently.

If you want to display a single post outside the Loop then you use the get_post() function to load your post data or post content.

Below example loads the post data the has 157 ID.

<?php

$my_id = 157;

$myPost = get_post( $my_id );

echo ‘Post Title: ‘ .$myPost->post_title .'<br />’;

echo ‘Post Content: ‘ .$myPost->post_content .'<br />’;

?>

The get_post() only one parameter function required: you want to load the psot ID. So you must pass a variable it has an integer value of the post ID.

If you pass the integer value like that (,7) it will cause a fatal error. The other optional parameter is that how you would like the result returned formed it is an object, an associative array, or a numeric array. This function by default returned as an object. If you want to return an associative array then you can run this code:

<?php

$my_id = 1031;

$myPost = get_post( $my_id, ARRAY_A );

echo ‘Post Title: ‘ .$myPost[‘post_title’] .'<br />’;

echo ‘Post Content: ‘ .$myPost[‘post_content’] .'<br />’;

?>