Posted on March 23rd, 2009 in WP Tips-Tricks | 9,935 views

Trước đây có một số bạn hỏi tôi cách chèn thêm link riêng (ví dụ như link đến forum) vào menu Page của WordPress thì làm thế nào bởi WordPress không hỗ trợ chúng ta làm việc đó trong WP-Admin. Tất nhiên là nếu chịu khó suy nghĩ thì ta vẫn có thể tìm ra cách bởi các menu ấy chẳng qua là code HTML được hàm wp_list_pages() của WordPress xuất ra dựa trên danh sách các Page hiện có trong blog của bạn.

Cách đơn giản nhất để làm chuyện này là mở file header.php của theme mà bạn đang dùng sau đó thêm code HTML vào bên dưới hàm wp_list_pages().
chèn link ngoài vào menu page

Cách thứ hai là bạn dùng một hàm php để "thọc" vào kết quả trả về của hàm wp_list_pages() bằng cách chèn đoạn code dưới đây vào file functions.php

1
2
3
4
5
6
function add_custom_links($output) {
        $output .= '<li><a href="http://hieudt.info/forum/">Forum</a></li>';
	$output .= '<li><a href="http://twitter.com/hieudt/">My Twitter</a></li>';
        return $output;
}
add_filter('wp_list_pages', 'add_custom_links');

Một số lưu ý:
1) Nếu không thích chèn code vào theme thì bạn có thể chèn vào plugin nào đó cũng được nhưng bắt buộc phải đặt code vào giữa khối <?php ... ?>.
2) Ví dụ ở trên chỉ dùng để minh họa, khi sử dụng bạn nên sửa lại theo ý riêng của mình.

Hy vọng bài viết này sẽ giúp ích cho nhiều bạn. Nếu bạn còn có cách nào khác hoặc biết plugin làm được chuyện này thì hoan nghênh bạn chia sẻ với mọi người nhé! :)

Posted on March 13th, 2009 in WP Tips-Tricks | 9,147 views

Vừa rồi tôi đã hướng dẫn bạn cách đếm số comment, còn bây giờ tôi sẽ hướng dẫn đến cách đếm số post và page. Tương tự như comment nhưng post và page lại nằm ở table 'wp_posts' và có kèm theo vài điều kiện khác.

Đoạn code dưới đây sẽ đếm số post:

1
2
3
4
5
6
<?php
global $wpdb;
$num_posts = $wpdb->get_var("SELECT COUNT(ID) FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' AND post_password = ''");
if ($num_posts > 0) $num_posts = number_format($num_posts);
echo 'Có tổng cộng ' . $num_posts . ' bài viết trên blog của tôi';
?>

Các điều kiện đã dùng trong câu SQL trên là:

  • post_type = 'post'
  • post_status = 'publish'
  • post_password = ''

Để đếm số page, bạn chỉ việc thay 'post' thành 'page' ở chỗ post_type, còn nếu bạn muốn đếm luôn cả bài viết có đặt password thì bạn chỉ cần xóa đi điều kiện post_password = '' là xong. :D

Mặc dù hiện nay có khá nhiều plugin có chức năng tạo bảng thống kê trên blog của bạn nhưng nếu bạn chỉ đơn thuần muốn đếm số lượng post/page và comment thì tại sao bạn không tự tạo một bảng thống kê nho nhỏ cho riêng mình chỉ với vài dòng code đơn giản? Những blog chia sẻ software hay ebook mà đặt đoạn code trên ở sidebar thì tuyệt vời. >:d<

Posted on March 12th, 2009 in WP Tips-Tricks | 10,951 views

This article will guide you how to display the number of comments on your blog (of course those are approved comments) by a simple php code below.

1
2
3
4
5
6
<?php
global $wpdb;
$num_comments = $wpdb->get_var("SELECT COUNT(comment_ID) FROM $wpdb->comments WHERE comment_approved = '1'");
if ($num_comments > 0) $num_comments = number_format($num_comments);
echo "There are " . $num_comments . " comments on my blog";
?>

Paste the code to places you like in your theme's php files. And this is a live demo :D

There are 1,226 comments on my blog

In the next article, I will guide you how to count the number of posts/pages on your blog. Remember to read! :)

Posted on December 23rd, 2008 in WP Tips-Tricks | 7,494 views

If you want to display how many SQL queries have been made to your database as I’ve done on this blog like this:

44 queries in 0.699 seconds

Just open the footer.php in your theme folder and paste the following code at anywhere you like:

1
<?php echo get_num_queries(); ?> queries in <?php timer_stop(1); ?> seconds

Another code for displaying that but except regular visitors and search engine bots, we will use a conditional function to do that:

1
2
3
<?php if (is_user_logged_in()) { ?>
<?php echo get_num_queries(); ?> queries in <?php timer_stop(1); ?> seconds.
<?php } ?>

The get_num_queries() function returns the number of executed query during your page loading.

Thanks to Jean-Baptiste Jung for this useful tip.

Posted on December 20th, 2008 in WP Tips-Tricks | 8,636 views
Enhanced Comment Display

Enhanced Comment Display

WordPress 2.7 has just been released few days ago with many significant features make blogging on the new Dashboard more easy than ever. According to that, maybe you will find your themes are no longer compatible with new WordPress version because something in WordPress was changed, but you can get them working again by editing some template files in your theme folder.

Noupe has published an article “Mastering Your WordPress 2.7 Theme & Admin Area: Tips and Tricks“. The article will teach you some tips/tricks to get your theme working again and represent some of the best and significant features added to your Administration Panel.

If you have other tips to make your themes work better, feel free to share it with me!

Posted on December 4th, 2008 in WP Tips-Tricks | 6,950 views

After reading the article “Exclude your own visits in Google Analytics for WordPress blogs” on JohnTP.com,
I think that is very useful trick to exclude your own visits in Google Analytics when you access your WordPress blogs, using the code bellow to do that:

<?php if (!$user_ID) { ?> your Google Analytics code here <?php } ?>

But the code is not pretty well for blogs with enabled user registration because using $user_ID variable will exclude all visitors who has already been a member. You know that we are going to exclude tracking for only the administrator whose username is "admin" and level is 10 so that we need to have a better condition to detect which type of currently logged in user. I have an improvement for the code here, place it to your footer.php :

1
2
3
4
<?php global $current_user; get_currentuserinfo();
if ($current_user->user_level != 10 && $current_user->user_login != 'admin') { ?>
your Google Analytics code here
<?php } ?>

This code is currently working well on my blog. I hope it will help you to exclude your own visits in Google Analytics exactly!

PS: Note that you can use any web statistics code you like (statcounter for example), not just for Google Analytics.

Posted on December 2nd, 2008 in WP Tips-Tricks | 7,868 views

If you intend to exclude pages from search results, you can use the Simply-Exclude plugin. However, another solution that works pretty well is to add a filter in functions.php to only include posts categories instead of excluding pages from search results:

1
2
3
4
5
6
7
function mySearchPostsFilter($query) {
if ($query->is_search()) {
$query->set('cat','12,14,16,17,18'); // your category IDs
}
return $query;
}
add_filter('pre_get_posts','mySearchPostsFilter');

Just replace the posts category IDs (’12,14,16,17,18′) by your owns and it should work. That’s all!