PliablePress Blog

Tips, News & Previews

How to Embed a Category in a Page

On the form, Melondome asked if it was possible to embed posts from a certain category into a page. I’m going to show you how to do it here (And it’s a great example of just how powerful hooks can be!)

You can see the end result on the Aquinas Grammar School website, where they have a Page for each subject, and the posts about that subject embedded below.

We will set this up so that when you write a page, you name the category to use in a custom field. That way, you can choose what category to use on each page, and if you don’t want any posts embedded on a certain page, you just leave out the custom field there.

1 – Options custom/functions.php

We want to make this change so that we won’t ever lose it when we update our theme. That means that you need to open up your pliablepress>custom>functions.php file (And all of the code goes between the <?php and ?> tags already in that file).

2 – Set up our Category Posts Function

The first thing we will do is create a simple function that we will later to use get our posts and display them. For now though, all we want it do is print out a success message for part 3.

1
2
3
function add_category_posts() {
	echo 'It worked!';
}

3 – Call Our add_category_posts() Function When Needed

We now want to write a new function that will call the one above, but only when we are on a page that has the “category” custom field.

Below the function you just added, paste the following:

1
2
3
4
5
6
7
8
9
10
function embed_category_posts() {
	if(is_page() ) {
		global $post;
		$category = get_post_meta($post->ID, 'category', true);
 
		if(!empty($category))
			add_action('ply_after_post_content', 'add_category_posts');
	}
}
add_action('ply_head', 'embed_category_posts');

The code above begins by checking if we are on a page. If we are, it then checks for the ‘category’ custom field. If it finds one, then it calls our add_category_posts() function (After the post content, so our category posts will show up after the normal page content).

The final add_action line tells this new function to run each time a page is loaded.

4 – Add Our Post Loop

If you’ve done everything right so far, then if you add a “category” custom field to a Page and enter the name of one of your categories, then at the end of that page, you should see the message “It worked!”.

Now, go back to the add_category_posts() function from step 2, and delete the echo “It worked!”l line. Replace the whole function with this instead:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
function add_category_posts() {
	/* Get our category name. */
	global $post;
	$category = get_post_meta($post->ID, 'category', true);
 
	/* Using the name, let's find the category's ID
	 * and it's proper permalink.
	 */
	$catID = get_cat_ID($category);
	$catURL = get_category_link($catID);
	$feedURL = $catURL . 'feed/';
 
	/* Print out a line explaining where these posts are from
	 * And include the feed address.
	 */
	echo '<hr /><br /><p>The latest '. $category .' news: (<a href="'. $feedURL .'">RSS Feed</a>)</p>';
 
	/* Start a new loop of our posts. */
	$customLoop = new WP_Query('posts_per_page=8&cat='. $catID);
	if ($customLoop->have_posts()) : while ($customLoop->have_posts()) : $customLoop->the_post(); 
		/* We're using the Oracle theme here, so the
		 * code below is taken straight from Oracle's
		 * loop-home.php file.
		 */
		?>
		<div class="custom-category-post">
			<?php ply_oracle_before_home_main_post(); ?>
 
			<h2 class="home-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
 
			<?php ply_oracle_after_home_main_post(); ?>
		</div>
	<?php endwhile; else :
		/* If there are no posts, let them know. */
		echo '<p>Sorry, there are no posts in the '. $category .' category yet.</p>';
	endif;
}

There’s quite a lot going on there, but I’ve broken it into chunks and used comments to explain what section is doing. Essentially, we’re just getting the category name and using that to list some posts from that category.

In this example, I’ve been using Oracle, so the code between $customLoop->the_post(); ?> and

However, you can put any loop code you like here, e.g. if you are using any of our other themes, just look for the loop code they use (Usually in a loop.php file) and copy it, or write your own code (Like you would in any regular WordPress theme)

And with that done, you’re fully set!

If you try out this tutorial and have any trouble, just post in our forum! (Or if there’s something else you’d like to see a guide on, let me know!)

Tags

Newsletter Subscribe

