Welcome, guest!

Feel free to read the blog, browse for themes, or join the club.

[shortcode] for query_posts

  1. I didnt find any plugin to have a shortcode usable in pages to display multiple loops as we need to show post of 2 diffrent catagorys in 2 places of a page ... i did come up with the following solution after searching around fr diffrent approches ... i think it might be usefull for ohter ppl and i'am intrested in feedback ... its geard for use in hybrid ...

    useage:
    [loop query="cat=1&showposts=2"]

    Code:

    function myLoop($atts, $content = null) {
    	extract(shortcode_atts(array(
    			"query" => '',
    		), $atts));
    
    	// de-funkify $query - taken from http://digwp.com/2010/01/custom-query-shortcode/ needed to get it working better ideas ?
    	$query = preg_replace('~&#x0*([0-9a-f]+);~ei', 'chr(hexdec("\\1"))', $query);
    	$query = preg_replace('~&#0*([0-9]+);~e', 'chr(\\1)', $query);
    
    	global $wp_query;
    
    	query_posts( $query );
    	$more = 0;
    	ob_start();
    	?>
    
    	<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    
    				<div id="post-<?php the_ID(); ?>" class="<?php hybrid_entry_class(); ?>">
    
    				<?php hybrid_before_entry(); // Before entry hook ?>
    
    				<div class="entry-content">
    					<?php the_content( sprintf( __('Continue reading %1$s', 'hybrid'), the_title( ' "', '"', false ) ) ); ?>
    					<?php wp_link_pages( array( 'before' => '<p class="pages">' . __('Pages:', 'hybrid'), 'after' => '</p>' ) ); ?>
    				</div><!-- .entry-content -->
    
    				<?php hybrid_after_entry(); // After entry hook ?>
    
    			</div><!-- .hentry -->
    
    		<?php endwhile; ?>
    
    	<?php else: ?>
    
    		<p class="no-data">
    		<?php _e('Sorry, no posts matched your criteria.', 'hybrid'); ?>
    		</p><!-- .no-data -->
    
    	<?php endif; ?>
    
    	<?php wp_reset_query();
    	$content = ob_get_contents();
    	ob_end_clean();
    	return $content;
    }
    add_shortcode("loop", "myLoop");
    
    ?>
  2. You must be a logged-in exclusive member to view this reply.

  3. I ended up using amr_shortcode_any_widget to shortcode the Query Posts widget and do essentially the same thing. Also uses Widget Logic to only appear on a specific page.

    I'm not sure three plugins is better than your method, though - but I do use Query Posts and Widget Logic elsewhere.

  4. @Justin -
    at the moment its just for my personal function.php - i think my PHP-Knowledge is somewhere between starter and intermediate :P - could it be done without ob_start() i could concanate the string myself but this way the code ist much easier ^^

    Would you prefer to write a custom page template based on your page-custom-query-list ? an to simple include more than one Query ?

    @Kyle
    I was thinking of the same thing but it would complicate the logic much more because we have around 6 pages each with 2 categorys to show the last 2 posts each ... and with this aproach we could keep the logic wher it belongs at the page ...

    @all
    Beside the mentioned ideas any thought or problems that may arise ?

  5. You must be a logged-in exclusive member to view this reply.

  6. You can see it here:
    http://www.gronau.at/projekt/oeskb/eine-seite/stm-team/

    the tabs are done via a plugin too - i am not fully satiesfied but its easy to change or to add one more Tab and you have everything in one place ...

    thank you for your help and this great framework - its my second page i design with hybrid and this time its much eaisier as it adopt the way your work its just beautifull - remove a action here add it at another place ... only to find out what is hooked where or filter is sometimes hard :D

Reply

You must log in to post.

Limited Access

If you have an account, please take a moment to log in.

Non-exclusive members have limited access to the support forums.

To enjoy the full range of support, sign up for an exclusive membership in the theme club.

Support Forums

  • Bliss Theme (188 posts)
  • Hybrid Theme (16,976 posts)
  • Options Theme (10,322 posts)
  • Structure Theme (2,624 posts)
  • Visionary Theme (767 posts)
  • bbPress Themes (293 posts)
  • WordPress Plugins (1,614 posts)
  • General Discussion (3,955 posts)