Jump to content
Sign in to follow this  
shaun

Any way to pull blog posts dynamically into 'boxes'?

Recommended Posts

shaun

Hello, I'm currently setting up a homepage for a site with the PlatformPro theme. I'm using the featured page template, with 3 boxes underneath of the featured slider. I was wondering if there was any way to fill one of those boxes with 5 or so recent blog posts. Typically this line of code works: <?php $featuredPosts = new WP_Query(); $featuredPosts->query('showposts=5'); while ($featuredPosts->have_posts()) : $featuredPosts->the_post(); ?><p>"><?php the_title(); ?></p> <?php endwhile; ?> However, I tried pasting this into the HTML editor in WordPress, and had no luck (I'm assuming that's because the 'boxes' are posts themselves). Is there another way to pull this off?

Share this post


Link to post
Share on other sites
cmunns

No it's possible but you need to get a plugin taht will allow you to execute PHP in the post editor panels. PHP-exec should work.

Share this post


Link to post
Share on other sites
dgumpper

Shaun... I implemented Shortcode Exec PHP plugin with great results. The plugin allows you to easily create shortcodes and keep the PHP out of the post/pages. The following code is an example of how to pull the title of the last 5 post from a category.

// Prevent recursion
	global $outerPost;
	if ($outerPost)
		return '';
	
	// Parse parameters
	extract(shortcode_atts(array('query' => 'post_type=post'), $atts));
	$query = html_entity_decode($query);
	
	// Create new post loop
	global $post;
	$outerPost = $post;
	$my_query = new WP_Query($query);
	while ($my_query->have_posts()) {
		$my_query->the_post();
		setup_postdata($my_query->post);
	
		// Process post
		echo '<div>'; the_title(); echo '</div>';
	}
	$post = $outerPost;
	unset($outerPost);
	$id = $post->ID;[/code]

In Boxes or Banners, you just add the shortcode. [i]shortcode_name[/i] query="cat=3&showposts=5" Make sure you use square brackets around the shortcode in your boxes. Just another option.

Share this post


Link to post
Share on other sites
Rob

David, could you elaborate a bit more on how to actually execute that process? I have installed Shortcode Exec PHP, and entered every line of your posting above, putting the shortcode specified in square brackets as you said. All that shows up is the code in brackets. Some of us are not as experienced with actual PHP code.


Former PageLines Moderator, Food Expert and Raconteur

Share this post


Link to post
Share on other sites
dgumpper

Robert, While in Shortcode Exec PHP, did you "Test" your copy of the code by pressing the test button? If you have a blog post that matches the criteria of the category, it will display the content. If not, then there might be a typo in the code. If that is okay, make sure the "Enable" and "Output Echo" next to the shortcode name is selected. Let me know how you make out.

Share this post


Link to post
Share on other sites
dgumpper

Robert, What is your shortcode in the [] brackets?

Share this post


Link to post
Share on other sites
Rob
[shortcode_name query="cat=5&showposts=5"]

