|
Hiển thị
Hiển thị nội dung liên quan:
- Mở template single.php chèn vào cuối trong vòng lặp nội dung đoạn code sau:
echo yarpp_related( array('post_type' => array('mynews'),'require_tax' => array('news_tax' => 1)),get_the_ID());
|
Cú pháp:
echo yarpp_related(
$settings, //tham số cài đặt
$post_id //post id
// third argument: (optional) true to echo the HTML block; false to return it
true/false
);
|
Tham số: cho phép bạn sử dụng cấu hình riêng, nếu không muốn kế thừa mặc định.
array(
'post_type' => array('post', 'page', ...),
'show_pass_post' => false, // show password-protected posts
// show only posts which were published before the reference post
'past_only' => false,
//a list of term_taxonomy_ids. entities with any of these terms will be excluded from consideration.
'exclude' => array(),
//to limit to entries published recently, set to something like '15 day', '20 week', or '12 month'.
'recent' => false,
// Weights are used to construct the "match score" between candidates and the reference post
'weight' => array(
'body' => 1,
'title' => 2, // larger weights mean this criteria will be weighted more heavily
'tax' => array(
'post_tag' => 1,
... // put any taxonomies you want to consider here with their weights
)
),
// Specify taxonomies and a number here to require that a certain number be shared:
'require_tax' => array(
'post_tag' => 1 // for example, this requires all results to have at least one 'post_tag' in common.
),
// The threshold which must be met by the "match score"
'threshold' => 5,
// Display options:
'template' => , // either the name of a file in your active theme or the boolean false to use the builtin template
'limit' => 5, // maximum number of results
'order' => 'score DESC'
);
|
Sử dụng cài đặt mặc định đã thiết lập trong admin, bỏ qua tham số đầu tiên, thiết lập giá trị null để sử dụng mặc định.
yarpp_related(null, get_the_ID());
|
Không cần chỉ định post ID thì mặc định lấy post hiện tại. Chèn trong vòng lặp posts.
yarpp_related()
|
Hàm get_yarpp_related cho kết quả tương tự nhưng không echo, mà thay vì đó là trả về giá trị.
yarpp_get_related( $args, $reference_ID )
|
- Cách khác bạn có thể sử dụng widget để hiển thị nội dung liên quan, widget sẽ hoạt động ở trang post chi tiết.
Tại trang quản lý widgets, Kéo widget có tên "Related Posts (YARPP)" vào sidebar.

Chọn giao diện hiển thị và tiêu đề cho widget và nhấn Lưu thay đổi.
Wordpress cung cấp API cho phép bạn nhúng nội dung widget trực tiếp vào trong code, chúng ta sử dụng hàm the_widget có thể hiển thị mọi nội dung widget giống như bạn thao tác gọi widget trên giao diện sidebar.
<?php the_widget('YARPP_Widget','title=Related Posts&template=yarpp-template-hoangweb.php',array('before_title'=>'','after_title'=>'','before_widget'=>'','after_widget'=>''))?>
|
Truyền tên Class khởi tạo widget tương ứng vào tham số đầu tiên, tham số thứ 2 bạn thiết lập giá trị options của widget với chuỗi tham số kiểu URL hoặc truyền giá trị mảng. Tham số cuối cùng, là thông số sidebar bao toàn bộ phần hiển thị của widget.
Sử dụng template.
Để kích hoạt template bạn nhấn vào Custom, nếu không tìm thấy templates của yarpp trong theme nó sẽ xuất hiện một nút bạn nhấn vào đó để copy toàn bộ template mẫu tại thư mục yet-another-related-posts-plugin\yarpp-templates
vào thư mục theme hiện tại. Chọn template bạn muốn hiển thị, ở mục "Template file". Cuối cùng nhấn lưu thay đổi.
Đối với widget, nếu các templates của yarpp đã sẵn sàng tìm thấy một danh sách được hiển thị ra sau khi bạn nhấn vào nút custom.

Tham số template này trong widget có option name là 'template' nếu bạn không biết thì có thể tìm bằng cách chuột phải chọn kiểm tra phần tử và tìm đến thuộc tính name của trường 'template file'.
<?php the_widget('YARPP_Widget','title=Related Posts&template=yarpp-template-hoangweb.php',array('before_title'=>'','after_title'=>'','before_widget'=>'','after_widget'=>''))?>
|
Made with help of Dr.Explain
Unregistered version