作者: admin

  • 预览wordpress官方主题的方法

    https://2025.wordpress.net

    修改前面的数字,就可以查看对应的主题

  • acme.sh 用法

    安装 acme.sh

    curl https://get.acme.sh | sh -s email=your-email@outlook.com

    设置别名,方便使用:

    alias acme.sh=~/.acme.sh/acme.sh

    申请证书:

    acme.sh --issue -d www.lwbj.cn  --nginx

    acme.sh --issue -d www.lwbj.cn  --webroot /var/www/www.lwbj.cn

    部署证书:

    acme.sh --install-cert -d www.lwbj.cn --key-file /etc/nginx/ssl/www.lwbj.cn.key --fullchain-file /etc/nginx/ssl/www.lwbj.cn.pem --reloadcmd "service nginx force-reload"

    删除证书:

    acme.sh --remove -d www.lwbj.cn

    删除后记得到 /root/.acme.sh/ 目录下找到域名文件夹,一并删除。

    官方文档:https://github.com/acmesh-official/acme.sh/wiki/%E8%AF%B4%E6%98%8E

  • 卸载xcode命令行工具

    如果需要卸载xcode命令行工具,可以执行

    sudo rm -rf /Library/Developer/CommandLineTools

    接下来,运行以下命令来清理 xcode-select 的路径设置:

    sudo xcode-select --reset

    如果你还希望彻底删除与 Xcode 相关的文件,可以运行:

    sudo rm -rf /Applications/Xcode.app

    最后,验证命令行工具是否已成功删除:

    xcode-select -p

    如果没有输出或输出的是一个不存在的路径,则表示命令行工具已成功删除。

  • MacBook安装Homebrew

    执行下面的命令以安装homebrew:

    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

    在安装homebrew之前,需要先安装xcode命令行工具,上面的安装脚本会进行检查,如果没安装xcode命令行工具,会自动进行安装。

    如果需要手动安装xcode命令行工具,可以执行:

    xcode-select --install
  • WordPress自定义文章类型,在管理列表页显示分类名

    只需要在register_taxonomy函数的第三个参数中,添加show_admin_column=>true即可。代码如下:

    <?php
    
    /**
     * 为产品 post type 添加分类功能
     */
    add_action('init', 'my_taxonomies_product', 0);
    function my_taxonomies_product() {
        $labels = array(
            'name'              => _x('产品分类', 'taxonomy 名称'),
            'singular_name'     => _x('产品分类', 'taxonomy 单数名称'),
            'search_items'      => __('搜索产品分类'),
            'all_items'         => __('所有产品分类'),
            'parent_item'       => __('该产品分类的上级分类'),
            'parent_item_colon' => __('该产品分类的上级分类:'),
            'edit_item'         => __('编辑产品分类'),
            'update_item'       => __('更新产品分类'),
            'add_new_item'      => __('添加新的产品分类'),
            'new_item_name'     => __('新产品分类'),
            'menu_name'         => __('产品分类'),
        );
        $args = array(
            'labels' => $labels,
            'hierarchical' => true,
            'show_admin_column' => true,
        );
        register_taxonomy('product_category', 'product', $args);
    }
    

    如果要在列表中添加自定义字段,即wp_postmeta表中的字段(通过meta_box添加的),可以使用下面的代码:

    <?php
    
    // 在列表中把加的字段显示出来
    add_action("manage_posts_custom_column",  "product_custom_columns");
    function product_custom_columns($column) {
        global $post;
        switch ($column) {
            case "product_director":
                echo get_post_meta($post->ID, '_product_director', true);
                break;
        }
    }
    
    add_filter("manage_edit-product_columns", "movie_edit_columns");
    function movie_edit_columns($columns) {
        $columns['product_director'] = '产品分类';
        return $columns;
    }
    
  • WordPress在single页面获取自定发布类型的分类

    在详情页面,即single页面获取自定义发布类型的分类

    <?php
    
    /** 第一个参数表是当前文章的ID, 第二个参数表示所属的分类法,即taxonomy name。在single页面时可以直接使用 $post->ID **/
    $terms = get_the_terms($post->ID, 'product_category');
    if ($terms && !is_wp_error($terms)) :
        $terms_name_arr = array();
        foreach ($terms as $term) {
            $terms_name_arr[] = $term->name;
        }
        $terms_name_str = join(" ", $terms_name_arr);
    endif;
    echo $terms_name_str;
  • WordPress在single页面获取当前发布类型的名字

    <?php
    $postType = get_post_type_object(get_post_type($post));
    if ($postType) {
        echo esc_html($postType->labels->singular_name);
    }

  • WordPress在archive.php页面获取当前分类信息

    代码如下:

    echo get_queried_object()->term_id;

    或者直接使用:

    $term_id = get_queried_object_id();

    获取分类的其它信息

    $tax = $wp_query->get_queried_object();
    echo $tax->name; // wp_terms表的name字段
  • WordPress在archive.php页面获取文章列表

    除了使用WP_Query函数之外,还可以使用query_posts函数来查询,如下:

    <?php
    query_posts(array(
        'post_type'   => 'case', // You can add a custom post type if you like
        'paged'     => get_query_var('paged', 1),
        'posts_per_page' => 1
    ));
    
    while (have_posts()) :
        the_post();
    ?>
        <a href="<?php the_permalink() ?>" class="col-xs-12 col-sm-4 col-md-4 col-lg-4 block-four-list">
            <div class="block-four-list-wrap">
                <img src="<?php echo catch_that_image(); ?>" alt="">
                <div class="infos-right-view2 about-a-infos">
                    <h5>成功案例</h5>
                    <h1><?php the_title() ?></h1>
                    <span class="a-more-4">了解更多<i class="iconfont icon-youjiantou6"></i></span>
                </div>
            </div>
        </a>
    
    
    <?php
    endwhile;
    
    ?>
  • WordPress修改默认发布类型(post)的一些参数

    <?php
    add_action('init', 'cp_change_post_object');
    // Change dashboard Posts to News
    function cp_change_post_object() {
        $get_post_type = get_post_type_object('post');
        $labels = $get_post_type->labels;
        //$labels->name = 'News';
        $labels->singular_name = '新闻资讯';
        // $labels->add_new = 'Add News';
        // $labels->add_new_item = 'Add News';
        // $labels->edit_item = 'Edit News';
        // $labels->new_item = 'News';
        // $labels->view_item = 'View News';
        // $labels->search_items = 'Search News';
        // $labels->not_found = 'No News found';
        // $labels->not_found_in_trash = 'No News found in Trash';
        // $labels->all_items = 'All News';
        // $labels->menu_name = 'News';
        // $labels->name_admin_bar = 'News';
    }