Những đoạn code thường dùng khi thiết kế website bằng wordpress
Những đoạn code thường dùng khi thiết kế website bằng wordpress
Mục lục
- 1 1.Code lấy bài viết mặt định.
- 2 2.Code lấy 10 bài viết mới nhất theo category.
- 3 3.Code lấy danh sách chuyên mục
- 4 4.Code tạo menu
- 5 5.Code get menu
- 6 6.Code tạo sidebar
- 7 7.Code get sidebar
- 8 8.Code phân trang.
- 9 9.Code crop ảnh úp lên media
- 10 10.Code lấy ảnh thumbnail
- 11 11.Code bài viết liên quan
- 12 12.Code tính lượt view cho bài viết
- 13 13.Code lấy nội dung bài viết rút gọn.
- 14 14.Code lấy tất cả hình ảnh trong nội dung bài viết.
- 15 15.Code lấy custom filed
- 16 16.Code like box facebook
- 17 17.Code share bài viết liên mạng xã hội
- 18 18.Code query bài viết theo custom filed
- 19 19.Code lấy bài viết ngẫu nhiên.
- 20 20.Code lấy 10 comment mới nhất.
- 21 21.Code comment bằng facebook cho wordpress
- 22 22. Code tạo 1 sortcode đơn giản trong wordpress
1.Code lấy bài viết mặt định.
1
2
3
4
5
6
7
8
|
<!— Get post mặt định —>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php endwhile; else : ?>
<p>Không có</p>
<?php endif; ?>
<!— Get post mặt định —>
|
Đoạn code đặt trong index sẽ lấy list bài viết mới nhất, Đặt trong category sẽ lấy danh sách bài viết của category đó, đặt trong single sẽ lấy nội dung của bài đó!.
2.Code lấy 10 bài viết mới nhất theo category.
1
2
3
4
5
6
7
|
<!— Get post News Query —>
<?php $getposts = new WP_query(); $getposts->query(‘post_status=publish&showposts=10&post_type=post&cat=1’); ?>
<?php global $wp_query; $wp_query->in_the_loop = true; ?>
<?php while ($getposts->have_posts()) : $getposts->the_post(); ?>
<?php endwhile; wp_reset_postdata(); ?>
<!— Get post News Query —>
|
- showposts=10 sẽ lấy 10 bài viết mới nhất
- cat=1 chỉ lấy các bài viết có cat_id bằng 1
3.Code lấy danh sách chuyên mục
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<!— Get category —>
<?php $args = array(
‘hide_empty’ => 0,
‘taxonomy’ => ‘category’,
‘orderby’ => id,
);
$cates = get_categories( $args );
foreach ( $cates as $cate ) { ?>
<li>
<a href=“<?php echo get_term_link($cate->slug, ‘category’); ?>“><?php echo $cate->name ?></a>
</li>
<?php } ?>
<!— Get category —>
|
Đây là đoạn code lấy danh sách các chuyên mục, ‘taxonomy’ => ‘category’ có nghĩa là lấy theo category.
1
2
3
4
5
6
7
8
9
10
|
add_action( ‘init’, ‘register_my_menus’ );
function register_my_menus(){
register_nav_menus(
array(
‘main_nav’ => ‘Menu chính’,
‘link_nav’ => ‘Liên kết’,
‘info_nav’ => ‘Thông tin’,
)
);
}
|
Code này sẽ tạo 3 vị trí đặt menu như trên, Bỏ đoạn code nào vào file functions.php nhé
1
2
3
4
5
6
7
8
|
<?php wp_nav_menu(
array(
‘theme_location’ => ‘main_nav’,
‘container’ => ‘false’,
‘menu_id’ => ‘header-menu’,
‘menu_class’ => ‘menu-main’
)
); ?>
|
Đây là code get menu, chú ý ‘theme_location’ => ‘main_nav’, đây là điểu kiệu chúng ta sẽ lấy menu nào, code này thường được chèn vào file header.php
1
2
3
4
5
6
|
if (function_exists(‘register_sidebar’)){
register_sidebar(array(
‘name’=> ‘Cột bên’,
‘id’ => ‘sidebar’,
));
}
|
Đây là code tạo ra 1 sidebar. Đặt code này vào file functions.php nhé!
1
|
<?php if ( !function_exists(‘dynamic_sidebar’) || !dynamic_sidebar(‘sidebar’) ) : ?><?php endif; ?>
|
Đây là code lấy sidebar ra ngoài, code này thường được đặt trong file sidebar.php
8.Code phân trang.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<?php if(paginate_links()!=”) {?>
<div class=“quatrang”>
<?php
global $wp_query;
$big = 999999999;
echo paginate_links( array(
‘base’ => str_replace( $big, ‘%#%’, esc_url( get_pagenum_link( $big ) ) ),
‘format’ => ‘?paged=%#%’,
‘prev_text’ => __ |