Tổng hợp một số code wordpress thường dùng trong thiết kế website bằng wordpress mà happiweb.net sử dụng
1. Lặp hiển thị theo post_type
<?php
$args = array(
'post_type' => 'slider',
'post_status' => 'publish',
'showposts' => -1,
);
$the_query = new WP_Query($args);
while ($the_query->have_posts()) {
$the_query->the_post();
?>
// code của bạn
<?php } ?>
Chú ý: 'post_type' => 'slider' có thể thay đổi theo post_type của bạn
2. Lấy URL full của hình ảnh
<?php
<?php
$image_url = wp_get_attachment_image_src(get_post_thumbnail_id(get_the_ID()), 'large');
echo $image_url[0];
echo $image_url[0];
?>
Chú ý: Ngoài large có thể còn: full, thumbnail, medium
3. Xuất menu
<?php
$defaults = array(
'theme_location' => 'main_menu',
'menu' => '',
'container' => '',
'container_class' => '',
'container_id' => '',
'menu_class' => 'contact-header-menu',
'menu_id' => 'menu',
'echo' => true,
'fallback_cb' => 'wp_page_menu',
'before' => '',
'after' => '',
'link_before' => '',
'link_after' => '',
'items_wrap' => '<ul id="%1$s" class="%2$s">%3$s</ul>',
'depth' => 0,
'walker' => ''
);
wp_nav_menu($defaults);
?>
5. WP_Query post_count
<?php
$count = $the_query->post_count;
while ($the_query->have_posts()) {
$the_query->the_post();
}
?>
5. List sản phẩm không thuộc danh mục
<?php
global $paged;
if (get_query_var('paged'))
$my_page = get_query_var('paged');
else {
if (get_query_var('page'))
$my_page = get_query_var('page');
else
$my_page = 1;
set_query_var('paged', $my_page);
$paged = $my_page;
}
$args = array(
'post_type' => 'sanpham',
'posts_per_page' => 6,
'tax_query' => array(
array(
'taxonomy' => 'danhmuc',
'field' => 'id',
'terms' => 9,
'operator' => 'NOT IN'
)
),
'paged' => $my_page
);
$the_query = new WP_Query($args);
if ($the_query->have_posts()) {
while ($the_query->have_posts()) {
$the_query->the_post();
$href = get_permalink();
$title = get_the_title();
$post_id = get_the_ID();
$getPrice = "Giá: " . get_field("gia", $post_id);
?>
<?php
}
} else {
echo "<p class='no-result'>Hiện sản phẩm đang cập nhật!</p>";
}
wp_reset_query();
if (function_exists('wp_pagenavi')) {
wp_pagenavi(array('query' => $the_query));
}
?>
// Hiện 6 sản phẩm, không hiển thị những sản phẩm có id danh mục là: 9, code có phân trang
3. Xuất menu
<?php
$defaults = array(
'theme_location' => 'main_menu',
'menu' => '',
'container' => '',
'container_class' => '',
'container_id' => '',
'menu_class' => 'contact-header-menu',
'menu_id' => 'menu',
'echo' => true,
'fallback_cb' => 'wp_page_menu',
'before' => '',
'after' => '',
'link_before' => '',
'link_after' => '',
'items_wrap' => '<ul id="%1$s" class="%2$s">%3$s</ul>',
'depth' => 0,
'walker' => ''
);
wp_nav_menu($defaults);
?>
4. Lặp hiển thị theo post_type có meta_query
<?php
$args = array(
'showposts' => 4,
'post_type' => 'sanpham',
'meta_query' => array(
array(
'key' => 'is_sale',
'value' => '1',
)
<?php
$args = array(
'showposts' => 4,
'post_type' => 'sanpham',
'meta_query' => array(
array(
'key' => 'is_sale',
'value' => '1',
)
)
);
$the_query = new WP_Query($args);
while ($the_query->have_posts()) {
$the_query->the_post();
$post_id = get_the_ID();
$href = get_permalink();
$title = get_the_title();
$gia = get_field("gia", $post_id);
?>
$the_query = new WP_Query($args);
while ($the_query->have_posts()) {
$the_query->the_post();
$post_id = get_the_ID();
$href = get_permalink();
$title = get_the_title();
$gia = get_field("gia", $post_id);
?>
// loop code
<?php
}
wp_reset_query();
?>
wp_reset_query();
?>
5. WP_Query post_count
<?php
$args = array(
'posts_per_page' => 5,
'post_type' => 'post',
);
$the_query = new WP_Query($args);$count = $the_query->post_count;
while ($the_query->have_posts()) {
$the_query->the_post();
}
?>
5. List sản phẩm không thuộc danh mục
<?php
global $paged;
if (get_query_var('paged'))
$my_page = get_query_var('paged');
else {
if (get_query_var('page'))
$my_page = get_query_var('page');
else
$my_page = 1;
set_query_var('paged', $my_page);
$paged = $my_page;
}
$args = array(
'post_type' => 'sanpham',
'posts_per_page' => 6,
'tax_query' => array(
array(
'taxonomy' => 'danhmuc',
'field' => 'id',
'terms' => 9,
'operator' => 'NOT IN'
)
),
'paged' => $my_page
);
$the_query = new WP_Query($args);
if ($the_query->have_posts()) {
while ($the_query->have_posts()) {
$the_query->the_post();
$href = get_permalink();
$title = get_the_title();
$post_id = get_the_ID();
$getPrice = "Giá: " . get_field("gia", $post_id);
?>
<?php
}
} else {
echo "<p class='no-result'>Hiện sản phẩm đang cập nhật!</p>";
}
wp_reset_query();
if (function_exists('wp_pagenavi')) {
wp_pagenavi(array('query' => $the_query));
}
?>
// Hiện 6 sản phẩm, không hiển thị những sản phẩm có id danh mục là: 9, code có phân trang
Đăng nhận xét
Vui lòng không spam comment bạn nhé!