Hallo Andreas,
vielleicht hilft dir dies weiter. Zuerstmal eine Tabelle, die ich zusätzlich zu WordPress Tabellen in meiner Datenbank habe:
CREATE TABLE IF NOT EXISTS `cs_xt_counter_userinfostat` (
`id` int(14) NOT NULL AUTO_INCREMENT,
`lang` varchar(255) COLLATE latin1_german2_ci NOT NULL DEFAULT '',
`os` varchar(255) COLLATE latin1_german2_ci NOT NULL DEFAULT '',
`browser` varchar(255) COLLATE latin1_german2_ci NOT NULL DEFAULT '',
`robot` varchar(255) COLLATE latin1_german2_ci NOT NULL DEFAULT '',
`hits` int(14) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_german2_ci AUTO_INCREMENT=184 ;
Bei mir hat dieses Tabelle im Test über 180 Einträge, die Betriebssystem, Browser etc. als Statistiken enthält. Diese möchte ich nun mit 10 Zeilen pro blätterbarer Seite auf einer WP static Page anzeigen, ohne mir jemals wieder Gedanken darüber zu machen und vollautomatisch je nach Zustand der "externen Tabelle".
Zuerst habe ich einen neue Testseite angelegt. Diese hat bei mir die ID=43. Kannst deine existierende Seite auch benutzen.
Danach habe ich in die functions.php meines Themes folgendes eingebaut. Man kann es auch als Plugin verpacken, dann geht es mit jedem Theme:
//function modifies a specific post with aditional content from foreign table
//pupose: content from foreign table should force pagination of WordPress
// generated page automatically
function modify_page_with_foreign_paged_content() {
//make some globals accessible
global $post, $id, $page, $pages, $multipage, $numpages, $wpdb;
//additional entries per paginated page from foreign table
$per_page = 10;
//define the static WordPress Page ID should contains the additional content
$affected_page_id = 43;
//if its the page with ID = 43 (my test page) and not in admin area (meens frontend only)
if (is_page($affected_page_id) && !is_admin()) {
//how much entries are in the foreign table
$num_entries = (int)$wpdb->get_var("SELECT COUNT( * ) FROM cs_xt_counter_userinfostat");
//pad the paginated array to correct value (event if 0 entries in foreign db table)
$pages = array_pad($pages, max($num_entries / $per_page, 1), '');
//evaluate the current displayed paginated page and thereof the start record in foreign table
$start = $page * $per_page;
//fetch only the required records to be displayed at the selected paginated page
$results = $wpdb->get_results("SELECT * FROM cs_xt_counter_userinfostat LIMIT $start, $per_page");
//iterate the results and add the content from foreign table
//first paginated page contains at front the text entered at the page editor
//markup can be done as usual, i have only wrapped a column to a new line as example
foreach($results as $result) {
//as example: if actual $page == 3 the additional content has to be added
// to $page - 1 because array counts from 0 upwards
//the var $result is a stdClass object, where column names are member of (see browser)
$pages[$page-1] = $pages[$page-1] .'<br/>' . $result->browser;
}
//configure the appearance for WordPress loop if we have more than one page
//or only one page (cause 0 foreign entries or not enough to show a 2nd page)
if (count($pages) > 1) {
//we have multiple paginated pages
$multipage = 1;
$numpages = count($pages);
}
else{
//sorry, not enough content, so we have a solid page only
$multipage = 0;
$numpages = 1;
}
//thats it, now it paginates automatically during ordinaty page template
//nothing more special have to be done now
}
}
add_action('the_post', 'modify_page_with_foreign_paged_content', 0 );
Alles anzeigen
Die einzigen Änderungen, die du machen müsstest, wären $per_page (Anzahl Zeilen pro Page), $affected_page_id (die betroffend statische WordPress Seite) , natürlich die entspechende Datenbanktabelle in beiden SQL Abfragen und ggf. das Markup, in das du die Daten verpacken willst.
Dann läuft das ganz ohne weiteres Zutun mit einem stinknormalen Page Template, ich hatte Twenty Twelfe im Einsatz. Hier ein Screenshot: