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

Leave a Reply