And yes, I did test it. It tested fine. However, the result initially was just the shortcode. That may have been a cache problem. I did something (Heaven's knows what) to yield a list of the last 5 posts, but it worked only in one box. None of the list items were actually linked to the articles they represent. The other two boxes had the heading, no shortcode, but no content either. Without each list item linked, this may simply not be the solution I was hoping for. You can see the problem at epicurusinstitute.org


Former PageLines Moderator, Food Expert and Raconteur

Share this post


Link to post
Share on other sites
cmunns

Yes of course...because your custom loop is not linking it... see this line: `echo '

'; the_title(); echo '
';` change it to `?><?php the_title();?><?php`

Share this post


Link to post
Share on other sites
Rob

Adam, thank you. That worked, but I must be missing some other element because now each link daisy-chains... not individual lines, but one link immediately following the next with no spaces or line break. I'm still only able to make it work in one box though. Here's the code I'm using, exactly:

// Prevent recursion
	global $outerPost;
	if ($outerPost)
	return '';
	// Parse parameters
	extract(shortcode_atts(array('query' => 'post_type=post'), $atts));
	$query = html_entity_decode($query);
	
	// Create new post loop
	global $post;
	$outerPost = $post;
	$my_query = new WP_Query($query);
	while ($my_query->have_posts()) {
	$my_query->the_post();
	setup_postdata($my_query->post);
	
	// Process post
	?>"><?php the_title();?><?php
	}
	$post = $outerPost;
	unset($outerPost);
	$id = $post->ID;[/code]


Former PageLines Moderator, Food Expert and Raconteur

Share this post


Link to post
Share on other sites
Rob

Adam, thanks again. Ah... I fixed the breaks by adding immediately after the in your snippet. But how to make this work in all three boxes rather than just the one? That's got me....


Former PageLines Moderator, Food Expert and Raconteur

Share this post


Link to post
Share on other sites
dgumpper

I am not sure, but it could be that the shortcode exe php might not be able to run the same shortcode multiple times during a single page load. Try creating unique shortcode for each box and add your code to each one. I know it is not the best and is a workaround, but thats what I would do for now. Maybe Adam has suggestions or can validate this as the cause for the shortcode not displaying in the other boxes.

Share this post


Link to post
Share on other sites
Rob

David, On your suggestion, I set up

[shortcode_econ query="cat=3&showposts=5"]

. Unfortunately, it too did not work. I guess the boxes with categories is not really workable in all cases.


Former PageLines Moderator, Food Expert and Raconteur

Share this post


Link to post
Share on other sites
Kate

Hey Robert, At the end of the PHP code you currently have, please try adding wp_reset_query();. That may help.

Share this post


Link to post
Share on other sites
Rob

Thanks Kate. I tried it without luck. Nothing seems to work. In theory, this shouldn't be so difficult.


Former PageLines Moderator, Food Expert and Raconteur

Share this post


Link to post
Share on other sites
dgumpper

I think I figured this out. You cannot run the same variables of two different shortcodes on the same page. I did get the following to work. On the second shortcode you created, change your code to the following: 1. change the $outerPost to $outerPost1 any place within the code. 2. change $my_query to $my_query1 any place within the code. Test the shortcode in Shortcode EXEC PHP, then run your page and see if the second box with the second shortcode displays. I did get this to work on a test page on my site.

Share this post


Link to post
Share on other sites
Rob

Bingo! David, that plus Kate's snippet did the trick. In the 3rd box, I changed 1 to 2 for each instance. Each box has a different shortcode, applicable to the one added in Shortcode Exec PHP. So for the Business Category, I use

[shortcode_busi query="cat=6&showposts=5"]
The code to add to Shortcode Exec PHP is:
// Prevent recursion
	global $outerPost2;
	if ($outerPost2)
	return '';
	// Parse parameters
	extract(shortcode_atts(array('query' => 'post_type=post'), $atts));
	$query = html_entity_decode($query);
	// Create new post loop
	global $post;
	$outerPost2 = $post;
	$my_query2 = new WP_Query($query);
	while ($my_query2->have_posts()) {
	$my_query2->the_post();
	setup_postdata($my_query2->post);
	// Process post
	?>"><?php the_title();?><?php
	}
	$post = $outerPost2;
	unset($outerPost2);
	$id = $post->ID;
	wp_reset_query();[/code]

	
	And for Economics it's 
[code][shortcode_econ query="cat=3&showposts=5"]
For this shortcode, I used:
// Prevent recursion
	global $outerPost1;
	if ($outerPost1)
	return '';
	// Parse parameters
	extract(shortcode_atts(array('query' => 'post_type=post'), $atts));
	$query = html_entity_decode($query);
	// Create new post loop
	global $post;
	$outerPost1 = $post;
	$my_query1 = new WP_Query($query);
	while ($my_query1->have_posts()) {
	$my_query1->the_post();
	setup_postdata($my_query1->post);
	// Process post
	?>"><?php the_title();?><?php
	}
	$post = $outerPost1;
	unset($outerPost1);
	$id = $post->ID;
	wp_reset_query();[/code]

This individual approach worked brilliantly. Thank you all!


Former PageLines Moderator, Food Expert and Raconteur

Share this post


Link to post
Share on other sites
bwoolley

This is great advice, and I'm overjoyed that it's working! I'm playing with the formatting, and I'd love to know how to modify the code so that the "title" is followed by "read", linking to the post itself. Can anyone help me out? I'm not any good at actually authoring php... Many thanks in advance!

Share this post


Link to post
Share on other sites
bwoolley

OK, I figured out the link problem (re-reading this page made me realize that the answer was in Adam's post above.) And have also solved the list problem - it's a combination of the two: (sorry, code truncated... see next post) Now for more formatting games!

Share this post


Link to post
Share on other sites
bwoolley

I think I've got it. It might not be the most elegant solution, but it looks the way I wanted it to: bullet points before each entry, and a "read ->" link after it. ` // Process post ?>

??? <?php the_title();?> Read ->
<?php; echo ' `

Share this post


Link to post
Share on other sites
keishanatalee
Hi, I am new to page lines and I am trying to use the short code to add a most recent post to my Latest News Box. I tried the process that you are describing but it is not working for me. I got it to work once, not sure what I did, but now it's back to just showing the code. Can someone help me get through this please? My site is kareemhertzog360.com and as you can see in the Latest News box it just displays the shortcode I am using in the box content area. Also in the shortcode - what does name represent, what does cat represent and what does amp represent? I am thinking that cat represents the category. I also only want one post to show up not five. Thanks.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

×