Support » Fixing WordPress » Creating shortcode of WP while loop to place shortcode in Text widget

  • Resolved jparnell8839


    Ok, so I have a custom post type I have created with the Custom Post Type UI plugin called How To’s. Making a page template, assigning it to a page (and having that page in the Menu bar) and new loop for that post type works successfully. Now, I want to display “Recent How To’s” in my sidebar. I figured “Oh, well I’ll just create a new function for the while loop and put it in a shortcode”.


    Here’s the code I have for the function in my theme’s functions.php:

    function recent_posts_function($atts, $content = null) {
          'how_to_post' => 1,
       ), $atts));
       $return_string = '<h3>'.$content.'</h3>';
       $return_string .= '<ul>';
       query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));
       if (have_posts()) :
          while (have_posts()) : the_post();
             $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
       $return_string .= '</ul>';
       return $return_string;
    function register_shortcodes(){
       add_shortcode('recent-how-to', 'recent_posts_function');
    add_filter('widget_text', 'do_shortcode');

    Now, one would assume that if I put a new Text Widget and paste [recent-how-to posts="5"]Recent How To's[/recent-posts] in there, it would perform as needed. Correct?


    All I get on my widget is the literal print of [recent-how-to posts=”5″]Recent How To’s[/recent-posts], as if the add_filer('widget_text', 'do_shortcode'); isnt working.

    Any suggestions?

Viewing 1 replies (of 1 total)
  • Resolved.

    Downloaded Ultimate TinyMCE, which came with the UTMCE PHP Widget. Pasted my modified loop in there

    //WordPress loop for custom post type
     $my_query = new WP_Query('post_type=how_to_post&posts_per_page=-5');
          while ($my_query->have_posts()) : $my_query->the_post(); ?>
    	<?php echo '
    		<?php the_permalink(); ?>
    	<?php echo '">' ?>
    	<?php the_title(); ?>
    	<?php echo '</a></li>
    </br>' ?>                
    <?php endwhile;  wp_reset_query(); ?>
Viewing 1 replies (of 1 total)
  • The topic ‘Creating shortcode of WP while loop to place shortcode in Text widget’ is closed to new replies.