Jump to content
Sign in to follow this  
inov8ed

Shortcode function to list posts from specific category?

Recommended Posts

inov8ed

Does anyone have a function already to lists posts from a category inside a post or page that they have used with PlatformPro? There seem to be some plugins out there that do this, but I want to keep it simple and some of the plugins don't seem all that stable. Any thoughts would be appreciated or suggestions of plugins that you have already used. Thanks. -Adam

Share this post


Link to post
Share on other sites
inov8ed

Looks great. I really should install this plugin. The only problem is the fine print on Exec-PHP that you have to completely turn off TinyMCE to use it. Hmmm... This means that I can't use the editor for any part of the post if that is in there. Is there an easy way to take the function from the codex and make a function in the functions.php file of PlatformPro Base so I can just use my own shortcode? Thanks! -Adam

Share this post


Link to post
Share on other sites
bryan-hadaway

That might be old info or refer to older versions? I personally just use it so I can place PHP inline, I've never really noticed any conflicts. I would just try it out and if any problems occur we'll deal with it when we come to that bridge. Thanks, Bryan

Share this post


Link to post
Share on other sites
inov8ed

Hi Bryan, The newest version still gives me the warning about using the visual editor, but I will work around it and see what happens. -Adam

Share this post


Link to post
Share on other sites
cfisher

Check out a plugin called Global Content Blocks. It allows you to create and enter shortcodes that can call virtually anything via the editor. I only recently started to use it, but it has run flawlessly so far.

Share this post


Link to post
Share on other sites
inov8ed

Ok. I have been trying this with the Global Content Blocks plugin by creating a php block with: query_posts( 'cat=74' ); And then putting it on a page. The problem is this doesn't quite seem to have the effect I am looking for. Basically, I am looking to put a list of all posts from a specific category in a bullet list that can be put on a page so that I don't have to go back to the page to update the links with new posts in that category. It seems the above php query brings the entire page into the loop (creating a very strange looking page within a page). I guess a short code could be done to do this instead of trying the reusable plugin block? Any ideas? Thanks! -Adam

Share this post


Link to post
Share on other sites
inov8ed

I found this that looked interesting. Would this work better for what I am looking for?

function sc_liste($atts, $content = null) {
	        extract(shortcode_atts(array(
	                "num" => '5',
	                "cat" => ''
	        ), $atts));
	        global $post;
	        $myposts = get_posts('numberposts='.$num.'&order=DESC&orderby=post_date&category='.$cat);
	        $retour='
	
'; return $retour; } add_shortcode("list", "sc_liste");[/code]

-Adam

Share this post


Link to post
Share on other sites
inov8ed

Hi Bryan, That one looked interesting, but reading up on the forums on it, it looked like it was still heavily in dev and not ready for production (although there were lots of positive comments on it). I could give it a try though. -Adam

Share this post


Link to post
Share on other sites
inov8ed

The shortcode above seems to work pretty well. Is there any downside to using this instead of a plugin? Does the code look good? The forum seems to have blown the display to pieces. Here it is in full. ` function sc_liste($atts, $content = null) { extract(shortcode_atts(array( "num" => '5', "cat" => '' ), $atts)); global $post; $myposts = get_posts('numberposts='.$num.' &order=DESC&orderby=post_date&category='.$cat); $retour='

  • '; foreach($myposts as $post) : setup_postdata($post); $retour.='
  • '.the_title("","",false).' '; endforeach; $retour.='
'; return $retour; } add_shortcode("list", "sc_liste"); ` -Adam

Share this post


Link to post
Share on other sites
Kate

@Adam If both work fine, it's probably just a matter of preference ;)

Share this post


Link to post
Share on other sites
inov8ed

Hi Kate, I guess I just like having the experts here taking a look at the code. Makes me sleep at night if you think no problems will arise from it (or if you have suggestions on how to improve the efficiency of the code). Thanks. -Adam

Share this post


Link to post
Share on other sites
cmunns

the shortcode looks suitable. I would go with that as it's much lighter than a plugin.

Share this post


Link to post
Share on other sites
inov8ed

Thanks for the feedback on it. If I wanted to style just this shortcode, is it better to put the class inside the ul html (or put a div) in the PHP function or wrap it in a div in the post itself? Thanks. -Adam

Share this post


Link to post
Share on other sites
Andrew

Looks good, throw it in your functions.php and you should be good to go.

Share this post


Link to post
Share on other sites
inov8ed

How would I go about adding "options" to this shortcode? For example, if I wanted to sometimes include the featured image or the excerpt? Thanks. -Adam

Share this post


Link to post
Share on other sites
catrina

For displaying the featured image, you can use this PHP:

<?php the_post_thumbnail(); ?>
(as pointed out here: http://codex.wordpress.org/Post_Thumbnails) If you'd like to display the excerpt, use this:
<?php get_the_excerpt( $deprecated ) ?>

(http://codex.wordpress.org/Function_Reference/get_the_excerpt)


Please read the docs before posting. Please do not private message me unless I ask you to.

Designer | Catrina Dulay

Founder | Catrina and Mouse

Share this post


Link to post
Share on other sites
inov8ed

Hi Catrina, Ok. Great. However, how do I make them options in the shortcode so I can sometimes have the thumbnail, sometimes not? Do I just put it in the array? Thanks. -Adam

Share this post


Link to post
Share on other sites
inov8ed

Yes, I got it there. Adding the potential to include the thumbnail and excerpt would be great if you have any ideas. Thanks. -Adam

Share this post


Link to post
Share on other sites
bryan-hadaway

I'm going to throw this back in because it really is a good solution and super easy: http://wordpress.org/extend/plugins/list-category-posts/ Simply add [catlist] and there are a ton of ways to break it down: http://foro.picandocodigo.net/discussion/251/list-category-posts-documentation/ including author, comments, excerpts etc. etc. Thanks, Bryan

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  

×