WordPress.org

Ready to get started?Download WordPress

Forums

shortcode implementation issue (2 posts)

  1. nepzap2
    Member
    Posted 1 year ago #

    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 / >"; 
    
    //}
    }
    
    }
  2. vtxyzzy
    Member
    Posted 1 year ago #

    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;

Topic Closed

This topic has been closed to new replies.

About this Topic