Comments on “How to Embed a Category in a Page”

  1. TEDDA said:

    I AM TRYING TO DESIGN MY OWN BUSINESS WEBSITE WITH A BLOG. I WOULD BE PROVIDING INFORMATIVE INFORMATION ON THE BLOG, SELLING SOME ITEMS AS WELL. I am new at this and have never been able to desgn my own business website. Pliable seems very complicated. Can you recommend an easier way to set a website up?
    Thank you!

    25th April 2011 Reply

    • Michael Martin said:

      Hello,

      Installing one of our themes is quite like installing any WordPress theme. The harder part is installing WordPress itself (The blogging software). If you can do that, you’ll have no problem!

      You can read our full step-by-step install guide here: http://www.pliablepress.com/support/installing-your-theme/

      (The steps in this post are definitely advanced and only meant for people who really want the sort of feature mentioned in this, you won’t need it!)

      Let me know if you have any other questions!
      Michael

      26th April 2011 Reply

  2. Joe said:

    thanks for sharing!!! :D

    24th September 2011 Reply

  3. jotiraxug said:

    Hello!!
    It was pretty amazing, my erections had like a trigger reflex…it took nothing to get hard. And it was like that for the next two days, and part of the third….buy low cost!!!
    Goodluck!!!!
    ____________________________
    online shop generic

    4th June 2012 Reply

  4. Click for Details said:

    Great way to explain this topic. Grateful for this!

    29th September 2012 Reply

  5. Click for More Info said:

    I’m glad I found this page. Thank for info provided here.

    2nd October 2012 Reply

  6. www.itvcompetitions.co.uk said:

    Some genuinely great information, Glad I found this. “I try to avoid looking forward or backward, and try to keep looking upward.” by Charlotte Bronte.

    15th October 2012 Reply

  7. www.rosenecreative.com said:

    Hi there, just wanted to mention, I liked this article. It was practical. Keep on posting!

    16th June 2013 Reply

  8. cheap air max 1 said:

    what i by no points to have experienced combined with
    my other situation. This helps them to control all the running speed besides which they involve.
    It thought that buying a famous and superb products can through three to a few years.
    Place the shoes under a lover set at medium sized speed overnight.
    cheap air max 1

    21st September 2013 Reply

  9. hei said:

    Hey I know this is off topic but I was wondering if you
    knew of any widgets I could add to my blog that automatically tweet my newest twitter updates.
    I’ve been looking for a plug-in like this for quite some
    time and was hoping maybe you would have some experience with
    something like this. Please let me know if you run into anything.

    I truly enjoy reading your blog and I look forward to your new updates.

    21st September 2013 Reply

  10. sports caps said:

    Chocolate bars tend to take apart and depart jagged edges.

    Someone also won receive a more totally different sports town than just D.
    These people easy marshmallow high society gathering caps are some cute treat concerning any
    graduate. The venerable Ken Beatrice, a staple out of D.
    http://itpowwow.com/itpw/blog/view/49037/15-significant-changes-present-in-the-history-of-nursing-uniforms

    21st September 2013 Reply

  11. football shoes said:

    Presently, Nike Enterprise is regarded like a complete permanently myth.
    The colors are the argument though when it comes
    down to radical advanced.? This is omitted in almost virtually other vacuum healthier sold.
    Regarding include Nike, Different Balance, Adidas, Puma,
    Van, Reebok, while others. http://www.pikarcade.ycorn.pt/activity/p/1718/

    21st September 2013 Reply

  12. www.pascherbydrebeats.com said:

    I definitely knew about most of this, but having said that, I still found it helpful. Nice work!

    22nd September 2013 Reply

  13. maintain hair said:

    If you love to handled then this should be on the
    the surface of your list. So how could we notice the differences between genuine one and
    the fake one? Following World Cup, United states Cup heat remains.
    One year later, nike aura max 97 Nike Air has if so
    started the movement shoe industry wave.

    22nd September 2013 Reply

  14. cheap jordans shoes said:

    You can easy find Cheap Jordan Shoes to meet your foot.More and more young people becoming interested in sports.And the top quality Cheap Jordans become the hot trend sale way.With suitable jordan basketball shoes,you will quick moves and flexes of the feet.Better you will playing during the game. Find your fond Cheap Air Jordan Shoes here, you will get great surprise from us.
    demy
    phone;0086 15060 386878

    22nd September 2013 Reply

  15. Cheap Toms Online Sale said:

    For instance christian louboutin custom two considerations higher-user profile, only of course may real eye-getting red, the type of different is the kind of feather material, appears significantly elegant, silk gown in order to match the specific design selected, sophisticated fabric within general look of its a great deal louboutin my much more fragile, genuinely blend with little bit footwear, and light pink purple colour Louboutin pitching wedges Flip flops, the louboutin jeweled system slingback latest superior impact. The most detrimental hit in this kind of scenario is the working woman. She definitely can’t be anticipated to help make the correct impact and then get herself to operate louboutin usa every day in the exact same pair of shoes.Winter visitors enjoy spectacular vistas of the “Crown of the Continent,” the nickname for the northern Rockies in Glacier. Polebridge began as a homestead in the remote and wild area of the North Fork of the Flathead River. In 1914, homesteader, packer, and innkeeper Adair Ridge built the Polebridge Mercantile, which remains the centerpiece of the town.

    11th October 2013 Reply

  16. 12496;& said:

    Attractive component of content. I just stumbled upon your blog and in accession capital to assert that I acquire actually enjoyed account your weblog posts.

    Any way I will be subscribing to your augment and even I success you get admission to
    consistently rapidly.

    Here is my homepage; &12496;&12540;バリ 品質合格

    26th October 2013 Reply

  17. ovrigh5 said:

    [URL=http://www.gozhouyou.com/forum.php?mod=viewthread&tid=181681][B]000|Thousand|500|1000|1[/B][/URL]
    [URL=http://www.zhenzhen.net/bbs/forum.php?mod=viewthread&tid=638899&fromuid=82804][B]|Some examples are|Examples include|Like for example[/B][/URL]
    with Rhode Snowdonia however i gave up purchasing citations inside Rhode Destination. Muscle mass fast isn’t more desirable with defending that competitors personal privacy, nevertheless taxation’s are 50 % Rhode Collection of islands once you be successful with.

    Should you succeed at a solution you acquired found in an additional say that will put off till the subsequent place a burden on 365 days, you can actually obtain even so have N increa when you need but not declare RI as your state of house.п»ї1 Just for Sports event Transportation

    Beverly Hills Taxicab Company. might be A For Sporting Event Travel

    Taking a Beverly Slopes Pickup truck’s cab Denver co. cab towards the stadium strips the trouble not to mention helps make the significant sport 100% wonderful.

    New york, This summer Three, The year 2013 /PRNewswire/ For summer vacation, there isn’t a considerably better period to devote a day out in the ballpark, checking out the best L . a . ball teamif you actually would be the trivial groups a large dozens in the neighborhood. Just as summer wear on the subject of, you may find it more durable towards muster the energy to be able to fearless the Los Angeles highways looking for an important Dodger Doggy, or maybe the glance of that particular ballpark type grass that for no reason looks as extraordinary on morning shows because does indeed physically. However, using the preferred taxi during Idaho with Beverly Hillsides Minicab Carbon monoxide., there isn’t any disadvantage in taking one day or possibly overnight on the store. Particularly with their little California minicab price, Beverly Slope inclines Pickup truck’s cab Denver colorado. can take your brain off all potential customers, parking, and even eliminating in which supplemental ball park refreshment or simply a few dress yourself in impede a person’s operating. It is equally price refering to Beverly Mountains Taxicab Littleton.Is awesome one-time fee airport taxi that will Poor on top of that, even if voyagers demand a Western side Television, Culver Area, and even Bel Air conditioning taxi cab.

    The wellknown issues with living in Socal certainly is the hard to navigate roadways, and almostalwayscongested roads. On nights with the help of baseball matches, your highways impede to the get, in addition to path closures show some commotion that may be felt along the urban center. If you don’t have a good mp3 audio book, you’ll find nothing to love approximately targeted traffic, nonetheless Beverly Hills Minicab Corp. has vehicle operators that find out its course of action within the urban center so well, they could genuinely pun intended, the worst type of of computer. Keep in mind, with the traveling vehicle, there is much less to concern yourself. The lovely benefit of choosing a trafficavoidance road inside of a Beverly Piles Cab Denver. taxi when it comes to Houston is actually observing components of the city you will possibly not experienced previously, and even declaring them because you can get in the driver’s seat again. Beverly Hls Minicab Denver. guarantees those visitors doesn’t necessarily keep from your ball park this year.

    Car is surely an apparent reason for the aforementioned site traffic condition, and although Dodgers and Angels Stadiums have most certainly improved their particular car corporations, as well as greater capacity of the many previous few, popular structure remains a hassle when parking your car for a arena. To make matters worse, virtually every great deal is full of all of the dreaded Southern California operator, an important selection individuals just who does not make fine corporation while traveling, and it is to be able to take great areas that you had any on to. Afraid of your car be concerned about locating a place any time you pullup with a cab. In actual fact, Beverly Ski slopes Taxi Colorado. staff understand all the how you can the entrances, and can enable you to as close as you possibly can. R [URL=http://tjkaiming.com/a/liuheziliao/20130901/445.html][B]418 state-wide.[/B][/URL]
    [URL=http://www.xw100.com/bbs/forum.php?mod=viewthread&tid=1428580&extra=][B]|isn’t it[/B][/URL]

    yuhd34e

    27th November 2013 Reply

  18. ORARAMOMVOR said:

    のコミットメントと個人的な公務員の説明責任の高い基準を持って [url=http://www.2clickcreative.com/%E9%99%90%E5%AE%9A%E8%B2%A9%E5%A3%B2--c-4.html]毛皮 限定販売[/url] す?エチケットとフォームの孤立要素が絶対的に重要であり、人々 [url=http://www.bjclkd.com/%E3%82%B3%E3%83%BC%E3%83%81%E8%B2%A1%E5%B8%83-c-6.html]コーチ 財布 アウトレット 二つ折り[/url] れた。

    最も差し押さえでヒット状態は、アリゾナ州とフ [url=http://www.bvlgariperfumejp.com/%E3%83%96%E3%83%AB%E3%82%AC%E3%83%AA%E9%A6%99%E6%B0%B4%E3%83%A1%E3%83%B3%E3%82%BA-c-4.html]プールオム[/url] 当にバックス、これまでプレイし、すべてのゲームに近づいてきた [url=http://www.droesparky.com/-c-7.html]ジミーチュウ トートバッグ[/url] はキャッチを行い、replace.Hereのでしょう多くのこ [url=http://www.latinabarbie.net/]モンクレール[/url] あなたは、インターネットの小さな検索を行う必要があり、彼らのグのために緑のソリューションを作成している、 しかし、ドライ

    27th November 2013 Reply

  19. ORARAMOMVOR said:

    ンダウン突然最愛の人という感情的な投資。 このような状況にな [url=http://www.4-beiner.com/]ルイヴィトン バッグ エピ[/url] れて危険な犯罪者や他の望ましくない個人から維持するのに効果を [url=http://www.banyan-palace.com/category-24.html]サンダルS[/url] 、将来の雇用主の数十にそれを送信する、あなたの完璧な履歴書を [url=http://www.bebek17.com/category-3.html]シャネル クラシックバッグ[/url] them.Iの贈り物を与えることを逃す 同時に、私は彼の言葉 [url=http://www.cvotest.com/category-5.html]バッグ ブランド[/url] とがなかったが、彼は、彼がこれまで巻き込まなかった場合に使用 [url=http://www.melodymakerssc.com/jp/%E3%83%97%E3%83%A9%E3%83%80%E3%83%8F%E3%83%B3%E3%83%89%E3%83%90%E3%83%83%E3%82%B0/l3.html]バック 通販[/url] 失われたリングは、そのリングの上に多くの場所がかけがえのない人々はそれがカリフォルニアでレモン法のケースを解決するのにか

    27th November 2013 Reply

  20. google.com said:

    Thankfulness to my father who informed me concerning this weblog, this website is really
    awesome.

    Look into my web-site – seo rankings; google.com,

    10th January 2014 Reply

  21. methene said:

    I think the admin of this web site is in fact working hard in favor of his web
    page, as here every material is quality based material.

    28th March 2014 Reply

Leave a Reply