WordPress 高级技巧
如何用JAVASCRIPT在新窗口中打开外部链接?
由于"_blank"标签不符合XHTML 1.0 Strict标准,所以建议用javascript来实现"_blank"功能。
代码如下,将其粘贴到header.php的<HEAD></HEAD>里即可:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | function openExternalLinks() { if (!document.getElementsByTagName) return; var anchors = document.getElementsByTagName("a"); for (var i = 0; i < anchors.length; i++) { var anchor = anchors[i]; var anchorhref = anchors[i].getAttribute("href"); if (anchorhref && anchorhref.search(/^\w+:\/\/(?!www\.funbsd\.net)[^:\.\/]+\.[^:\.\/]+/i)!=-1) { anchor.target="_blank"; // 如果不是本站链接,则在新窗口中打开 } else if (anchorhref && anchorhref.search(/(?:^(?!http)\w+|^(?:http)\w+):\/\/www\.funbsd\.net(?:$|:\d+|\/)/i)!=-1) { anchor.target="_blank"; // 如果是本站的非HTTP链接,则在新窗口中打开 } else if (anchorhref && anchorhref.search(/^(?:http:\/\/www\.funbsd\.net\/download)/i)!=-1) { anchor.target="_blank"; // 如果是本站的子目录"/download",则在新窗口中打开 } } } window.onload = openExternalLinks; |
如何禁止在RSS阅读器里显示私人日志?
默认情况下,私人日志也会显示在RSS阅读器里。
但是私人日志应该在登录以后才能看到,而不应该显示在RSS阅读器里。
解决办法是修改wp-includes/feed-rss2.php文件,在<item></item>外面添加条件判断语句:
1 2 3 4 5 | <?php if ( get_post_status() != 'private' ) { ?> <item> ...... </item> <?php } ?> |
如何显示私人页面?
默认情况下,已发布的私人页面只能在管理页面里看到。
如果想在登录以后看到自己的私人页面,可以修改wp-includes/post.php,找到函数get_pages()中的:
1 | $query = "SELECT * FROM $wpdb->posts $join WHERE (post_type = 'page' AND post_status = 'publish') $where "; |
将其改为:
1 2 3 | global $user_ID; $query = "SELECT * FROM $wpdb->posts $join WHERE (post_type = 'page' AND (post_status = 'publish' OR post_status = 'private' AND post_author = '{$user_ID}')) $where "; |
如何显示私人链接?
默认情况下,已发布的私人链接也只能在管理页面里看到。
如果想在登录以后看到自己的私人链接,可以修改wp-includes/bookmark.php,找到函数get_bookmarks()中的:
1 2 | if ( $hide_invisible ) $visible = "AND link_visible = 'Y'"; |
将其改为:
1 2 3 | global $user_ID; if ( $hide_invisible ) $visible = "AND (link_visible = 'Y' OR link_visible = 'N' AND link_owner = '{$user_ID}')"; |
如何显示私人分类?
默认情况下,如果某个分类的日志都是私人日志,则此分类将不会显示,且私人日志也不会计入分类日志数。
如果想在登录以后看到自己的私人日志分类,可以修改wp-includes/taxonomy.php,找到函数_pad_term_counts()中的:
1 2 | // Get the object and term ids and stick them in a lookup table $results = $wpdb->get_results("SELECT object_id, term_taxonomy_id FROM $wpdb->term_relationships INNER JOIN $wpdb->posts ON object_id = ID WHERE term_taxonomy_id IN (".join(',', array_keys($term_ids)).") AND post_type = 'post' AND post_status = 'publish'"); |
将其改为:
1 2 3 | // Get the object and term ids and stick them in a lookup table global $user_ID; $results = $wpdb->get_results("SELECT object_id, term_taxonomy_id FROM $wpdb->term_relationships INNER JOIN $wpdb->posts ON object_id = ID WHERE term_taxonomy_id IN (".join(',', array_keys($term_ids)).") AND post_type = 'post' AND (post_status = 'publish' OR post_status = 'private' AND post_author = '{$user_ID}')"); |
如何在上一篇/下一篇中显示私人日志?
如果想在登录以后在上一篇/下一篇中看到自己的私人日志,可以修改wp-includes/link-template.php,找到函数get_adjacent_post中的:
1 | $where = apply_filters( "get_{$adjacent}_post_where", $wpdb->prepare("WHERE p.post_date $op %s AND p.post_type = 'post' AND p.post_status = 'publish' $posts_in_ex_cats_sql", $current_post_date), $in_same_cat, $excluded_categories ); |
将其改为:
1 2 | global $user_ID; $where = apply_filters( "get_{$adjacent}_post_where", $wpdb->prepare("WHERE p.post_date $op %s AND p.post_type = 'post' AND (p.post_status = 'publish' OR p.post_status = 'private' AND p.post_author = '{$user_ID}') $posts_in_ex_cats_sql", $current_post_date), $in_same_cat, $excluded_categories ); |
如何在归档和日历中显示私人日志?
如果想在登录以后在归档和日历中看到自己的私人日志,可以安装Show Private Posts插件,其中包含补丁程序。
如何在分类列表中只显示父类的RSS链接或图片?
如果在用wp_list_categories()显示分类列表时,只想显示父类的RSS链接或图片,可以修改wp-includes/classes.php,找到函数start_el()中的:
1 | if ( (! empty($feed_image)) || (! empty($feed)) ) { |
将其改为:
1 | if ( ((! empty($feed_image)) || (! empty($feed))) && ($category->parent == '0') ) { |
如何在归档列表中只显示指定时间内的列表?
如果在用wp_get_archives()显示归档列表时,只想显示1年之内的月份列表,可以修改wp-includes/general-template.php,找到函数wp_get_archives()中的:
1 2 | if ( 'monthly' == $type ) { $query = "SELECT DISTINCT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date DESC $limit"; |
将其改为:
1 2 | if ( 'monthly' == $type ) { $query = "SELECT DISTINCT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, count(ID) as posts FROM $wpdb->posts $join $where AND MONTH(post_date) > MONTH(NOW())-12 GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date DESC $limit"; |












引用 (1)
[...] 但是"_blank"标签不符合XHTML 1.0 Strict标准,所以建议用javascript来实现"_blank"功能。 参考WordPress高级技巧--如何用JAVASCRIPT在新窗口中打开外部链接? [...]