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

Showing posts with label thu-thuat-wordpress. Show all posts
Showing posts with label thu-thuat-wordpress. Show all posts

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;

11/29/13

add_image_size thêm kích thước ảnh mới trong wordpress

Tùy theo theme mà có kích thước ảnh khác nhau. Các ảnh này được resize theo 1 kích thước đã được khai báo trong file functions.php

Trong quá trình viết theme, bạn cần bổ sung 1 hình có kích thước mới ví dụ 152 X 92 pixels
    
Trong wordpress hỗ trợ tốt chúng ta trong phần này
            
    add_image_size('franchise-thumbnails', 152, 92, true);
    
=> Đây là hàm thêm kích thước ảnh mới với: tên, width, height, True hoặc false là drop ảnh hay không
            
Hàm này bạn chèn thêm trong file functions.php, lưu ý: Nó sẽ chỉ có hiệu lực khi bạn upload hình mới
        
Tham khảo: 

11/20/13

Sắp xếp post type trong admin wordpress

Mặc định bạn tạo ra 1 post type mới, danh sách các bài post sẽ sắp xếp theo 1 thứ tự mặc định (theo tiêu đề ASC). Bạn muốn tùy chỉnh việc sắp xếp này, có thể theo tiêu đề, theo ID, theo ngày

Tham khảo mẫu code dưới đâu. Code này được chèn trong file functions.php (hoặc kèm trong 1 plugin của bạn)

function set_custom_post_types_admin_order($wp_query) {
        if (is_admin()) {

            // Get the post type from the query
            $post_type = $wp_query->query['post_type'];

            if ($post_type == 'buy_a_franchise') {

                // 'orderby' value can be any column name
                //$wp_query->set('orderby', 'title');
                // 'order' value can be ASC or DESC
                $wp_query->set('order', 'DESC');
            }
        }
    }
    add_filter('pre_get_posts', 'set_custom_post_types_admin_order');

11/6/13

Wordpress Upload Image from front-end and get its url

function agp_process_woofile($files, $post_id, $caption){


  require_once(ABSPATH . "wp-admin" . '/includes/image.php');
  require_once(ABSPATH . "wp-admin" . '/includes/file.php');
  require_once(ABSPATH . "wp-admin" . '/includes/media.php');

    $attachment_id = media_handle_upload($files, $post_id);

 $attachment_url = wp_get_attachment_url($attachment_id);
  add_post_meta($post_id, '_file_paths', $attachment_url);

    $attachment_data = array(
    'ID' => $attachment_id,
    'post_excerpt' => $caption
  );

  wp_update_post($attachment_data);

  return $attachment_id;

} 
REF:http://stackoverflow.com/questions/17351348/upload-image-from-front-end-and-get-its-url

10/30/13

Hiển thị danh sách "post_type" trong wordpress



Hàm lấy danh sách post type theo tên post_type
<?php
        $args = array(
                                'numberposts' => 0,
                                'post_type'   => 'country-origin'
                            );
         $result = get_posts($args);                           
?>

Hàm lặp và hiển thị danh sách
<?php
     foreach($result as $row){
 ?>
<option value="<?=$row->ID?>"><?=$row->post_title?></option>
<?}?>


Ghi chú:
- $row->ID: Trả về ID
- $row->post_title: Trả về tên của post type



Wordpress lấy danh mục - get categories

Description

Returns an array of category objects matching the query parameters.
Arguments are pretty much the same as wp_list_categories and can be passed as either array or in query syntax.

Usage

 <?php $categories get_categories$args ); ?> 

Default Usage

<?php 

$args = array(
	'type'                     => 'post',
	'child_of'                 => 0,
	'parent'                   => '',
	'orderby'                  => 'name',
	'order'                    => 'ASC',
	'hide_empty'               => 1,
	'hierarchical'             => 1,
	'exclude'                  => '',
	'include'                  => '',
	'number'                   => '',
	'taxonomy'                 => 'category',
	'pad_counts'               => false 

); 

?>

Parameters

type 
(string) Type of category to retrieve
  • post - default
  • link
