Shortcode do not return the right data in post
Question
Trying to make a CD plug-in. But cant seem to get the data rows in the shortcode.. i know it cant echo in a shortcode, but how Can i get, lets say a top 10 CDs in a post with a shortcode?
hope someone Can help with this..
<?php
/*
Plugin Name: cd by dk
Plugin URI: http://wordpress.com
description: cd
Version: 1.0.0
Author: Rene
Author URI: http://wordpress.com
*/
// Create a new table
function cd_dk_table(){
global $wpdb;
$charset_collate = $wpdb->get_charset_collate();
$tablename = $wpdb->prefix."cd_dk";
$sql = "CREATE TABLE $tablename (
id mediumint(11) NOT NULL AUTO_INCREMENT,
artist varchar(80) NOT NULL,
album varchar(80) NOT NULL,
year mediumint(11) NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
}
register_activation_hook( __FILE__, 'cd_dk_table' );
// Add menu
function cd_dk_menu() {
add_menu_page("cd", "cd","manage_options", "cd_dk_myplugin", "cd_dk_displayList");
add_submenu_page("cd_dk_myplugin","All cd", "All cd","manage_options", "cd_dk_allentries", "cd_dk_displayList");
add_submenu_page("cd_dk_myplugin","Tilføj ny cd", "Add new cd","manage_options", "cd_dk_addnewentry", "cd_dk_addEntry");
}
add_action("admin_menu", "cd_dk_menu");
function cd_dk_displayList(){
include "list.php";
}
function cd_dk_addEntry(){
include "add.php";
}
function cd_dk2($atts) {
global $wpdb;
$tablename = $wpdb->prefix."cd_dk";
$cdcontent1 = "<table><tr>
<td>nr.</td>
<td>Artist</td>
<td>Album</td>
<td>year</td>
</tr>
";
// Select records
$cd_dk_entriesList = $wpdb->get_results("SELECT * FROM ".$tablename." order by year desc");
if(count($cd_dk_entriesList) > 0){
$count = 1;
foreach($cd_dk_entriesList as $entry){
$id = $entry->id;
$artist = $entry->navn;
$album = $entry->station;
$year = $entry->kontotal;
$cdcontent2 = "<tr>
<td>".$count."</td>
<td>".$artist."</td>
<td>".$album."</td>
<td>".$year."</td>
</tr>
";
$count++;
}
}
$cdcontent3 = "</table>";
return "$cdcontent1 $cdcontent2 $cdcontent3";
}
add_shortcode('cd_dk2', 'cd_dk2');
0
2 years
2020-12-16T03:10:19-05:00
2020-12-16T03:10:19-05:00 0 Answers
5 views
0
Leave an answer