und Teil 2 …
PHP
//navigation ...
$current_page = $pg;
$num_posts = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts p WHERE p.post_status='publish' $additional_search_query");
$last_page = ceil($num_posts / $paging);
$search_qs = $search_query!=""?"&s=".urlencode($search_query):"";
$str_prev = $pg > 1?"<a href=\"$_SERVER[PHP_SELF]?is_popup=yes¤t_post_id=".$_GET['current_post_id']."&pg=".($current_page-1)."$search_qs\" onclick=\"return checkInput(forms['select_id'])\">« vorherige Seite</a>":"";
$str_next = $pg < $last_page?"<a href=\"$_SERVER[PHP_SELF]?is_popup=yes¤t_post_id=".$_GET['current_post_id']."&pg=".($current_page+1)."$search_qs\" onclick=\"return checkInput(forms['select_id'])\">naechste Seite »</a>":"";
$str .= "<div class=\"navigation\">";
$str .= "<div class=\"alignleft\">$str_prev</div>";
$str .= "<div class=\"alignright\">$str_next</div>";
$str .= "</div>";
//form button
$str .= "<p class='submit'><input type='submit' name='action' value='Update' /></p>";
//hidden values
$str .= "<div><input type='hidden' name='all_ids' value='".implode(",", $current_page_ids)."' /></div>";
$str .= "</form>\n";
//writing JavaScript code
if(count($js_old_ids) > 0) {
$str .= "\n<script type=\"text/javascript\">\n<!--\n";
foreach($js_old_ids as $js) $str .= "old_ids[old_ids.length] = $js;\n";
$str .= "\n-->\n</script>\n";
}
}
return $str;
}
printf (<<<EOF
<html>
<head>
<title>› Terong Related Links</title>
<link rel="stylesheet" href="../../wp-admin/wp-admin.css?version=1.5.1.3" type="text/css" />
<meta http-equiv="Content-Type" content="%s; charset=%s" />
<script type="text/javascript">
<!--
var old_ids = new Array;
function checkInput(f) {
var new_ids = new Array;
for(i=0; i < f.elements.length; i++) if(f.elements[i].type=='checkbox' && f.elements[i].checked) new_ids[new_ids.length] = f.elements[i].value;
if(old_ids.toString() != new_ids.toString()) return confirm("Aenderungen verwerfen?");
}
-->
</script>
</head>
<body>
<div class="wrap">
<p class='submit'><input type='button' value='schliesse das Fenster' onclick='self.close();window.opener.location.reload()' /></p>
%s
</div>
</body>
</html>
EOF
, get_option('html_type'), get_option('blog_charset'), get_posts_list());
exit;
}
function terong_add_quicktags() {
global $is_quicktags, $pagenow;
if($pagenow=="post.php" && isset($_GET['action']) && $_GET['action']=='edit') {
?>
<script language="JavaScript" type="text/javascript">
<!--
function modal_dialog_show(url, width, height) {
if (typeof(window.showModalDialog) != 'undefined')
window.showModalDialog(url, window, "dialogWidth:" + width + "px;dialogHeight:" + height + "px;edge:Raised;center:Yes;help:No;resizable:No;");
else {
var left = screen.availWidth/2 - width/2;
var top = screen.availHeight/2 - height/2;
activeModalWin = window.open(url, "", "width="+width+"px,height="+height+",left="+left+",top="+top);
window.onfocus = function(){if(activeModalWin.closed == false){activeModalWin.focus();};};
}
}
//-->
</script>
<?
if($is_quicktags) {
?>
<script language="JavaScript" type="text/javascript">
<!--
var obj;
if (obj = document.getElementById('ed_toolbar'))
obj.innerHTML += '<input type="button" class="ed_button" value="Aehnliche Artikel" onclick="terong_open_window();"/>';
function terong_open_window() {
strFileName = "../wp-content/plugins/terong_related.php?is_popup=yes¤t_post_id=<?php echo($_GET[post]); ?>";
terong_win = modal_dialog_show(strFileName, 800, 600);
}
//-->
</script>
<?php
}
else {
echo "<div id='top_right_link' style='position: absolute; top: 30px; right: 15px;'><a href='' onclick='modal_dialog_show(\"../wp-content/plugins/terong_related.php?is_popup=yes¤t_post_id=$_GET[post]\", 800, 600)'>» Relevante Beitraege</a></div>";
}
}
}
add_action('admin_footer', 'terong_add_quicktags');
function terong_related($text="") {
if(is_single()) {
global $wpdb, $post;
$searches = get_related_ids($post->ID);
if($searches) {
$text .= "<div id=\"aehnliche\">Relevante Beiträge";
$text .= "<ul>";
foreach($searches as $s) {
if($s->ID != $post->ID) {
$title = trim(stripslashes($s->post_title));
$text .= "<li><a href='".get_permalink($s->ID)."' title=\"$title\" rel=\"bookmark\">$title</a></li>";
}
}
$text .= "</ul></div>";
}
}
return $text;
}
add_filter('the_content', 'terong_related');
function get_related_ids($post_id) {
global $wpdb;
$sql = "SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE meta_key in('related_post_name', 'related_id') AND post_id=$post_id";
$searches = $wpdb->get_results($sql);
if($searches) {
$post_name = array();
$post_id = array();
foreach($searches as $s) {
if($s->meta_key=='related_post_name') {
$arr = explode(',', $s->meta_value);
foreach($arr as $a) $post_name[] = ("'".trim($a)."'");
}
elseif($s->meta_key=='related_id') {
$arr = explode(',', $s->meta_value);
foreach($arr as $a) $post_id[] = ("'".trim($a)."'");
}
}
$text_post_name = implode(', ', $post_name);
$text_post_id = implode(', ', $post_id);
$sql = "SELECT DISTINCT post_title, ID FROM $wpdb->posts WHERE ";
$arr_sql = array();
if($post_name) $arr_sql[] = "post_name in($text_post_name)";
if($post_id) $arr_sql[] = "ID in($text_post_id)";
$sql .= implode(" OR ", $arr_sql);
$sql .= " ORDER BY ID DESC";
$searches = $wpdb->get_results($sql);
return($searches);
}
else return array();
}
/*
Changes Log:
Version 1.1c (16 Oct 2005)
Bug fixes on conflict with other popup based plugins. Thanks to Leonid Mamchenkov
Version 1.1b (13 Oct 2005)
Add alternative links on the top right for some installation which experienced conflicts with other plugins.
Version 1.1b (13 Oct 2005)
A minor bug fix on browser detection, has been tested in Firefox 1.0.7, IE 6.0, Opera 8.0.2
Codes taken from http://webreference.com/tools/browser/javascript.html
Version 1.1a (13 Oct 2005)
Change all table name in SQL query to a generic name i.e $wpdb->posts
Version 1.1 (11 Oct 2005)
1. Check if it is single page (is_single()) then display related links.
It is more make sense to just display related links in a full expanded text no
matter there are any <!--more--> or not.
2. Exclude current post to be displayed in the current links even its ID included in
the related_post_id or the post slug / post_name included in the related_post_name.
3. Add popup search form (click 'Related' quick tag button to open)
*/
?>
Alles anzeigen