0

I am beginning to make a site in wordpress (so no php or html experience). I want to generate textlist from a mysql column into a text block.

I have made the php code for the shortcode below which returns an array which now displays in the textblock as "Array" instead of a list of strings.

If i just print the values they appear on the head of the page.

What are the next steps I need to do/look for. I can't find it because I probably do not know the correct search terms. My guess is something with HTML.

<?php   
function location_marker_shortcode( $atts ) {
   $a = shortcode_atts( array(
      'mapnumber' => 'world'
   ), $atts );

global $wpdb;

//select databases (the 84 part should be the input of the shortcode)
$marker_labels = $wpdb->get_col('SELECT label FROM wp_mapsvg_database_84');

foreach ( $marker_labels as $marker_label ) 
{
    //print labels 
    echo $marker_label;
}

return $marker_labels;

}  
//add shortcode to wordpress
add_shortcode( 'matthijs', 'location_marker_shortcode' );
?>

I have now this code which gives me a list exactly what i want but not in the "paragraph block" in wordpress where my shortcode is situated.

<?php   
function location_marker_shortcode( $atts ) {
   $a = shortcode_atts( array(
      'mapnumber' => 'world'
   ), $atts );

global $wpdb;

//select databases (the 84 part should be the input of the shortcode)
$marker_labels = $wpdb->get_col('SELECT label FROM wp_mapsvg_database_84');

foreach ( $marker_labels as $marker_label ) 
{
     echo  '<li>'. $marker_label.'</li>';
}

}  

//add shortcode to wordpress
add_shortcode( 'matthijs', 'location_marker_shortcode' );
?>
3
  • 1
    Rather than manually connecting to the database look at using the wpdb class (codex.wordpress.org/Class_Reference/wpdb) Commented Jan 30, 2019 at 17:19
  • @CharlieStanard I will look into it Commented Jan 30, 2019 at 18:20
  • @CharlieStanard now I have updated the code to work with the wpdb class still the same question though Commented Jan 30, 2019 at 19:32

1 Answer 1

2

I'm not 100% sure what you're trying to accomplish, but try this. You don't want to echo out all the values as you're looping through them. Concatenate everything in a variable and then return the entire string at the end of your shortcode. This should generate an unordered list.

<?php   
function location_marker_shortcode( $atts ) {
   $a = shortcode_atts( array(
      'mapnumber' => 'world'
   ), $atts );

global $wpdb;

//select databases (the 84 part should be the input of the shortcode)
$marker_labels = $wpdb->get_col('SELECT label FROM wp_mapsvg_database_84');

$output = '<ul>';

foreach ( $marker_labels as $marker_label ) 
{
    $output .= '<li>' . $marker_label . '</li>';
}

$output .= '</ul>';

return $ouput;

}  
//add shortcode to wordpress
add_shortcode( 'matthijs', 'location_marker_shortcode' );
?>
Sign up to request clarification or add additional context in comments.

3 Comments

$output did not work for me but I changed it to echo see my edited question. Can you see why? This does give me the thing I want but not in the correct block
@matthijsW WordPress executes this code when bootstrapping things, so it will actually run the code and echo those values at the top of the page before the shortcode is even used. You need to make sure you're not echo'ing anything - you have to store the string as a variable and then return at the end. Also it looks like you have an extra closing curly brace } in your sample code.
i understand that echo is on the top of the page it was just to show that it gave the right list but still wrong place. i tried your code but still gave me the text array I now see why a small error. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.