Học Wordpress9.8su10hoc wordpress90286 student
Thứ Ba, 20 tháng 3, 2012

Code Bài Viết Nổi Bật Wordpress

(Học WordPress) - Hi các bạn!
Đang làm cái chức năng lấy các bài viết nổi bật trong một category.Lượn lờ trên mạng tìm kiếm xem.có rất nhiều bài viết nhưng mà vẫn chưa hài lòng lắm.Sau một hồi mò vọc cũng đc ra cái code đúng yêu cầu hiển thị các bài viết trong chuyên mục

<!--side bar-->
 <div>
        <div id="sidebarxemnhieu">
        <div class="iconsidebar"></div>
        <h2 class="my_widget">BÀI VIẾT NỔI BẬT</h2>
        </div>
        <div class="xemnhieu">
                <ul>
                    <?php
                       
                                        // The Query
                //query_posts('meta_key=post_views_count&year=$current_year&monthnum=$current_month&posts_per_page=5&orderby=meta_value_num&order=DESC');
                query_posts( array ( 'category_name' => 'Featured', 'posts_per_page' => 5 ) );
                                       // The Loop
                                       
                                        while ( have_posts() ) : the_post();
                                        //hinh anh
                                       
                ?>

                               
                                  <li>
                                    <div class="imgnew"><a href="<?php the_permalink(); ?>"  title="Permanent Link to <?php the_title(); ?>"><?php get_thumbnail($post->ID, 'thumbnail', 'alt="' . $post->post_title . '"'); ?></a></div>
                                <a href="<?php the_permalink(); ?>"  rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a>
                                   
                                   
                            </li>
                           
                                        <?php
                                        endwhile;
                           
                                        // Reset Query
                                        wp_reset_query();

                                       ?>
                           
                   
                </ul>
            </div>
       
</div>
 <!--end sidebar-->
code này mình chèn ở side bar .
giải thích chút câu lênh query :
query_posts( array ( 'category_name' => 'Featured', 'posts_per_page' => 5 ) );
lấy các bài viết trong 1 category nào đó .ở đây mình lấy tại category Featured. với số bài viết là 5.
Demo: Các bạn có thể xem tại Website : Blog Radio ở phần sidebar.
hi Thêm chút Css cho đẹp mắt tí.Và đây là code css Harry đã làm sẵn ai thích có thể xài nó nhé.chú ý vào trang blog radio để tải mấy cái hình cần thiết cho css.
/******************end share**********************/
#sidebarxemnhieu
{
    background: url("images/sidebarnew.png") no-repeat transparent;
    width:260px;
    height:29px;
 
}
#sidebarxemnhieu h2
{
color: #FFFFFF;
    float: left;
    font-style: bold;
    left: 15px;
    position: relative;
    top: 5px;

}
.iconsidebar
{
background: url("images/icon2.png") no-repeat scroll left -290px transparent;
}
.xemnhieu
{
    background: transparent url(images/back_widget.png) repeat-x top left;
        padding: 10px;
    margin-bottom:20px;
   
}
.xemnhieu ul
{
border: medium none;
    margin: 0;
    padding: 0;
}
.xemnhieu ul li
{
    height: 50px;
    margin:0px 0px 20px 0px ;

   
}
.imgnew
{
    border: 1px solid #DDDDDD;
    height: 50px;
    margin:0px 10px 0px 10px;
    overflow: hidden;

    width: 60px;
    float:left;
}
.xemnhieu ul li a:hover:
{
    background-color:red;
}
/********end bai viet noi bat*************/
Harry cũng gà code lắm nên đặt tên lung tung các bạn sửa lại theo như ý mình nhé.cảm ơn các bạn theo dõi.Mặc dù  viết bài này mục đích chính là ghi lại những gì mình tìm hiểu học được wordpress sau này còn chỗ mà tìm.

Ngoài ra bạn thấy đấy chỉ cần đổi câu lệnh query là ta có thể thực hiện tùy biến không chỉ là bài viết cùng chuyên mục mà lấy cả bài viết cùng tag , bài viết mới nhất và bài viết có số lượng xem nhiều nhất.
bạn có thể vào đây để xem các câu lệnh đó.chúc các bạn thành công .Bài Viết sau Harry làm tut lấy các bài viết được xem nhiều nhất.

1 nhận xét:

Đăng nhận xét

 
;