Có phải bạn muốn thiết kế website tiết kiệm chi phí và thời gian mà còn chuẩn seo ?

Thiết kế website bằng wordpress

Với các gói 500k, 750k, 999k là bạn co ngay 1 website đẹp, chuẩn seo

Thiết kế website bằng wordpress

Với các gói 500k, 750k, 999k là bạn co ngay 1 website đẹp, chuẩn seo

Thiết kế website bằng wordpress

Với các gói 500k, 750k, 999k là bạn co ngay 1 website đẹp, chuẩn seo

Thiết kế website bằng wordpress

Với các gói 500k, 750k, 999k là bạn co ngay 1 website đẹp, chuẩn seo

Thiết kế website bằng wordpress

Với các gói 500k, 750k, 999k là bạn co ngay 1 website đẹp, chuẩn seo

12/11/13

Hiển thị bài POST với điều kiện của meta_box hay custom field

Bài toán đặt ra: Viết chức năng của bài post gồm có: nút check, những bài viết nào mà được checked thì mới hiện thi ra trang chủ. Để viết được nút check box thì bạn dùng custom field, hay meta_box (bài này mình sẽ hướng dẫn sau)

Để hiện thị những bài viết nào mà được checked trong admin thì bạn hiểu đơn giản sau: ví dụ mình có 1 check box như sau

<input type="checkbox" name="is_home" value="1"/>

Thì khi lưu bài viết, giá trị có thuộc tính name="is_home", và thuộc tính value="1" được lưu trong bảng post_meta

id         post_id   meta_key   meta_value
xxx       xxx         is_home     1

Để lấy bài viết chỉ khi checked (trong admin) ra ngoài thì bạn hiểu như sau: Lấy những bài viết có is_home=1

Tham khảo đoạn code sau:

query_posts(array(
        'post_type' => 'post',
        'orderby' => $order,
        'cat' => $category,
        'showposts' => 3,
        'meta_query' => array(
            array(
                'key' => 'is_home',
                'value' => '1',
            )
        )

    ));

==> Hiển thị bài viết có danh mục $category (biến truyền vào), kiểu bài viết là "post", sắp xếp thứ tự theo biến $order (biến truyền vào), lấy 3 bài viết thôi và lấy những bài viết nào có is_home=1

Tham khảo những cách sau
Example 1

<?php 
 
// args
$args = array(
 'numberposts' => -1,
 'post_type' => 'event',
 'meta_key' => 'location',
 'meta_value' => 'Melbourne'
);
 
// get results
$the_query = new WP_Query( $args );
 
// The Loop
?>
<?php if( $the_query->have_posts() ): ?>
 <ul>
 <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
  <li>
   <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
  </li>
 <?php endwhile; ?>
 </ul>
<?php endif; ?>
<?php wp_reset_query();  // Restore global post data stomped by the_post(). ?>

Example 2

<?php 
 
// args
$args = array(
 'numberposts' => -1,
 'post_type' => 'event',
 'meta_query' => array(
  'relation' => 'AND',
  array(
   'key' => 'location',
   'value' => 'Melbourne',
   'compare' => '='
  ),
  array(
   'key' => 'attendees',
   'value' => 100,
   'type' => 'NUMERIC',
   'compare' => '>'
  )
 )
);
 
// get results
$the_query = new WP_Query( $args );
 
// The Loop
?>
<?php if( $the_query->have_posts() ): ?>
 <ul>
 <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
  <li>
   <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
  </li>
 <?php endwhile; ?>
 </ul>
<?php endif; ?>
 
<?php wp_reset_query();  // Restore global post data stomped by the_post(). ?>

Example 3

<?php 
 
// args
$args = array(
 'numberposts' => -1,
 'post_type' => 'event',
 'meta_query' => array(
  'relation' => 'OR',
  array(
   'key' => 'location',
   'value' => '%Melbourne%',
   'compare' => 'LIKE'
  ),
  array(
   'key' => 'location',
   'value' => '%Sydney%',
   'compare' => 'LIKE'
  )
 )
);
 
// get results
$the_query = new WP_Query( $args );
 
// The Loop
?>
<?php if( $the_query->have_posts() ): ?>
 <ul>
 <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
  <li>
   <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
  </li>
 <?php endwhile; ?>
 </ul>
<?php endif; ?>
 
<?php wp_reset_query();  // Restore global post data stomped by the_post(). ?>

Example 4

<?php 
 
// args
$args = array(
    'numberposts' => -1,
    'post_type' => 'event',
    'meta_query' => array(
        array(
            'key' => 'images',
            'value' => 0,
            'type' => 'NUMERIC',
            'compare' => '>'
        )
    )
);
 
// get results
$the_query = new WP_Query( $args );
 
// The Loop
?>
<?php if( $the_query->have_posts() ): ?>
    <ul>
    <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
        <li>
            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
            <?php // load 'images' repeater field data. Please see repeater field for code examples ?>
        </li>
    <?php endwhile; ?>
    </ul>
<?php endif; ?>
 
<?php wp_reset_query();  // Restore global post data stomped by the_post(). ?>

Example 5

?php 
 
// custom filter to replace '=' with 'LIKE'
function my_posts_where( $where )
{
 $where = str_replace("meta_key = 'images_%_type'", "meta_key LIKE 'images_%_type'", $where);
 
 return $where;
}
 
