Nội dung

Những đoạn code hay dùng để code theme wordpress

3 năm trước, Code theme wordpress, 1787 Lượt xem

Đây là những đoạn code tổng hợp thưởng dùng giúp cho các bạn code theme wordpress 1 cách nhanh hơn!

reponside css

@media only screen and (max-width: 600px) {
  //code
}

Chèn file css

<link type="text/css" rel="stylesheet" href="<?php echo get_template_directory_uri(); ?>/style.css" media="screen,projection">

Chèn file javascript

<script type="text/javascript" src="<?php echo get_template_directory_uri(); ?>/css/jquery-3.2.1.min.js"></script>

Fix admin bar

 <!-- fix admin bar-->
<script>
$(document).ready(function() {
    if ($('#wpadminbar')[0]) {
        $('header').css('top', '32px')
    }
});
</script>

Chèn header và footer

<?php wp_head(); ?>
 <?php wp_footer(); ?>

Lấy đường dẫn trang chủ: Ví dụ: https://netweb.vn/

<?php echo get_home_url(); ?>/

Lấy ghép các file nhỏ trong wordpress

<?php get_template_part('template-parts/menu'); ?>

Đăng ký menu wordpress

register_nav_menus( array(
		'top_menu' => esc_html__( 'Top Menu' ),
		'header_menu' => esc_html__( 'Header Menu' ),
		'menu_mobie' => esc_html__( 'Menu Mobile' ),
		'left_menu_1' => esc_html__( 'Left Menu 1' ),
		'left_menu_2' => esc_html__( 'Left Menu 2' ),
		'left_menu_3' => esc_html__( 'Left Menu 3' )
	) );

Lấy dữ liệu content

<?php the_content(); ?>

Lấy tiêu đề page, post ra ngoài

<?php the_title(); ?>

Lấy ngày dạng d/m/y

<?php the_time('d/m/Y'); ?>

Lấy đường dẫn url hiện tại trong theme

<?php echo the_permalink(); ?>

Get thumbnail url

<?php echo get_the_post_thumbnail_url(); ?>
<?php echo the_post_thumbnail(); ?>

Get mô tả ngắn

<?php the_excerpt(); ?>

Đoạn cho rút ngắn ký tự mô tả wordpress

function custom_excerpt_length( $length ) {
        return 30;
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );

Rút ngắn ký tự cho title

<?php $title = get_the_title(get_the_ID()); ?>
<a href="<?php the_permalink() ?>"> 
<h3> 
<?php echo wp_trim_words($title,18); ?> 
</h3> 
</a>

Dùng cho excerpt mô tả rút ngắn ký tự

<?php $des_ok = get_the_excerpt(); ?> 
<?php echo wp_trim_words($des_ok,10); ?>

Dùng cho acf

Lấy dữ liệu option acf

<?php the_field('footer_lop_moi', 'option') ?>

Lấy field acf

<?php the_field('khu_vuc');?>
<?php echo get_field('chuyen_nganh');?>

Author

https://developer.wordpress.org/reference/functions/get_the_author_meta/

Tạo vòng lặp nhiều field

https://netweb.vn/code-repeater-field-acf-tao-vong-lap-nhieu-field.html

Khai báo tạo template cho page

<?php
/*
Template Name: Home Page
*/
?>

Gravity Forms

<?php echo do_shortcode('[gravityform id="1" title="true" description="true"]'); ?>
[gravityform id="1" title="true" description="true"]

Lấy những bài viết mới nhất wordpress

<div class="khoitinok">
<h3 class="title-wg wow fadeInUp">Tin mới nhất</h3>

    <?php
    $recent_posts = wp_get_recent_posts(array(
        'numberposts' => 10, 
        'post_status' => 'publish' 
    ));
    foreach($recent_posts as $post) : ?>
	<div class="tinmoi wow fadeInUp">
		<div class="hinhtm">
			<a href="<?php echo get_permalink($post['ID']) ?>">
				<?php echo get_the_post_thumbnail($post['ID'], 'full'); ?>
			</a>

		</div>
		<div class="title-tm">
			<a href="<?php echo get_permalink($post['ID']) ?>">
				<?php echo $post['post_title'] ?>
			</a>
		</div>
	</div>
    <?php endforeach; wp_reset_query(); ?>
</div>	

Get tên theo taxonomy

https://stackoverflow.com/questions/3376651/wordpress-how-do-i-get-taxonomy-name-in-taxonomy-php

ẩn acf admin

// hide acf
//add_filter('acf/settings/show_admin', '__return_false');

// function secret_plugin_webcusp()
// {
//     global $wp_list_table;
//     $hidearr = array('advanced-custom-fields-pro/acf.php', 'user-role-editor/user-role-editor.php');
//     $myplugins = $wp_list_table->items;
//     foreach ($myplugins as $key => $val) {
//         if (in_array($key, $hidearr)) {
//             unset($wp_list_table->items[$key]);
//         }
//     }
// }
// add_action('pre_current_active_plugins', 'secret_plugin_webcusp');


//hide Custom Post Type UI
//remove_action('admin_menu', 'cptui_plugin_menu');

// function secret_plugin_webcusp()
// {
//     global $wp_list_table;
//     $hidearr = array('custom-post-type-ui/custom-post-type-ui.php', 'user-role-editor/user-role-editor.php');
//     $myplugins = $wp_list_table->items;
//     foreach ($myplugins as $key => $val) {
//         if (in_array($key, $hidearr)) {
//             unset($wp_list_table->items[$key]);
//         }
//     }
// }
// add_action('pre_current_active_plugins', 'secret_plugin_webcusp');


//add_filter('all_plugins', '__return_empty_array');
final

Update 2021

Đăng ký 1 template mới cho post hoặc post type

<?php
/*
 * Template Name: Single full with
 * Template Post Type: post
 */

?>

get sidebar wordpress

<?php get_sidebar('nice-bar'); ?>

Code thum

 <a href="<?php echo the_permalink(); ?>" class="thumb" title="<?php the_title(); ?>">
							
							<?php if ( has_post_thumbnail() ) { ?>
								 <img src="<?php echo get_the_post_thumbnail_url(); ?>" width="610" height="340" alt="<?php the_title(); ?>">
							  <?php 
								  }else{ 
							  ?>
								 thum bai khi khôn có
								  <?php
							  } 
							  ?>  
                            
                        </a>

Bài viết mới cập nhập

  • Hướng dẫn Plugin floating contact

    Hướng dẫn Plugin floating contact

    9 tháng trước, 329 Lượt xem

    Netweb xin hướng dẫn cách quản lý Plugin floating contact các bạn nhớ làm theo kỹ tường bước để plugin hoạt động Tính năng: Hiển…

  • Backup Yandex Mail

    Backup Yandex Mail

    10 tháng trước, 221 Lượt xem

    Khác với dịch vụ email theo tên của microsoft và google với chi phí đắt đỏ tầm 1tr/năm/1user, vì thế nên netweb khuyên dùng dịch…

Bình luận trên facebook netweb.vn

Đam mê thiết kế web, kinh nghiệm 9 năm trong nghề thiết kế website, đang thiết kế web dạo tại netweb.vn, Callme: 08.9898.2526 (Mr Hải)