How to get post content from outside the loop in WordPress

Share this:
Share

Dear Reader,

Are there times when you want to get post content from outside the loop in WordPress? or wanted to control post content from outside the loop?

In this article we’ll discuss how you can get non exclusive post content, or control some data about the presently showed post outside of the Loop.

WordPress gives a few capacities to work on sets of posts for even better grain control over post show. Alongside access to worldwide factors, there are an arrangement of WordPress capacities to return non specific data that is not particular to a solitary post, or the post at present showed.

Outside the Loop functions

Taking after is a rundown of much of the time utilized outside the Loop capacities:

➤ wp_list_ pages(): Displays a rundown of pages as connections

➤ wp_list_categories(): Displays a rundown of classes as connections

➤ wp_list_bookmarks(): Displays joins spared in the Links SubPanel

➤ wp_tag_cloud(): Displays a label cloud from all labels

➤ get_ permalink(): Returns the permalink of a posts

➤ next_ posts_link(): Link to show next posts

➤ previous_ posts_link(): Link to show past posts

Navigational Links in Posts

You as of now perceived how you could make navigational connections utilizing next_ posts_link() and previous_ posts_link() in the custom Loop illustration. How about we investigate some of these capacities in real life to get a genuine vibe for how they function.

List Pages

Most menus in a WordPress topic are produced utilizing the wp_list_ pages() work. This capacity will give back your pages in a rundown design, so it’s vital to wrap the capacity call with <ul> labels as appeared here:

Menus in WordPress

The previous code would create a rundown of pages from WordPress with connections. See you set the parameter title_li to nothing, which takes out the default title showed for your pages. The capacity would produce your menu list like so:

You could likewise utilize the more current wp_ page_menu() capacity to create a page menu. There are a few favorable circumstances to this more up to date menu work.

The first is another show_home parameter permitting a Home connection to naturally be added to the rundown of pages. You likewise don’t need to evacuate the title utilizing title_li as we did in the former code.

This capacity likewise wraps a custom <div> around your menu, the class of which you can set. A case of this capacity takes after:

List Categories

Another normal capacity for creating menus is wp_list_categories(). This capacity records your classifications, and subcategories, in a rundown also. Take a gander at an illustration:

This code will produce a rundown of classes with connections. Similarly as before you are setting your title to nothing, as opposed to the default “Categories” title.

You are additionally setting the profundity to 3. The profundity parameter controls what number of levels in the pecking order of classifications to be incorporated into the rundown.

The classes will be requested by their name. You are additionally barring three classifications (6, 14, and 38) in view of their IDs.

next_ posts_link() and previous_ posts_link() are regularly utilized specifically after your Loop has finished. These two capacities will produce the past and next connections for survey more posts on your site.

See that the next_ posts_link() work really gives back your past posts. The explanation behind this is WordPress accept your posts are showing backward sequential request, which means the following page of posts would really be posts from prior in the course of events.

Get Post Content

Presently envision you’d jump at the chance to stack a solitary post outside of the Loop. To do this you utilize the get_ post() capacity to stack your post information. The accompanying case stacks the post information for post ID 178:

The get_ post() work just has one required parameter: the post ID you need to stack. You should pass a variable containing a whole number for the ID.

Passing an exacting number (for instance, 5) will bring about a deadly mistake. The second discretionary parameter is the way you might want the outcomes returned: either a protest, an acquainted cluster, or a numeric exhibit.

Naturally a question is returned. To give back an acquainted exhibit you could run this code:

Regardless of how you give back the outcomes, however, this summon of get_ post() gives back the crude substance from the WordPress database.

Channels and handling typically done inside the circle won’t be connected to the returned content.

The arrangement is to utilize the setup_ postdata() work in conjunction with get_ post() to set up your worldwide post information and format labels for use with your post:

The get_ post() work utilizes the inward WordPress protest reserve. This implies if the post you are stacking is as of now in the store you will save money on running an unneeded database inquiry.

It’s anything but difficult to perceive how helpful this capacity can be to rapidly and effectively stack a solitary post outside of the Loop.

Get Author Meta

A few capacities that can be utilized inside the Loop can likewise be utilized outside of the Loop. For instance, you can utilize the the_author_meta() capacity to recover particular creator meta:

The email address for client id 1 is <?php the_author_meta(‘user_email’,1); ?>

Keep in mind that when calling the the_author_meta() work outside of the Loop you need to determine the creator’s ID that you need to load metadata for.

On the off chance that you call this capacity inside the Loop you don’t have to determine this ID, since it will stack the creator information for the present post.

Get Post title

WordPress likewise highlights particular capacities for recovering individual information about a post outside of the Loop. For instance, you can utilize the get_the_title() capacity to recover a post’s title in view of post ID like so:

Get Post metadata

You can likewise utilize a capacity to recover post metadata (custom fields) from an individual post. To do this you utilize the get_ post_meta() work as appeared here:

The get_ post_meta() work acknowledges three parameters: post ID, key, and single. The post ID is the ID of the post you need to load metadata for. The key is the name of the meta esteem you need to stack.

The third discretionary esteem figures out if the outcomes are returned as an exhibit or whether the capacity will give back a solitary outcome.

As a matter of course this is set to False so an exhibit would be returned. As should be obvious, you set this esteem to True so just a solitary shading is returned.

We’ve secured the essential mechanics of WordPress substance determination and show, and gave a manual for the WordPress center to help you find the code used to execute these capacities.

The genuine force of WordPress is in its extensibility through modules and topics.

What are your thoughts about this post? As always, I’d love to hear your thoughts!

Did you like this article about managing post content from outside the loop in WordPress? Share it with your friends on Facebook, Twitter, and Google Plus!

Don’t forget to Subscribe to our Newsletter for more insightful post updates, tips and guides!

See you in the next post.

Stay awesome!

Authored By Aakanksha Singh

Hi, I’m Aakanksha, a computer science engineer and the founder of TheTechPower. I help people make there online presence process easier by empowering them with clean, beautiful and easy to use wordpress themes.

Share this:
Share

7 Comment

  1. Yanira says: Reply

    Excellent Web-site, Preserve the useful work. Thanks a lot. http://www.mfpc.tv/ch/userinfo.php?uid=1269485

    1. Aakanksha says: Reply

      Thanks for the wonderful comment.

  2. I found you from Travel Blogger link up, I have no idea what any of this is, but Im sure its helpful for the right audience.

  3. This is such a comprehensive guide to getting content outside the loop of WordPress. Thanks for sharing these tips and step-wise directions. Will try it out soon. Cheers!!

  4. Thanks for this useful content. I am still struggling with the technical part of maintaining a blog. I hope your site will help me.

  5. Chloe says: Reply

    Hi, thank you for sharing these. I shall be saving this info for future reference.

  6. Nice Article. Thank you for sharing. It is useful for newbie bloggers.

Leave a Reply