add_filter('posts_where', 'my_posts_where');
 
// args
$args = array(
 'numberposts' => -1,
 'post_type' => 'event',
 'meta_query' => array(
  array(
   'key' => 'images_%_type',
   'value' => 'type_1',
  )
 )
);
 
// get results
$the_query = new WP_Query( $args );
 
// The Loop
?>
<?php if( $the_query->have_posts() ): ?>
 <ul>
 <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
  <li>
   <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
  </li>
 <?php endwhile; ?>
 </ul>
<?php endif; ?>
 
<?php wp_reset_query();  // Restore global post data stomped by the_post(). ?>



REF: http://www.advancedcustomfields.com/resources/how-to/how-to-query-posts-filtered-by-custom-field-values/

















12/7/13

Hướng dẫn viết Wedget trong - viết theme trong wordpress

B1: Vào wp-content/themes, tạo cấu trúc theme như sau



B2: Mở file wp-content/themes/nhatnghe/style.css, khai báo đạon code comment sau



Lưu ý: nhatnghe là tên theme đang thiết kế

B3: Trang quản trị Wordpress, chọn "Appearance/Themes"



- Tìm và kích hoạt theme nhatnghe đang thiết kế



B4: Mở trang wp-content/themes/nhatnghe/index.php, trước </head>, chèn code nhúng css

PHP Code:
<link rel="stylesheet" type="text/css" media="all" href="<?php bloginfo'stylesheet_url' ); ?>" />
- Trong thẻ body, viết code vẽ layout cho trang web

PHP Code:
<div id="wrapper">
     <
div id="banner"></div>
    <
div id="menu"></div>
    <
div id="left"></div>
    <
div id="content"></div>
</
div>  
- Mở file wp-content/themes/nhatnghe/style.css, viết code định dạng theme



- Chuyển sang trang chủ, xem kết quả



B5: Chèn menu vào theme, trong div#menu, chèn code sau



Xem kết quả



B6: Mở file style.css, thêm các định dạng css cho menu



Xem kết quả



B7: Trong div#content, chèn code hiện Page/Post

PHP Code:
<?php if ( have_posts() ) : ?> 

<?php while ( have_posts() ) : the_post(); ?> 
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><br><?php the_content(); ?> 
<?php endwhile; ?> 

<?php else: ?> 
<?php endif; ?>
Xem kết quả



B8: Tạo file functions.php để cấu hình vị trí các widget trong theme



B9: Mở file functions.php, khai báo vị trí tên cot_ben_trai_nhatnghe



B10: Vào trang quản trị Wordpress, Themes, Chọn theme nhatnghe. Kéo các widget muốn hiển thị vào Sidebat tên Cột bên trái




B11: MỞ nhatnghe/index.php, trong div#left, khai báo code hiện Sidebar tên cot _trai_nhat_nghe



Xem kết quả

__________________

Viết shortcode trong wordpress

Yêu cầu: Kết nối cơ sở dữ liệu, hiện danh sách dữ liệu lên site

Chuẩn bị:

Cài wordpress

Tạo bảng wp_hocsinh



Nhập dữ liệu.

B1: Tạo cấu trúc cho plugin như sau:



B2: Mở wp-content/plugins/quanlyhocsinh/index.php, gõ code sau:



Lưu ý: QuanLyHocSinh là tên plugin đang thiết kế

B3: Vào trang quản trị WordPress, chọn Plugins/Installed Plugins



- Click “Activate” plugin QuanLyHocSinh

B4: Mở file wp-content/plugins/quanlyhocsinh/index.php, viết code cho plugin





B5: Trang quản trị wordpress, chọn Pages, click “Add New”



- Nhập nội dung Page dạng shortcode



Xem kết quả:


12/5/13

Lấy POST theo post_meta, taxonomy trong wordpress

Ví dụ: trong website có chức năng hiển thị bài viết theo user.

User đó đăng nhập muốn xem những bài viết của mình (id_user được lưu trong meta_post).

Để hiển thị những bài post(bảng post) theo user (bảng metapost) thì chúng ta dùng đoạn code sau
$args = array(
    'post_type' => "buy_a_franchise",
    'post_status' => array('publish', 'pending', 'draft', 'auto-draft', 'future', 'private', 'inherit', 'trash'),
    'orderby' => 'DESC',
    'meta_query' => array(
        array(
            'key' => 'id_user',
            'value' => $id_user,
        )
    )
);

Hiển thị tất cả bài post của theo ID user, ID user được lưu trong bảng post_meta.

Tiếp theo là đoạn code nâng cao hơn, hiển thị bài post theo post_meta, taxonomy

$args = array(
            'post_type' => $posttype,
            'post_status' => array($poststatus),
            'orderby' => 'rand',
            'posts_per_page' => 30,
            'meta_query' => array(
                array(
                    'key' => 'wpcf-paid',
                    'value' => array($paidvalue),
                    'compare' => 'IN',
                )
            ),
            'tax_query' => array(
                'relation' => 'AND',
                array(
                    'taxonomy' => $taxtype,
                    'field' => 'slug',
                    'terms' => $geo
                ),
                array(
                    'taxonomy' => 'brands',
                    'field' => 'slug',
                    'terms' => $brands
                )
            )
        );
        $query->query_vars = $args;