WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Custom function works in a page template, but not as a shortcode (2 posts)

  1. Matthew Gerring
    Member
    Posted 4 years ago #

    So I have this function to take an unordered list and turn it into a nice, pretty list of prices. It's in my functions.php file and it looks like this:

    function rc_pricelist($page_name) {
    	global $wpdb;
    	$page_ID = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name = '".$page_name."'");
    	$list = get_page($page_ID);
    	$content = $list->post_content;
    	$productrows = explode('</li>',$content);
    	$list1 = '<div class="product-list"><div class="product-list-title">'.$list->post_title.'</div>';
    		foreach ( $productrows as $productrow ) {
    			if ( $productrow != end($productrows) ) {
    			$strip = strip_tags($productrow);
    			$contentsep = explode(':',$strip);
    			$list1 .= '<div class="product-row"><div class="product">'.$contentsep[0].'</div><div class="price">'.$contentsep[1].'</div></div>';
    			}
    		}
    	$list1 .= '</div>';
    	echo $list1;
    
    }

    It accepts a page slug as input, pulls the page content, and re-formats the content the way I want. It works just fine inside of a page template, like so-

    <?php rc_pricelist('forks'); ?>

    The need arose to call the same function from inside of a static page, so that price lists can be added to other places in the site without having to hard-code anything. So I read up on the shortcode API and came up with this:

    function rc_pricelist_shortcode($content = null) {
    	global $wpdb;
    	$page_ID = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name = '".$content."'");
    	$list = get_page($page_ID);
    	$content = $list->post_content;
    	$productrows = explode('</li>',$content);
    	$list1 = '<div class="product-list"><div class="product-list-title">'.$list->post_title.'</div>';
    	foreach ( $productrows as $productrow ) {
    			if ( $productrow != end($productrows) ) {
    			$strip = strip_tags($productrow);
    			$contentsep = explode(':',$strip);
    			$list1 .= '<div class="product-row"><div class="product">'.$contentsep[0].'</div><div class="price">'.$contentsep[1].'</div></div>';
    			}
    		}
    	$list1 .= '</div>';
    	return $list1;
    
    }
    add_shortcode('pricelist','rc_pricelist_shortcode');

    For some reason, using the shortcode like this:

    [pricelist]forks[/pricelist]

    returns this:

    <div class="product-list"><div class="product-list-title"></div></div>

    I tried appending $content to the end of the returned string, and it still didn't show up.

    What gives?

  2. Matthew Gerring
    Member
    Posted 4 years ago #

    Got it to work. All I had to do was add

    `function rc_pricelist_shortcode($atts, $content = null) {
    //do stuff
    }
    add_shortcode('pricelist','rc_pricelist_shortcode');

Topic Closed

This topic has been closed to new replies.

About this Topic