月度归档: 2020 年 12 月

  • WordPress 插件开发 自定义文章类型 置顶

    WordPress 插件开发 自定义文章类型 置顶

    在WordPress中,原生是自带文章“在博客中置顶”这个功能选项的,但是我们在插件开发时如果自定义了文章的类型,那么这个按钮就无法显示,需要通过代码设置才能显示出来,下面我们记录一下具体的代码。

    当前所使用主题的 functions.php 文件中

    add_action( 'add_meta_boxes', 'jiloc_add_book_box' );
    function jiloc_add_book_box(){
    add_meta_box( 'jiloc_add_book_box', '自定义文章类型置顶', 'jiloc_sticky ', '自定义类型type', 'side', 'high' );
    }
    function jiloc_sticky (){ ?>
    <input id="super-sticky" name="sticky" type="checkbox" value="sticky" <?php checked( is_sticky() ); ?> /><label for="super-sticky" class="selectit">置顶本产品</label>
    <?php
    }

    效果如下

    主题中输出置顶文章代码

    $sticky = get_option( 'sticky_posts' );
    query_posts( array( 'post__in' => $sticky, 'ignore_sticky_posts' => 1 ) );

    主题中输出非置顶文章代码

    $sticky = get_option( 'sticky_posts' );
    query_posts(array('ignore_sticky_posts' => 1,'post__not_in' => $sticky) );

    不想把置顶文章和非置顶文章区分开来的话,直接把不输出置顶文章中的 ignore_sticky_posts 和 post__not_in 去掉即可正常输出,并且自动将置顶文章排在首位。

    如果是正常输出文章让置顶文章自动置顶,那么我们很有必要在置顶文章标题后面或前面添加【置顶】标识,以便用户知道这是一篇置顶文章。方法也很简单,只需要在循环语句中的标题后面添加以下代码即可:

    <?php if (is_sticky()) {?><span class="sticky-icon">置顶</span><?php } ?>
    

    意思就是判定当前文章是否为置顶文章,是的话就输出置顶标识。

  • jQuery 添加 required 必填属性      图文教程

    jQuery 添加 required 必填属性 图文教程

    现在大部分主流浏览器均已支持required属性,那么我们如何使用javascript添加required属性呢,下面我们记录一下jQuery的代码

    $('#id').attr("required", "true");
    

  • WordPress 插件开发 增加自定义页面类型及前端显示页面

    WordPress 插件开发 增加自定义页面类型及前端显示页面

    最近想开发一个新的插件用来监控VPS库存情况,下面我们记录一下一些代码的细节片段。

    这里写一下如何通过插件功能自定义某个页面的类型,前端显示时固定使用某个模板来显示。

    <?php
    /*
    Plugin Name: JiLoc Test
    */
    class PageTemplate {
    	/**
    	 * A reference to an instance of this class.
    	 */
    	private static $instance;
    	/**
    	 * The array of templates that this plugin tracks.
    	 */
    	protected $templates;
    	/**
    	 * Returns an instance of this class. 
    	 */
    	public static function get_instance() {
    		if ( null == self::$instance ) {
    			self::$instance = new PageTemplater();
    		} 
    		return self::$instance;
    	} 
    	/**
    	 * Initializes the plugin by setting filters and administration functions.
    	 */
    	private function __construct() {
    		$this->templates = array();
    
    		// Add a filter to the attributes metabox to inject template into the cache.
    		if ( version_compare( floatval( get_bloginfo( 'version' ) ), '4.7', '<' ) ) {
    			// 4.6 and older
    			add_filter(
    				'page_attributes_dropdown_pages_args',
    				array( $this, 'register_project_templates' )
    			);
    		} else {
    			// Add a filter to the wp 4.7 version attributes metabox
    			add_filter(
    				'theme_page_templates', array( $this, 'add_new_template' )
    			);
    		}
    		// Add a filter to the save post to inject out template into the page cache
    		add_filter(
    			'wp_insert_post_data', 
    			array( $this, 'register_project_templates' ) 
    		);
    
    		// Add a filter to the template include to determine if the page has our 
    		// template assigned and return it's path
    		add_filter(
    			'template_include', 
    			array( $this, 'view_project_template') 
    		);
    
    		// Add your templates to this array.
    		$this->templates = array(
    			'goodtobebad-template.php' => 'It\'s Good to Be Bad',
    		);
    			
    	} 
    	/**
    	 * Adds our template to the page dropdown for v4.7+
    	 *
    	 */
    	public function add_new_template( $posts_templates ) {
    		$posts_templates = array_merge( $posts_templates, $this->templates );
    		return $posts_templates;
    	}
    	/**
    	 * Adds our template to the pages cache in order to trick WordPress
    	 * into thinking the template file exists where it doens't really exist.
    	 */
    	public function register_project_templates( $atts ) {
    		// Create the key used for the themes cache
    		$cache_key = 'page_templates-' . md5( get_theme_root() . '/' . get_stylesheet() );
    		// Retrieve the cache list. 
    		// If it doesn't exist, or it's empty prepare an array
    		$templates = wp_get_theme()->get_page_templates();
    		if ( empty( $templates ) ) {
    			$templates = array();
    		} 
    		// New cache, therefore remove the old one
    		wp_cache_delete( $cache_key , 'themes');
    		// Now add our template to the list of templates by merging our templates
    		// with the existing templates array from the cache.
    		$templates = array_merge( $templates, $this->templates );
    		// Add the modified cache to allow WordPress to pick it up for listing
    		// available templates
    		wp_cache_add( $cache_key, $templates, 'themes', 1800 );
    		return $atts;
    	} 
    	/**
    	 * Checks if the template is assigned to the page
    	 */
    	public function view_project_template( $template ) {
    		
    		// Get global post
    		global $post;
    		// Return template if post is empty
    		if ( ! $post ) {
    			return $template;
    		}
    		// Return default template if we don't have a custom one defined
    		if ( ! isset( $this->templates[get_post_meta( 
    			$post->ID, '_wp_page_template', true 
    		)] ) ) {
    			return $template;
    		} 
    		$file = plugin_dir_path( __FILE__ ). get_post_meta( 
    			$post->ID, '_wp_page_template', true
    		);
    		// Just to be safe, we check if the file exist first
    		if ( file_exists( $file ) ) {
    			return $file;
    		} else {
    			echo $file;
    		}
    		// Return template
    		return $template;
    	}
    } 
    add_action( 'plugins_loaded', array( 'PageTemplate', 'get_instance' ) );

  • 配置docker desktop的境像下载地址为国内镜像地址

    配置docker desktop的境像下载地址为国内镜像地址

    右键右下角 docker的托盘图标,依次打开  Setings >> Docker Engine,在Docker Engine里配置如下内容,

    {
      "registry-mirrors": [
        "https://registry.docker-cn.com",
        "http://hub-mirror.c.163.com",
        "https://docker.mirrors.ustc.edu.cn"
      ],
      "insecure-registries": [],
      "debug": true,
      "experimental": false
    }

    然后点击右边的”Apply & Restart” 按钮保存与重新docker。

    Docker中国区官方镜像地址: https://registry.docker-cn.com

    网易地址: http://hub-mirror.c.163.com
    中国科技大学地址: https://docker.mirrors.ustc.edu.cn

  • Windows下wsl更新到wsl2适用于 Linux 的 Windows 子系统安装指南 (Windows 10)

    Windows下wsl更新到wsl2适用于 Linux 的 Windows 子系统安装指南 (Windows 10)

    刚好将本机的WSL升级到WSL2,下面给出官方的文档地址,适用于 Linux 的 Windows 子系统安装指南 (Windows 10)

    https://docs.microsoft.com/zh-cn/windows/wsl/install-win10

WeChat