kết quả xsmn hôm nay
The snippet can be accessed without any authentication.
Authored
Dự đoán chính xác kết quả xsmn hôm nay, soi cầu các cặp loto đẹp trong kqxsmn hôm nay chuẩn xác nhất kèm theo các phân tích soi cầu XSMN với tỷ lệ chính xác cao nhất.
scscsc 43.73 KiB
Đ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.
<!-- 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 -->
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
<!-- 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 -->
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.
4.Code tạo menu
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',
)
);
}
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é
5.Code get menu
<?php wp_nav_menu(
array(
'theme_location' => 'main_nav',
'container' => 'false',
'menu_id' => 'header-menu',
'menu_class' => 'menu-main'
)
); ?>
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
6.Code tạo sidebar
if (function_exists('register_sidebar')){
register_sidebar(array(
'name'=> 'Cột bên',
'id' => 'sidebar',
));
}
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é!
7.Code get sidebar
<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('sidebar') ) : ?><?php endif; ?>
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.
<?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' => __('«'),
'next_text' => __('»'),
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages
) );
?>
</div>
<?php } ?>
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' => __('«'),
'next_text' => __('»'),
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages
) );
?>
</div>
<?php } ?>
Code này thường chèn dưới đoạn code số 1 (Code get post mặt định)
9.Code crop ảnh úp lên media
add_action( 'after_setup_theme', 'wpdocs_theme_setup' );
function wpdocs_theme_setup() {
add_image_size( 'slider-thumb', 750, 375, true);
add_image_size( 'post-thumb', 300, 200, true);
add_image_size( 'tour-thumb', 270, 200, true);
add_image_size( 'video-thumb', 215, 130, true);
}
1
2
3
4
5
6
7
add_action( 'after_setup_theme', 'wpdocs_theme_setup' );
function wpdocs_theme_setup() {
add_image_size( 'slider-thumb', 750, 375, true);
add_image_size( 'post-thumb', 300, 200, true);
add_image_size( 'tour-thumb', 270, 200, true);
add_image_size( 'video-thumb', 215, 130, true);
}
Khi chèn đoạn code này vào file functions.php, hình ảnh upload lên media sẽ được từ động crop thành 4 kích thước như trên.
10.Code lấy ảnh thumbnail
<!-- Get thumbnail -->
<?php echo get_the_post_thumbnail( $post_id, 'full', array( 'class' =>'thumnail') ); ?>
<!-- Get thumbnail -->
1
2
3
<!-- Get thumbnail -->
<?php echo get_the_post_thumbnail( $post_id, 'full', array( 'class' =>'thumnail') ); ?>
<!-- Get thumbnail -->
Code này thường được đặt trong vòng lặp get post.
11.Code bài viết liên quan
Hiển thị bài viết liên quan theo tag
<!-- Hiển thị bài viết theo Tag -->
<div id="relatedposttags">
<?php
$tags = wp_get_post_tags($post->ID);
if ($tags)
{
$tag_ids = array();
foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
// lấy danh sách các tag liên quan
$args=array(
'tag__in' => $tag_ids,
'post__not_in' => array($post->ID), // Loại trừ bài viết hiện tại
'showposts'=>5, // Số bài viết bạn muốn hiển thị.
'caller_get_posts'=>1
);
$my_query = new wp_query($args);
if( $my_query->have_posts() )
{
echo '<h3>Bài viết liên quan</h3><ul>';
while ($my_query->have_posts())
{
$my_query->the_post();
?>
<li><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li>
<?php
}
echo '</ul>';
}
}
?>
</div>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<!-- Hiển thị bài viết theo Tag -->
<div id="relatedposttags">
<?php
$tags = wp_get_post_tags($post->ID);
if ($tags)
{
$tag_ids = array();
foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
// lấy danh sách các tag liên quan
$args=array(
'tag__in' => $tag_ids,
'post__not_in' => array($post->ID), // Loại trừ bài viết hiện tại
'showposts'=>5, // Số bài viết bạn muốn hiển thị.
'caller_get_posts'=>1
);
$my_query = new wp_query($args);
if( $my_query->have_posts() )
{
echo '<h3>Bài viết liên quan</h3><ul>';
while ($my_query->have_posts())
{
$my_query->the_post();
?>
<li><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li>
<?php
}
echo '</ul>';
}
}
?>
</div>
Hiển thị vài viết liên quan theo category
<?php
$categories = get_the_category($post->ID);
if ($categories)
{
$category_ids = array();
foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id;
$args=array(
'category__in' => $category_ids,
'post__not_in' => array($post->ID),
'showposts'=>5, // Số bài viết bạn muốn hiển thị.
'caller_get_posts'=>1
);
$my_query = new wp_query($args);
if( $my_query->have_posts() )
{
echo '<h3>Bài viết liên quan</h3><ul class="list-news">';
while ($my_query->have_posts())
{
$my_query->the_post();
?>
<li>
<div class="new-img"><a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(array(85, 75)); ?></a></div>
<div class="item-list">
<h4><a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h4>
<?php the_excerpt(); ?>
</div>
</li>
<?php
}
echo '</ul>';
}
}
?>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?php
$categories = get_the_category($post->ID);
if ($categories)
{
$category_ids = array();
foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id;
$args=array(
'category__in' => $category_ids,
'post__not_in' => array($post->ID),
'showposts'=>5, // Số bài viết bạn muốn hiển thị.
'caller_get_posts'=>1
);
$my_query = new wp_query($args);
if( $my_query->have_posts() )
{
echo '<h3>Bài viết liên quan</h3><ul class="list-news">';
while ($my_query->have_posts())
{
$my_query->the_post();
?>
<li>
<div class="new-img"><a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(array(85, 75)); ?></a></div>
<div class="item-list">
<h4><a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h4>
<?php the_excerpt(); ?>
</div>
</li>
<?php
}
echo '</ul>';
}
}
?>
2 đoạn code này thường được đặt dưới nội dung bài viết, tức phần dưới của file single.php
12.Code tính lượt view cho bài viết
function setpostview($postID){
$count_key ='views';
$count = get_post_meta($postID, $count_key, true);
if($count == ''){
$count = 0;
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
} else {
$count++;
update_post_meta($postID, $count_key, $count);
}
}
function getpostviews($postID){
$count_key ='views';
$count = get_post_meta($postID, $count_key, true);
if($count == ''){
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
return "0";
}
return $count;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
function setpostview($postID){
$count_key ='views';
$count = get_post_meta($postID, $count_key, true);
if($count == ''){
$count = 0;
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
} else {
$count++;
update_post_meta($postID, $count_key, $count);
}
}
function getpostviews($postID){
$count_key ='views';
$count = get_post_meta($postID, $count_key, true);
if($count == ''){
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
return "0";
}
return $count;
}
Đây là code tính lượt view cho bài viết. Code này được đặt trong file functions.php
Sử dụng:
Chèn code này vào trong file single.php
<?php
setpostview(get_the_id());
?>
1
2
3
<?php
setpostview(get_the_id());
?>
Hiển thị lượt view:
<?php
echo getpostviews(get_the_id());
?>
1
2
3
<?php
echo getpostviews(get_the_id());
?>
13.Code lấy nội dung bài viết rút gọn.
function teaser($limit) {
$excerpt = explode(' ', get_the_excerpt(), $limit);
if (count($excerpt)>=$limit) {
array_pop($excerpt);
$excerpt = implode(" ",$excerpt).'[...]';
} else {
$excerpt = implode(" ",$excerpt);
}
$excerpt = preg_replace('`\[[^\]]*\]`','',$excerpt);
return $excerpt.'...';
}
1
2
3
4
5
6
7
8
9
10
11
function teaser($limit) {
$excerpt = explode(' ', get_the_excerpt(), $limit);
if (count($excerpt)>=$limit) {
array_pop($excerpt);
$excerpt = implode(" ",$excerpt).'[...]';
} else {
$excerpt = implode(" ",$excerpt);
}
$excerpt = preg_replace('`\[[^\]]*\]`','',$excerpt);
return $excerpt.'...';
}
Đây là code lấy nội dung bài viết được chỉ định số từ nhất định. Cách dùng <?php echo teaser(30); ?>, Với được code trên mình sẽ lấy 30 từ đầu tiên trong bài viết!
14.Code lấy tất cả hình ảnh trong nội dung bài viết.
function get_link_img_post(){
global $post;
preg_match_all('/src="(.*)"/Us',get_the_content(),$matches);
$link_img_post = $matches[1];
return $link_img_post;
}
1
2
3
4
5
6
function get_link_img_post(){
global $post;
preg_match_all('/src="(.*)"/Us',get_the_content(),$matches);
$link_img_post = $matches[1];
return $link_img_post;
}
Chèn code này trong file functions.php, để hiển thị chúng ta dùng forech nó ra nhé!
15.Code lấy custom filed
<?php
get_post_meta( $post_id, $key, $single );
?>
1
2
3
<?php
get_post_meta( $post_id, $key, $single );
?>
16.Code like box facebook
<div class="fb-page" data-href="https://facebook.com/huykiradotnet" data-hide-cover="false" data-show-facepile="true" data-show-posts="false"></div>
<div id="fb-root"></div>
<script>
(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/vi_VN/sdk.js#xfbml=1&version=v2.7;
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>
1
2
3
4
5
6
7
8
9
10
11
12
<div class="fb-page" data-href="https://facebook.com/huykiradotnet" data-hide-cover="false" data-show-facepile="true" data-show-posts="false"></div>
<div id="fb-root"></div>
<script>
(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/vi_VN/sdk.js#xfbml=1&version=v2.7;
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>
17.Code share bài viết liên mạng xã hội
<div id="fb-root"></div>
<script>
(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/vi_VN/sdk.js#xfbml=1&version=v2.7";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>
<span class="like">
<div class="fb-like" data-href="<?php the_permalink(); ?>" data-layout="button_count" data-action="like" data-size="small" data-show-faces="true" data-share="true"></div>
<script src="https://apis.google.com/js/platform.js" async defer></script>
<g:plusone size="medium"></g:plusone>
</span>
<span class="social-s">
<a target="_blank" href="https://www.facebook.com/sharer.php?u=<?php the_permalink(); ?>"><i class="fa fa-facebook" aria-hidden="true"></i></a>
<a target="_blank" href="https://plus.google.com/share?url=<?php the_permalink(); ?>"><i class="fa fa-google-plus" aria-hidden="true"></i></a>
<a target="_blank" href="https://twitter.com/share?url=<?php the_permalink(); ?>"><i class="fa fa-twitter" aria-hidden="true"></i></a>
</span>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<div id="fb-root"></div>
<script>
(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/vi_VN/sdk.js#xfbml=1&version=v2.7";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>
<span class="like">
<div class="fb-like" data-href="<?php the_permalink(); ?>" data-layout="button_count" data-action="like" data-size="small" data-show-faces="true" data-share="true"></div>
<script src="https://apis.google.com/js/platform.js" async defer></script>
<g:plusone size="medium"></g:plusone>
</span>
<span class="social-s">
<a target="_blank" href="https://www.facebook.com/sharer.php?u=<?php the_permalink(); ?>"><i class="fa fa-facebook" aria-hidden="true"></i></a>
<a target="_blank" href="https://plus.google.com/share?url=<?php the_permalink(); ?>"><i class="fa fa-google-plus" aria-hidden="true"></i></a>
<a target="_blank" href="https://twitter.com/share?url=<?php the_permalink(); ?>"><i class="fa fa-twitter" aria-hidden="true"></i></a>
</span>
18.Code query bài viết theo custom filed
Query 1 custom filed.
<?php
// args
$args = array(
'numberposts' => -1,
'post_type' => 'event',
'meta_key' => 'location',
'meta_value' => 'Melbourne'
);
// query
$the_query = new WP_Query( $args );
?>
<?php if( $the_query->have_posts() ): ?>
<ul>
<?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>">
<img src="<?php the_field('event_thumbnail'); ?>" />
<?php the_title(); ?>
</a>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
<?php wp_reset_query(); // Restore global post data stomped by the_post(). ?>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?php
// args
$args = array(
'numberposts' => -1,
'post_type' => 'event',
'meta_key' => 'location',
'meta_value' => 'Melbourne'
);
// query
$the_query = new WP_Query( $args );
?>
<?php if( $the_query->have_posts() ): ?>
<ul>
<?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>">
<img src="<?php the_field('event_thumbnail'); ?>" />
<?php the_title(); ?>
</a>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
<?php wp_reset_query(); // Restore global post data stomped by the_post(). ?>
Query nhiều filed
<?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' => '>'
)
)
);
// query
$the_query = new WP_Query( $args );
?>
<?php if( $the_query->have_posts() ): ?>
<ul>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>">
<img src="<?php the_field('event_thumbnail'); ?>" />
<?php the_title(); ?>
</a>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
<?php wp_reset_query(); // Restore global post data stomped by the_post(). ?>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?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' => '>'
)
)
);
// query
$the_query = new WP_Query( $args );
?>
<?php if( $the_query->have_posts() ): ?>
<ul>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>">
<img src="<?php the_field('event_thumbnail'); ?>" />
<?php the_title(); ?>
</a>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
<?php wp_reset_query(); // Restore global post data stomped by the_post(). ?>
19.Code lấy bài viết ngẫu nhiên.
<?php
$postquery = new WP_Query(array('posts_per_page' => 35, 'orderby' => 'rand'));
if ($postquery->have_posts()) {
while ($postquery->have_posts()) : $postquery->the_post();
$do_not_duplicate = $post->ID;
?>
<li>
<a href="<?php the_permalink();?>"><?php the_title();?></a>
</li>
<?php endwhile; } wp_reset_postdata(); ?>
1
2
3
4
5
6
7
8
9
10
<?php
$postquery = new WP_Query(array('posts_per_page' => 35, 'orderby' => 'rand'));
if ($postquery->have_posts()) {
while ($postquery->have_posts()) : $postquery->the_post();
$do_not_duplicate = $post->ID;
?>
<li>
<a href="<?php the_permalink();?>"><?php the_title();?></a>
</li>
<?php endwhile; } wp_reset_postdata(); ?>
Đoạn code trên lấy 35 bài viết ngẫu nhiên.
20.Code lấy 10 comment mới nhất.
<?php
$cmt = get_comments(array(
'status' => 'approve',
'number'=> 10,
));
?>
<div class="content-w content-news">
<ul>
<?php foreach ($cmt as $value) { ?>
<li>
<a href="<?php the_permalink($value->comment_post_ID);?>#comment-<?php echo $value->comment_ID; ?>"><?php echo get_avatar($value->comment_author_email, 150 ); ?></a>
<a href="<?php the_permalink($value->comment_post_ID); ?>#comment-<?php echo $value->comment_ID; ?>"><?php echo $value->comment_author; ?></a> - <span style="color: #cd8a35;font-size: 12px;"><?php echo $value->comment_date; ?></span>
<p style="font-size: 13px;"><?php echo cuttitle($value->comment_content); ?></p>
<div class="clear"></div>
</li>
<?php } ?>
</ul>
</div>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php
$cmt = get_comments(array(
'status' => 'approve',
'number'=> 10,
));
?>
<div class="content-w content-news">
<ul>
<?php foreach ($cmt as $value) { ?>
<li>
<a href="<?php the_permalink($value->comment_post_ID);?>#comment-<?php echo $value->comment_ID; ?>"><?php echo get_avatar($value->comment_author_email, 150 ); ?></a>
<a href="<?php the_permalink($value->comment_post_ID); ?>#comment-<?php echo $value->comment_ID; ?>"><?php echo $value->comment_author; ?></a> - <span style="color: #cd8a35;font-size: 12px;"><?php echo $value->comment_date; ?></span>
<p style="font-size: 13px;"><?php echo cuttitle($value->comment_content); ?></p>
<div class="clear"></div>
</li>
<?php } ?>
</ul>
</div>
Đoạn code trên lấy 10 comment mới nhất có hình avatar
21.Code comment bằng facebook cho wordpress
<div class="cmt">
<div class="fb-comments" data-width="100%" data-href="<?php the_permalink(); ?>" data-numposts="3"></div>
</div>
<div id="fb-root"></div>
<script>
(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/vi_VN/sdk.js#xfbml=1&version=v2.7&appId=750688268378229";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<div class="cmt">
<div class="fb-comments" data-width="100%" data-href="<?php the_permalink(); ?>" data-numposts="3"></div>
</div>
<div id="fb-root"></div>
<script>
(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/vi_VN/sdk.js#xfbml=1&version=v2.7&appId=750688268378229";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>
22. Code tạo 1 sortcode đơn giản trong wordpress
function create_shortcode_randompost() {
$random_query = new WP_Query(array(
'posts_per_page' => 10,
'orderby' => 'rand'
));
ob_start();
if ( $random_query->have_posts() ) :
"<ol>";
while ( $random_query->have_posts() ) :
$random_query->the_post();?>
<li><a href="<?php the_permalink(); ?>"><h5><?php the_title(); ?></h5></a></li>
<?php endwhile;
"</ol>";
endif;
$list_post = ob_get_contents(); //Lấy toàn bộ nội dung phía trên bỏ vào biến $list_post để return
ob_end_clean();
return $list_post;
}
add_shortcode('random_post', 'create_shortcode_randompost');
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
function create_shortcode_randompost() {
$random_query = new WP_Query(array(
'posts_per_page' => 10,
'orderby' => 'rand'
));
ob_start();
if ( $random_query->have_posts() ) :
"<ol>";
while ( $random_query->have_posts() ) :
$random_query->the_post();?>
<li><a href="<?php the_permalink(); ?>"><h5><?php the_title(); ?></h5></a></li>
<?php endwhile;
"</ol>";
endif;
$list_post = ob_get_contents(); //Lấy toàn bộ nội dung phía trên bỏ vào biến $list_post để return
ob_end_clean();
return $list_post;
}
add_shortcode('random_post', 'create_shortcode_randompost');
Đây là sortcode list ra 10 vài viết ngẫu nhiên. Cách dùng là:
<?php
echo do_shortcode('[random_post]');
?>
1
2
3
<?php
echo do_shortcode('[random_post]');
?>
23. Code tự động lưu ảnh về host khi copy bài từ nguồn khác.
class Auto_Save_Images{
function __construct(){
add_filter( 'content_save_pre',array($this,'post_save_images') );
}
function post_save_images( $content ){
if( ($_POST['save'] || $_POST['publish'] )){
set_time_limit(240);
global $post;
$post_id=$post->ID;
$preg=preg_match_all('/<img.*?src="(.*?)"/',stripslashes($content),$matches);
if($preg){
foreach($matches[1] as $image_url){
if(empty($image_url)) continue;
$pos=strpos($image_url,$_SERVER['HTTP_HOST']);
if($pos===false){
$res=$this->save_images($image_url,$post_id);
$replace=$res['url'];
$content=str_replace($image_url,$replace,$content);
}
}
}
}
remove_filter( 'content_save_pre', array( $this, 'post_save_images' ) );
return $content;
}
function save_images($image_url,$post_id){
$file=file_get_contents($image_url);
$post = get_post($post_id);
$posttitle = $post->post_title;
$postname = sanitize_title($posttitle);
$im_name = "$postname-$post_id.jpg";
$res=wp_upload_bits($im_name,'',$file);
$this->insert_attachment($res['file'],$post_id);
return $res;
}
function insert_attachment($file,$id){
$dirs=wp_upload_dir();
$filetype=wp_check_filetype($file);
$attachment=array(
'guid'=>$dirs['baseurl'].'/'._wp_relative_upload_path($file),
'post_mime_type'=>$filetype['type'],
'post_title'=>preg_replace('/\.[^.]+$/','',basename($file)),
'post_content'=>'',
'post_status'=>'inherit'
);
$attach_id=wp_insert_attachment($attachment,$file,$id);
$attach_data=wp_generate_attachment_metadata($attach_id,$file);
wp_update_attachment_metadata($attach_id,$attach_data);
return $attach_id;
}
}
new Auto_Save_Images();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
class Auto_Save_Images{
function __construct(){
add_filter( 'content_save_pre',array($this,'post_save_images') );
}
function post_save_images( $content ){
if( ($_POST['save'] || $_POST['publish'] )){
set_time_limit(240);
global $post;
$post_id=$post->ID;
$preg=preg_match_all('/<img.*?src="(.*?)"/',stripslashes($content),$matches);
if($preg){
foreach($matches[1] as $image_url){
if(empty($image_url)) continue;
$pos=strpos($image_url,$_SERVER['HTTP_HOST']);
if($pos===false){
$res=$this->save_images($image_url,$post_id);
$replace=$res['url'];
$content=str_replace($image_url,$replace,$content);
}
}
}
}
remove_filter( 'content_save_pre', array( $this, 'post_save_images' ) );
return $content;
}
function save_images($image_url,$post_id){
$file=file_get_contents($image_url);
$post = get_post($post_id);
$posttitle = $post->post_title;
$postname = sanitize_title($posttitle);
$im_name = "$postname-$post_id.jpg";
$res=wp_upload_bits($im_name,'',$file);
$this->insert_attachment($res['file'],$post_id);
return $res;
}
function insert_attachment($file,$id){
$dirs=wp_upload_dir();
$filetype=wp_check_filetype($file);
$attachment=array(
'guid'=>$dirs['baseurl'].'/'._wp_relative_upload_path($file),
'post_mime_type'=>$filetype['type'],
'post_title'=>preg_replace('/\.[^.]+$/','',basename($file)),
'post_content'=>'',
'post_status'=>'inherit'
);
$attach_id=wp_insert_attachment($attachment,$file,$id);
$attach_data=wp_generate_attachment_metadata($attach_id,$file);
wp_update_attachment_metadata($attach_id,$attach_data);
return $attach_id;
}
}
new Auto_Save_Images();
Cài này chèn vào file functions.php nha!
24. Code lấy ảnh đầu tiên trong bài viết làm ảnh đại diện
function catch_that_image() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches [1] [0];
if(empty($first_img)){ //Defines a default image
$first_img = "/images/default.jpg"; //Duong dan anh mac dinh khi khong tim duoc anh dai dien
}
return $first_img;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
function catch_that_image() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches [1] [0];
if(empty($first_img)){ //Defines a default image
$first_img = "/images/default.jpg"; //Duong dan anh mac dinh khi khong tim duoc anh dai dien
}
return $first_img;
}
Cách sử dùng: Chèn đoạn code nào vào khu vực muốn hiển thị hình đại diện.
<img src="<?php echo catch_that_image() ?>" />
1
<img src="<?php echo catch_that_image() ?>" />
25. Một số form tìm kiếm đơn giản.
Tìm kiếm theo từ khóa.
<form action="<?php bloginfo('url'); ?>/" method="GET" role="form">
<div class="form-group">
<input type="text" name="s" class="form-control" id="" placeholder="Từ khóa">
</div>
<button type="submit" class="btn btn-primary">Tìm kiếm</button>
</form>
1
2
3
4
5
6
<form action="<?php bloginfo('url'); ?>/" method="GET" role="form">
<div class="form-group">
<input type="text" name="s" class="form-control" id="" placeholder="Từ khóa">
</div>
<button type="submit" class="btn btn-primary">Tìm kiếm</button>
</form>
Tìm kiếm theo 1 post_type nhất định với từ khóa.
<form action="<?php bloginfo('url'); ?>/" method="GET" role="form">
<input type="hidden" name="post_type" value="page">
<div class="form-group">
<input type="text" name="s" class="form-control" id="" placeholder="Từ khóa">
</div>
<button type="submit" class="btn btn-primary">Tìm kiếm</button>
</form>
1
2
3
4
5
6
7
<form action="<?php bloginfo('url'); ?>/" method="GET" role="form">
<input type="hidden" name="post_type" value="page">
<div class="form-group">
<input type="text" name="s" class="form-control" id="" placeholder="Từ khóa">
</div>
<button type="submit" class="btn btn-primary">Tìm kiếm</button>
</form>
Tìm kiếm theo từ khóa kết hợp với category.
<form action="<?php bloginfo('url'); ?>/" method="GET" role="form">
<div class="form-group">
<input type="text" name="s" class="form-control" id="" placeholder="Từ khóa">
</div>
<div class="form-group">
<select name="cat" id="input" class="form-control" required="required">
<option value="">Chọn chuyên mục</option>
<?php $args = array(
'hide_empty' => 0,
'taxonomy' => 'category',
'orderby' => id,
'parent' => 0
);
$cates = get_categories( $args );
foreach ( $cates as $cate ) { ?>
<option value="<?php echo $cate->term_id ?>"><?php echo $cate->name; ?></option>
<?php } ?>
</select>
</div>
<button type="submit" class="btn btn-primary">Tìm kiếm</button>
</form>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<form action="<?php bloginfo('url'); ?>/" method="GET" role="form">
<div class="form-group">
<input type="text" name="s" class="form-control" id="" placeholder="Từ khóa">
</div>
<div class="form-group">
<select name="cat" id="input" class="form-control" required="required">
<option value="">Chọn chuyên mục</option>
<?php $args = array(
'hide_empty' => 0,
'taxonomy' => 'category',
'orderby' => id,
'parent' => 0
);
$cates = get_categories( $args );
foreach ( $cates as $cate ) { ?>
<option value="<?php echo $cate->term_id ?>"><?php echo $cate->name; ?></option>
<?php } ?>
</select>
</div>
<button type="submit" class="btn btn-primary">Tìm kiếm</button>
</form>
25. Code lấy 8 bài viết xem nhiều:
<div class="widget">
<h3>
Bài viết xem nhiều
</h3>
<div class="content-w">
<ul>
<?php $getposts = new WP_query(); $getposts->query('post_status=publish&showposts=8&post_type=post&meta_key=views&orderby=meta_value_num'); ?>
<?php global $wp_query; $wp_query->in_the_loop = true; ?>
<?php while ($getposts->have_posts()) : $getposts->the_post(); ?>
<li>
<h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
</li>
<?php endwhile; wp_reset_postdata(); ?>
</ul>
</div>
</div>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<div class="widget">
<h3>
Bài viết xem nhiều
</h3>
<div class="content-w">
<ul>
<?php $getposts = new WP_query(); $getposts->query('post_status=publish&showposts=8&post_type=post&meta_key=views&orderby=meta_value_num'); ?>
<?php global $wp_query; $wp_query->in_the_loop = true; ?>
<?php while ($getposts->have_posts()) : $getposts->the_post(); ?>
<li>
<h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
</li>
<?php endwhile; wp_reset_postdata(); ?>
</ul>
</div>
</div>
Từ khóa: code hay, Học wordpress, hướng dẫn wordpress, lập trinh theme, lập trình theme wordpress, Theme wordpress
Huy Kira
Đam mê hóa học, ưng thi vào trường kinh tế... Đậu vào trường Sư phạm, quyết tâm đi dạy... Ra trường đi làm designer, tự học frontend, chuyển qua làm PHP, được công ty đào tạo laravel... Và hiện tại đang code react native... 😂😂 tối về viết blog... (Còn tiếp...)
33
Thành Trung:D cảm ơn a...bài viết rất chi tiết
Thích Trả lời 3 năm trước
HuyKiraHe he viết hơi ẩu, Không biết mọi người có hiểu ko :D
Thích Trả lời 3 năm trước
Đạo tặc CommentCũng đang tự xây dựng kho code riêng cho mình rồi. Sang năm rảnh đi nhận làm web dạo :v
Thích Trả lời 3 năm trước
HuyKiraĐúng y, mỗi người nên có 1 kho code riêng cho mình để code mới nhanh được :D
Thích Trả lời 3 năm trước
Hòa TrầnNgười mới chưa biết về wordpress thì nên bắt đầu từ đâu bác :(
Thích Trả lời 3 năm trước
HuyKira- Phải code html cho chắc, sau đó xem mấy seri cùa thạch phạm, lên youtube tìm video wordpress mà coi. - Tốt nhất thì theo 1 sư phụ họ chỉ cho :D
Thích Trả lời 3 năm trước
Hòa Trầnvâng. e thì biết mỗi blogspot.. mẫu nào cũng rip dc.. www.hoatranweb.com tính làm thêm wordpress mà chả biết làm gì. ?
Thích Trả lời 3 năm trước
Nguyễn PhúcCảm ơn Sếp ! đúng những thứ em đang cần :V
Thích Trả lời 3 năm trước
HuyKiraShare ngay và luôn :D
Thích Trả lời 3 năm trước
nguyễn tiến nghĩathêm cái code bình luận mặc định của wordpress đi bác ơi
Thích Trả lời 3 năm trước
Tịnh Nguyễn BlogĐất nước cần những người như bác ! Vào dòm dàn comment toàn các gương mặt thân quen không ta :v
Thích Trả lời 3 năm trước
HuyKiraThank cậu, thì anh em toàn đi bình luận dạo với nhau mà!
Thích Trả lời 3 năm trước
Tịnh Nguyễn BlogƠ cơ mà em đi giao lưu rất hiếm khi gặp bác, sắp tới quẩy lên nhé !
Thích Trả lời 3 năm trước
Copy Huy KiraNgon quá
Thích Trả lời 3 năm trước
DoanBai viet co y nghia
Thích Trả lời 3 năm trước
Học Luật OnlineDữ liệu của Code tính lượt view cho bài viết lưu ở đâu đấy bác ơi, muốn xóa thì làm sao vậy?
Thích Trả lời 3 năm trước
Học Luật OnlineÔi vãi quá, thấy nó ở postmeta rồi :3 website hơn 40 nghìn bài viết, vl rồi, cái datebase nó phình ra 1 cách kinh khủng
Thích Trả lời 3 năm trước
Đình BiWeb chả được cái gì, chỉ được content hay với theme quá chất :))
Thích Trả lời 3 năm trước
HuyKiraUhm web mình chả được chi hết :(
Thích Trả lời 3 năm trước
Toán cấp 3Cám ơn bác, rất bổ ích với mình
Thích Trả lời 3 năm trước
Vũ Huy HoàngHay quá anh em cũng đang học code PHP mà đọc thấy khó hiểu voãi chưởng :D
Thích Trả lời 3 năm trước
Nguyễn Chí CôngChất quá.
Thích Trả lời 2 năm trước
HuyKiraThank bạn :D
Thích Trả lời 2 năm trước
đỗ minhanh huy ơi. có bài viết hướng dẫn tạo widget không anh ơi , cho em xin đi anh, trên mạng có hướng dẫn mà em làm theo thì báo lỗi
Thích Trả lời 2 năm trước
Huy KiraBài viết về chủ đề đó a chưa làm em ơi!
Thích Trả lời 2 năm trước
Toán cấp 2Hay quá, like ngay mới được. Mình đã ứng dụng vào làm website của mình. Cám ơn bạn đã chia sẻ.
Thích Trả lời 2 năm trước
QuíHay quá anh, cảm anh rất nhiều. Còn cái nào hay chia sẽ cho anh em với nghe anh
Thích Trả lời 1 năm trước
Huy KiraKaka còn nhiều :)) vãi bữa share tiếp :D
Thích Trả lời 1 năm trước
Ngọc ĐứcCho mình hỏi một vấn đề liên quan tới việc lấy video từ youtube. Có cách nào lấy video từ kênh trên youtube về website và hiển thị giống như khi click xem một video và có các video liên quan bên cạnh.
Thích Trả lời 9 tháng trước
HongMình đang cần code hiển thị lượt tải file về. Bạn có thể chia sẻ ko?
Thích Trả lời 7 tháng trước
tuan kietanh làm bài hướng dẫn code đưa slider lên wordpress đi ạ
Thích Trả lời 7 tháng trước
Đồ thờ tâm linhthật là hữu ích, chúc website ngày một phát triển, chia sẻ thêm nhiều kiến thức về wordpress tới mọi người.
Thích Trả lời 6 tháng trước
Do JinAdmin cho e hỏi làm sao để tạo phân trang khi sử dụng Get post News Query?
Thích Trả lời 1 tháng trước
AVT
Nội dung bình luận!
Thiết kế web Đà Nẵng
Bài viết liên quan
Hướng dẫn lập trình theme woocommerce từ a đến z
Hướng dẫn lập trình theme woocommerce từ a đến z
Search cha con trong wordpress sử dụng ajax
Search cha con trong wordpress sử dụng ajax
Search cha con trong wordpress tưởng không dễ mà dễ không tưởng
Search cha con trong wordpress tưởng không dễ mà dễ không tưởng
Search theo custom field trong wordpress và các ứng dụng của nó
Search theo custom field trong wordpress và các ứng dụng của nó
Hướng dẫn xây dựng chức năng sản phẩm đã xem trong WooCommerce
Hướng dẫn xây dựng chức năng sản phẩm đã xem trong WooCommerce
Hướng dẫn hiển thị sản phẩm trong giỏ hàng WooCommerce
Hướng dẫn hiển thị sản phẩm trong giỏ hàng WooCommerce
Học wordpress
Search cha con trong wordpress sử dụng ajax
Search cha con trong wordpress sử dụng ajax
22 - 03 - 2020 | lượt xem: 184
Search cha con trong wordpress tưởng không dễ mà dễ không tưởng
Search cha con trong wordpress tưởng không dễ mà dễ không tưởng
21 - 03 - 2020 | lượt xem: 175
Search theo custom field trong wordpress và các ứng dụng của nó
Search theo custom field trong wordpress và các ứng dụng của nó
20 - 03 - 2020 | lượt xem: 171
Hướng dẫn xây dựng chức năng sản phẩm đã xem trong WooCommerce
Hướng dẫn xây dựng chức năng sản phẩm đã xem trong WooCommerce
18 - 03 - 2020 | lượt xem: 198
Hướng dẫn hiển thị sản phẩm trong giỏ hàng WooCommerce
Hướng dẫn hiển thị sản phẩm trong giỏ hàng WooCommerce
15 - 03 - 2020 | lượt xem: 186
Share code
Share plugin Elementor PRO cập nhật thường xuyên
Share plugin Elementor PRO cập nhật thường xuyên
13 - 07 - 2019 | lượt xem: 5368
Share code tin tức wordpress thích hợp làm web blog du lịch
Share code tin tức wordpress thích hợp làm web blog du lịch
22 - 04 - 2019 | lượt xem: 5313
Share full code tin tức bóng đá sử dụng wordpress
Share full code tin tức bóng đá sử dụng wordpress
16 - 02 - 2019 | lượt xem: 5151
Share 3 mẫu website bán hàng cực đẹp (Template HTML)
Share 3 mẫu website bán hàng cực đẹp (Template HTML)
27 - 10 - 2018 | lượt xem: 6695
Share 3 mẫu website trang profile cá nhân website CV giới thiệu bản thân
Share 3 mẫu website trang profile cá nhân website CV giới thiệu bản thân
04 - 10 - 2018 | lượt xem: 7115
Thông tin
Hiện tại blog bị giới hạn share trên fb cũng như, fan page. Nên khổng thể login bằng fb nha!
Mình có nhận thiết kế web nha!
Đã làm việc tại Đà Nẵng
Từng học tại Trường Đại học Sư phạm Đà Nẵng
Sống tại Đà Nẵng
Đến từ Chu Lai, Quang Nam-Da Nang, Vietnam
Đang hẹn hò với Mai Nhi
Bài viết sắp ra mắt
Hướng dẫn viết plugin đơn giản trong wordpress từ a đến z
Hướng dẫn viết widget bài viết theo chuyên mục trong wordpress
Video mới
Bài 11: Tạo và get sidebar của website woocommerceBài 10: Get sản phẩm theo chuyên mụcBài 9: Hiển thị sản phẩm nổi bật của website woocommerceBài 8: Tạo và hiển thị slider website, viết code file slider.phpBài 7: Ráp menu website và code file header.phpBài 6: Đăng ký menu, wiget, viết code cho file functions.php của theme woocommerceBài 5: Tạo cây thư mục theme cho website woocommerceBài 4: Demo giao diện cần làm, chuyển html thành theme wordpress woocommerceBài 3: Đăng sản phẩm demo, import sản phẩm từ website WooCommerce có sẵn
Học wordpress
Search cha con trong wordpress sử dụng ajax
Search cha con trong wordpress sử dụng ajax
22 - 03 - 2020
Search cha con trong wordpress tưởng không dễ mà dễ không tưởng
Search cha con trong wordpress tưởng không dễ mà dễ không tưởng
21 - 03 - 2020
Search theo custom field trong wordpress và các ứng dụng của nó
Search theo custom
Please register or sign in to comment