WordPress.org

Forums

[resolved] Creating shortcode of WP while loop to place shortcode in Text widget (2 posts)

  1. jparnell8839
    Member
    Posted 1 year ago #

    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".

    WRONG.

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

    function recent_posts_function($atts, $content = null) {
       extract(shortcode_atts(array(
          '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>';
          endwhile;
       endif;
       $return_string .= '</ul>';
    
       wp_reset_query();
       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?

    WRONG AGAIN

    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?

  2. jparnell8839
    Member
    Posted 1 year ago #

    Resolved.

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

    <?php
    //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 '
    <li><a>
    		<?php the_permalink(); ?>
    	<?php echo '">' ?>
    	<?php the_title(); ?>
    	<?php echo '</a></li>
    </br>' ?>                
    
    <?php endwhile;  wp_reset_query(); ?>

Topic Closed

This topic has been closed to new replies.

About this Topic