nginx https配置模板

server {
    listen 443 ssl http2 default_server;
    server_name  notes.wujie.me;
    root /var/www/notes.wujie.me;
    index  index.html index.htm index.php;

    #文件上传大小限制  必须要放在server下的server_name下
    client_max_body_size 200m;

    # 因为是默认的 https 站点,所以有可能是从 IP 进来的请求,那么把它跳转到域名
    if ($host != 'notes.wujie.me) {
        rewrite ^/(.*)$ https://notes.wujie.me/$1 permanent;
        break;
    }

    ssl_certificate /etc/nginx/ssl/notes.wujie.me.pem;
    ssl_certificate_key /etc/nginx/ssl/notes.wujie.me.key;

    #加上TLSv1,HTTPS检测会报PCI DSS不合规
    ssl_protocols  TLSv1.2 TLSv1.3;# Requires nginx >= 1.13.0 else use TLSv1.2
    ssl_prefer_server_ciphers on;
    ssl_ciphers EECDH+AESGCM:EDH+AESGCM;
    ssl_ecdh_curve secp384r1; # Requires nginx >= 1.1.0
    ssl_session_timeout 10m;
    ssl_session_cache shared:SSL:10m;
    ssl_session_tickets off; # Requires nginx >= 1.5.9
    ssl_stapling on; # Requires nginx >= 1.3.7
    ssl_stapling_verify on; # Requires nginx => 1.3.7

    add_header X-Frame-Options SAMEORIGIN;
    add_header X-Content-Type-Options nosniff;
    add_header X-XSS-Protection "1; mode=block";

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    # pass PHP scripts to FastCGI server
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php8.3-fpm.sock;
    }
}

Nginx PHP 配置模板

server {
    listen 80;
    root /var/www/www.example.com;
    # Add index.php to the list if you are using PHP
    index index.html index.htm index.php index.nginx-debian.html;

    server_name www.example.com;

    client_max_body_size 20m;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;

        # With php-fpm (or other unix sockets):
        fastcgi_pass unix:/run/php/php8.3-fpm.sock;
    }
}

wordpress之WP_Query函数

用法一、查询某一分类下的文章,代码如下:

<?php
$query = new WP_Query([
    'post_type' => 'product', // 文章类型是自定义类型 product 
    'posts_per_page' => 999, // 每页 999 条
    'order' => 'ASC', // 升序排序
    // 查询分类,即该自定义类型下的分类
    'tax_query' => [
        [
            // 分类的名字
            'taxonomy' => 'product_category',
            // 根据wp_terms表里的哪个字段进行查询,可以根据term_id或slug等
            'field' => 'term_id',
            // 上面字段的值
            'terms' => $sub->term_id,
        ]
    ],
]);

if ($query->have_posts()) :
    while ($query->have_posts()) :
        $query->the_post();
?>

        <a href="<?php the_permalink() ?>"><?php the_title() ?></a>

<?php
    endwhile;
endif;
// 使用 WP_Query 查询后,不要忘了进行重置查询,否则会影响后面代码的执行
wp_reset_query();
?>

wordpress之wp_list_categories函数

该函数用来输出一个某个分类法下的分类列表,输出的是html格式,默认是 <li>…</li>

$term = get_term_by('slug', 'software', 'product_category');
wp_list_categories([
    'taxonomy' => 'product_category', // 分类法
    'child_of' => $term->term_id, // 哪个分类的子分类
    'depth' => 0, 
    'hide_empty'=>false, // 如果该分类下没有文章,是否隐藏
    'title_li' => ''
] );

wordpress之the_xxx 系列的函数

该系列函数主要用于对单篇文章使用,获取单篇文章的相关信息,常用函数如下:

输出文章标题

<?php the_title() ?>

输出文章发布时间

<?php echo get_the_date( 'Y-m-d' ); ?>

输出文章简介,超出的部分用[…]来省略。

<?php the_excerpt(); ?>

输出文章的固定链接

<?php the_permalink() ?>

输出文章内容

<?php the_content() ?>

wordpress之sanitize_text_field函数

清理用户输入或者从数据库中获取的字符串。

检查是否合法的 UTF-8 字符,将 < 转换成 HTML 实体,去掉所有标签,删除所有换行符,占位符,以及额外的空格,去掉八位字节。

常用于后台meta box 保存内容时,但是使用了wp_editor就不能使用该函数进行过滤了,否则会导致生成的富文本格式不正确。

用法:

<?php sanitize_text_field($string); ?>

函数所在文件:wp-includes/formatting.php

参考:https://blog.wpjam.com/function_reference/sanitize_text_field/

wordpress函数之get_the_terms

该函数用于在已知文章ID和分类法的时候调用,常用于文章详情页面。用来获取分类信息

用法:

/** 
第一个参数表是当前文章的ID, 第二个参数表示所属的分类法,即taxonomy name。
在single页面时可以直接使用 $post->ID
**/
$terms = get_the_terms($post->ID, 'product_category');

wordpress函数之get_term_by

该函数用来获取一个分类的信息,实际是 wp_terms 和 wp_term_taxonomy 表的联合查询

第一个参数表示根据 wp_terms 表的哪个字段查询

第二个参数表示该字段的值

第三个参数表示分类法的名称,即 wp_term_taxonomy 表的 taxonomy

$term = get_term_by('slug', 'software', 'product_category');