Tăng bộ nhớ Php trong WordPress
Đặt trong file wp-config.php
1 |
define('WP_MEMORY_LIMIT', '79M'); |
Khai báo và sử dụng Header trong WordPress
Khai báo trong file header.php
, đặt trước thẻ đóng của </head>
1 |
<?php wp_head(); ?> |
Gọi Header trong các file như page.php
, single.php
, …
1 |
<?php get_header();?> |
Gọi Header trong trường hợp có nhiều file ví dụ : header-style-01.php
, header-style-02.php
1 |
<?php get_header('style-01');?> |
Khai báo và sử dụng Footer trong WordPress
Khai báo trong file footer.php
, đặt trước thẻ đóng của </body>
1 |
<?php wp_footer(); ?> |
Gọi footer trong các file như page.php
, single.php
, …
1 |
<?php get_footer();?> |
Gọi footer trong trường hợp có nhiều file ví dụ : footer-style-01.php
, footer-style-02.php
1 |
<?php get_footer('style-01');?> |
Một số hàm hiển thị nội dung trong WordPress
Hiển thị lang
của html
(Đặt tại thẻ <html>
)
1 2 3 4 5 6 7 |
<?php language_attributes(); ?> // Hoặc <?php bloginfo('language'); ?> // Vị trí đặt <html <?php language_attributes(); ?>> // Hoặc <html lang="<?php bloginfo('language'); ?>"> |
Hiển thị charset
của meta
(Đặt tại thẻ <meta>
)
1 2 3 |
<?php bloginfo( 'charset' ); ?> // Vị trí đặt <meta charset="<?php bloginfo( 'charset' ); ?>"> |
Hiển thị đường dẫn đến thư mục của theme
1 2 3 4 5 |
<?php echo get_template_directory_uri(); ?> // Hoặc <?php bloginfo('template_directory'); ?> // Hoặc <?php bloginfo('template_url'); ?> |
Hiển thị đường dẫn đến thư mục của theme child
1 2 3 |
<?php echo get_stylesheet_directory_uri();?> or <?php echo get_theme_file_uri();?> |
Hiển thị đường dẫn gốc của site (Domain)
1 |
<?php bloginfo('home'); ?> |
Hiển thị đường dẫn tới thư mục chứa Source wp-admin
1 |
<?php bloginfo('wpurl'); ?> |
Hiển thị đường dẫn page
trong WordPress
1 |
<?php bloginfo('home'); ?>/?page_id=1 |
Hiển thị id của page
hoặc post
1 |
<?php echo get_the_ID(); ?> |
Hiển thị tên trang web
1 |
<?php bloginfo('name'); ?> |
Hiển thị Class trong thẻ <body>
1 2 3 |
<?php body_class(); ?> // Vị trí đặt <body <?php body_class(); ?>> |
Hiển thị tiêu đề của post
hoặc page
(Đặt trong vòng lặp while
)
1 |
<?php the_title(); ?> |
Hiển thị tiêu đề (Tên term
) trong archive.php
1 2 3 |
<?php echo single_cat_title( '', false ); ?> //Hoặc <?php the_category(' '); ?> |
Hiển thị tiêu đề category ngoài archive.php
1 2 3 |
<?php foreach((get_the_category()) as $category): $category_link = get_category_link( $category->term_id ); ?> <a href="<?php echo $category_link; ?>" class="pa-cat"><span class="sp-cat"><?php echo $category->name; ?></span></a> <?php endforeach; ?> |
Hiển thị nội dung của post
hoặc page
(Đặt trong vòng lặp while
)
1 |
<?php the_content(); ?> |
Hiển thị ngày tháng
1 2 3 |
<?php the_time('Y.m.d'); ?> /*Định dạng ngày tháng bên Nhật*/ <?php the_time( 'Y年n月j日'); ?> |
Hiển thị đường dẫn đến chi tiết post
hoặc page
(Đặt trong vòng lặp while
)
1 |
<?php the_permalink(); ?> |
Hiển thị mô tả ngắn của bài viết (Đặt trong vòng lặp while
)
1 |
<?php the_excerpt(); ?> |
Hàm cắt chuỗi
1 2 3 4 5 |
<?php echo mb_strimwidth(get_the_title(), 0, 79, '...'); ?> // Hoặc <?php echo wp_trim_words(get_the_title(), 79, '...' ); ?> // Hoặc <?php echo mb_substr(get_the_excerpt(), 0, 150,'UTF-8').'...'; ?> |
Gọi các thành phần con trong WordPress (Giống như Include
)
1 |
<?php get_template_part( 'part/content', 'search' ); ?> |
Hiển thị Thumbnail
của post
(Đặt trong vòng lặp while
)
1 2 3 4 5 6 7 8 9 |
<?php if ( has_post_thumbnail() ) { ?> <img src="<?php the_post_thumbnail_url(); ?>" alt="<?php the_title(); ?>"> <?php } ?> //Hoặc <?php if ( has_post_thumbnail() ) { ?> <img src="<?php the_post_thumbnail_url(); ?>" alt="<?php the_title(); ?>"> <?php } else { ?> <img src="<?php echo get_stylesheet_directory_uri(); ?>/assets/images/no-image.png" alt="<?php the_title(); ?>"> <?php } ?> |
Khai báo page Template
(Tạo 1 page Template
, ví dụ: page-contact.php
, đặt câu khai báo ở đầu file)
1 |
<?php /* Template Name: Contact */ ?> |
Khắc phục lỗi không canh giữa hình ảnh trong Visual Editor
Wordpress
1 2 3 |
img.alignright { float: right } img.alignleft { float: left } img.aligncenter { display: block; margin-left: auto; margin-right: auto } |
Một số hàm kiểm tra điều kiện (If & Else) trong WordPress
Kiểm tra trang chủ (front-page.php
)
1 2 3 |
<?php if ( is_front_page() { // } ?> |
Kiểm tra trang chi tiết (Của kiểu 1 post type
nhất định, ví dụ : single-sample.php
)
1 2 3 |
<?php if ( is_singular('sample') { // } ?> |
Kiểm tra trang lưu trữ (Của kiểu 1 post type
nhất định, ví dụ : archive-sample.php
)
1 2 3 |
<?php if ( is_post_type_archive('sample') ) { ?> <h1><?php post_type_archive_title(); ?></h1> <?php } ?> |
Get single.php
custom
1 2 3 4 5 6 7 8 |
<?php $post = $wp_query->post; if ( in_category( '1') ) { include( TEMPLATEPATH.'/single-news.php' ); } else if ( in_category( '2') ) { include( TEMPLATEPATH.'/single-event.php' ); } ?> |
1 2 3 4 5 6 7 8 9 |
<?php /*Get in custom post type*/ if (has_term( '14', 'category-wiki')) { include('/cat/wiki/single-wiki-cat14.php'); } else { include('/cat/wiki/single-wiki-default-template.php'); } ?> |
Một số hàm khác
Next
and Pre
In Post
1 2 3 4 5 6 7 8 9 10 11 12 |
<div class="row single-next-prev"> <?php $previous_post = get_previous_post(); if (!empty( $previous_post )): ?> <a style="float:left;font-size:16px;padding:0 2%;box-sizing:border-box;" href=" <?php echo get_permalink( $previous_post->ID );?>" class="link"> Bài viết trước đó </a> <?php endif; ?> <?php $next_post = get_next_post(); if (!empty( $next_post )): ?> <a style="float:right;font-size:16px;padding:0 2%;box-sizing:border-box;" href=" <?php echo get_permalink( $next_post->ID );?>" class="link"> Bài viết kế tiếp </a> <?php endif; ?> </div> |
Hoặc
1 2 3 4 5 |
<?php next_post_link('%link'); ?> //Hiển thị tiêu đề bài viết kết tiếp <?php next_post_link( '%link', get_the_post_thumbnail( $next_post->ID ) ); ?> //Hiển thị hình ảnh thumbai bài viết kế tiếp ------------------------------------------ <?php previous_post_link('%link'); ?> //Hiển thị tiêu đề bài viết trước đó <?php previous_post_link( '%link', get_the_post_thumbnail( $previous_post->ID ) ); ?> //Hiển thị hình ảnh thumbai bài viết trước đó |
Xoá slug category custom post type. Copy code vào file function.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?php function remove_evil_slugs($post_link, $post, $leavename) { if('service' != $post->post_type || 'service' != $post->post_type ||'publish' != $post->post_status) { return $post_link; } $post_link = str_replace('/' . $post->post_type . '/', '/', $post_link); return $post_link; } add_filter('post_type_link', 'remove_evil_slugs', 10, 3); function parse_evil_slugs($query) { if(!$query->is_main_query() || 2 != count($query->query) || !isset($query->query['page'])) { return; } if(!empty($query->query['name'])) { $query->set('post_type', array('post', 'service', 'service', 'page')); //Điền slug của post type } } add_action('pre_get_posts', 'parse_evil_slugs'); ?> |
Vòng lặp gọi Repeater
trong ACF:
1 2 3 |
<?php if( have_rows('name_repeater') ): while ( have_rows('name_repeater')) : the_row(); ?> // Name của Repeater <?php the_sub_field('name_item'); ?>// Gọi Field trong Repeater <?php endwhile; endif; ?> |
Tags
1 2 3 4 5 6 7 |
<?php $tags = get_tags(); // lấy tags foreach ( $tags as $tag ) { // dùng foreach để hiển thị danh sách tags $tag_link = get_tag_link( $tag->term_id ); // lấy link tag $tag_name = $tag->name; // lấy tên tag $tag_slug = $tag->slug; // lấy slug tag ?> <a href="<?php echo $tag_link; ?>"><?php echo $tag_name; ?></a> <?php } ?> |