Note: type=link has been deprecated from WordPress 3.0 onwards. Use taxonomy=link_category instead.
child_of 
(integer) Display all categories that are descendants (i.e. children & grandchildren) of the category identified by its ID. There is no default for this parameter. If the parameter is used, the hide_empty parameter is set to false.
parent 
(integer) Display only categories that are direct descendants (i.e. children only) of the category identified by its ID. This does NOT work like the 'child_of' parameter. There is no default for this parameter. [In 2.8.4]
orderby 
(string) Sort categories alphabetically or by unique category ID. The default is sort by Category ID. Valid values:
  • id
  • name - default
  • slug
  • count
  • term_group
order 
(string) Sort order for categories (either ascending or descending). The default is ascending. Valid values:
  • asc - default
  • desc
hide_empty 
(boolean) Toggles the display of categories with no posts. The default is 1 for true or you can add '0' for false (show empty categories). Valid values:
  • 1 - default
  • 0
hierarchical 
(boolean) When true, the results will include sub-categories that are empty, as long as those sub-categories have sub-categories that are not empty. The default is true. Valid values:
  • 1 (true) - default
  • 0 (false)
exclude 
(string) Excludes one or more categories from the list generated by wp_list_categories. This parameter takes a comma-separated list of categories by unique ID, in ascending order. See the example.
include 
(string) Only include certain categories in the list generated by wp_list_categories. This parameter takes a comma-separated list of categories by unique ID, in ascending order. See the example.
  • list - default.
  • none
number 
(string) The number of categories to return
taxonomy 
(string or array) Taxonomy to return. This parameter added at Version 3.0 Valid values:
  • category - default
  • taxonomy - or any registered taxonomy
pad_counts 
(boolean) Calculates link or post counts by including items from child categories. Valid values:
  • 1 (true)
  • 0 (false) - default

Return values

(array) 
Returns an array of category objects matching the query parameters.
The complete content of $category is:
$category->term_id
$category->name
$category->slug
$category->term_group
$category->term_taxonomy_id
$category->taxonomy
$category->description
$category->parent
$category->count
$category->cat_ID
$category->category_count
$category->category_description
$category->cat_name
$category->category_nicename
$category->category_parent

Examples

Dropdown Box as used in Parent category at post category page

This is the code used in the build in category page. Code from 3.0.1
wp_dropdown_categories(array('hide_empty' => 0, 'name' => 'category_parent', 'orderby' => 'name', 'selected' => $category->parent, 'hierarchical' => true, 'show_option_none' => __('None')));
This slightly altered code will grab all categories and display them with indent for a new level (child category). The select box will have a name= and id= called 'select_name'. This select will not display a default "none" as the original code was used to attach a category as a child to another category (or none).
wp_dropdown_categories(array('hide_empty' => 0, 'name' => 'select_name', 'hierarchical' => true));

Dropdown Box

Here's how to create a dropdown box of the subcategories of, say, a category that archives information on past events. This mirrors the example of the dropdown example of wp_get_archives which shows how to create a dropdown box for monthly archives.
Suppose the category whose subcategories you want to show is category 10, and that its category "nicename" is "archives".
<select name="event-dropdown" onchange='document.location.href=this.options[this.selectedIndex].value;'> 
 <option value=""><?php echo esc_attr(__('Select Event')); ?></option> 
 <?php 
  $categories = get_categories('child_of=10'); 
  foreach ($categories as $category) {
  	$option = '<option value="/category/archives/'.$category->category_nicename.'">';
	$option .= $category->cat_name;
	$option .= ' ('.$category->category_count.')';
	$option .= '</option>';
	echo $option;
  }
 ?>
</select>

List Categories and Descriptions

This example will list in alphabetic order, all categories presented as links to the corresponding category archive. Each category descripition is listed after the category link.
<?php
$args = array(
  'orderby' => 'name',
  'order' => 'ASC'
  );
$categories = get_categories($args);
  foreach($categories as $category) { 
    echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
    echo '<p> Description:'. $category->description . '</p>';
    echo '<p> Post Count: '. $category->count . '</p>';  } 
?>

Get only top level categories

To get the top level categories only, set parent value to zero. This example gets link and name of top level categories.
<?php
$args = array(
  'orderby' => 'name',
  'parent' => 0
  );
$categories = get_categories( $args );
foreach ( $categories as $category ) {
	echo '<a href="' . get_category_link( $category->term_id ) . '">' . $category->name . '</a><br/>';
}
?>

Source File

get_categories() is located in wp-includes/category.php.