Những dòng code hữu ích của HWP

Ngày đăng: 20/05/2019 14:16 Chiều

HWP đang có kế hoạch mở khóa học wordpress chuyên sâu (dành cho bạn nào muốn phát triển nghề lập trình wordpress), lập trình từng dòng code. Bạn nào quan tâm vui lòng để lại tin nhắn bên dưới.

1) .htaccess chuyển hướng link từ https.www về no www

RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

————-
Code Full: Chuyển từ không có https qua có https, từ có www sang non www.
Thay domain.com thành tên miền của bạn

RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain.com [NC]
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://domain.com/$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^www.domain.com [NC]
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://domain.com/$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

2) Phân trang riêng cho taxanomy trong wordpress

Thêm code này vào trong: functions.php
Thay bdsgiatot_cat bằng tên taxanomy của bạn
Thay 16 bằng số tin hiển thị của bạn

function set_posts_per_page_for_towns_cpt($query) {
    if (!is_admin() && $query->is_main_query() && $query->tax_query->queries[0]['taxonomy'] == "bdsgiatot_cat") {
        $query->set('posts_per_page', '16');
    }
    if (!is_admin() && $query->is_main_query() && $query->tax_query->queries[0]['taxonomy'] == "duan_cat") {
        $query->set('posts_per_page', '16');
    }
}
add_action('pre_get_posts', 'set_posts_per_page_for_towns_cpt');

3) Hàm lấy ID Youtube chính xác nhất

public function getIDYoutube($url) {
        $pattern = '%^# Match any youtube URL
                (?:https?://)?  # Optional scheme. Either http or https
                (?:www\.)?      # Optional www subdomain
                (?:             # Group host alternatives
                  youtu\.be/    # Either youtu.be,
                | youtube\.com  # or youtube.com
                  (?:           # Group path alternatives
                    /embed/     # Either /embed/
                  | /v/         # or /v/
                  | /watch\?v=  # or /watch\?v=
                  )             # End path alternatives.
                )               # End host alternatives.
                ([\w-]{10,12})  # Allow 10-12 for 11 char youtube id.
                $%x'
        ;
        $result = preg_match($pattern, $url, $matches);
        if ($result) {
            return $matches[1];
        }
        return false;
    }

 

4) $query->set(‘meta_query’, $meta_query); | add_action(‘pre_get_posts’

function set_posts_per_page_for_towns_cpt($query) {
    $queried_object = get_queried_object();
    if (!is_admin() && $queried_object->name == "project") {
        $meta_query = $query->get('meta_query');
        $meta_query[] = array(
            'meta_query' => array(
                array(
                    'key' => 'post_parent',
                    'value' => '',
                )
            )
        );
        $query->set('meta_query', $meta_query);
    }
}

add_action('pre_get_posts', 'set_posts_per_page_for_towns_cpt');
4/5 - (2 bình chọn)