WordPress.org

Support

Support » How-To and Troubleshooting » shortcode implementation issue

shortcode implementation issue

  • When I use “return” it only returns one row from database but when I use echo it returns everything. The only problem is that echo places the shortcode at the top of page not where it should be. Can someone help me out. My code is below. Thanks.

    add_shortcode('martinPubs', 'martin_publications'); 
    
    function martin_publications() {  
    
    include "includes/publications_conn.php";
    
    $sql = "SELECT * FROM pubs
            WHERE pi = 'martin' AND (label = 's' || label = 'S')
    	ORDER BY year DESC";
    
    $result = mysql_query($sql);
    
    //echo "<div style='height: 200px; width: 730px; overflow-y: auto;'>";
    
    while ($row = mysql_fetch_assoc($result)) {
    
    	$id = $row['id'];
            $author_1 = $row['author_1'];
    	$author_2 = $row['author_2'];
    	$pages = $row['pages'];
    	$title = $row['title'];
    	$title_secondary = $row['title_secondary'];
    	$volume = $row['volume'];
    	$year = $row['year'];
    	$url = $row['url'];
    
    //if(!(null == $url || "" === $url)){
    
    	return "$author_1, $author_2. $year. <a href='$url' target='blank'>$title</a>. $title_secondary $volume: $pages<br / ><br / >"; 
    
    //}else {
    
    //return "$author_1, $author_2. $year. $title. $title_secondary $volume: $pages<br / ><br / >"; 
    
    //}
    }
    
    }
Viewing 1 replies (of 1 total)
  • You must accumulate all the output before returning it.

    This code is untested, but should be close:

    $out = '';
    while ($row = mysql_fetch_assoc($result)) {
    
    	$id = $row['id'];
            $author_1 = $row['author_1'];
    	$author_2 = $row['author_2'];
    	$pages = $row['pages'];
    	$title = $row['title'];
    	$title_secondary = $row['title_secondary'];
    	$volume = $row['volume'];
    	$year = $row['year'];
    	$url = $row['url'];
    
    	$out .= "$author_1, $author_2. $year. <a href='$url' target='blank'>$title</a>. $title_secondary $volume: $pages<br / ><br / >";
    }
    
    return $out;
Viewing 1 replies (of 1 total)
  • The topic ‘shortcode implementation issue’ is closed to new replies.
Skip to toolbar