4zhvml8 发表于 2024-10-6 12:40:41

真香,30天做一套wordpress主题(第2天):吃透主题设置


    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">欢迎来到《真香,30天做一套wordpress主题》系列<span style="color: black;">文案</span>,<span style="color: black;">咱们</span>的<span style="color: black;">目的</span>是(<span style="color: black;">无</span>蛀牙!)<span style="color: black;">创立</span>一套全新的wordpress主题,花上30天的时间闭关修炼,<span style="color: black;">倘若</span>你看到的<span style="color: black;">第1</span>篇<span style="color: black;">文案</span>不是《<span style="color: black;">基本</span>框架搭建》,<span style="color: black;">意见</span>你关注<span style="color: black;">咱们</span>(<strong style="color: blue;">数字江湖异志录</strong>),从该系列的<span style="color: black;">第1</span>篇<span style="color: black;">起始</span>阅读。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">咱们</span>将<span style="color: black;">尽可能</span>保持<span style="color: black;">文案</span>的<strong style="color: blue;">循序渐进</strong>和<strong style="color: blue;">通俗易懂</strong>,请确保自己<span style="color: black;">已然</span><span style="color: black;">把握</span>了那一篇<span style="color: black;">文案</span>的<span style="color: black;">所有</span>内容时才<span style="color: black;">选取</span>跳过,<span style="color: black;">否则</span>可能会<span style="color: black;">错失</span>关键的信息噢~</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">这儿</span><span style="color: black;">咱们</span>假定你<span style="color: black;">已然</span>知晓了以下<strong style="color: blue;"><span style="color: black;">基本</span>知识</strong>,这些<span style="color: black;">基本</span>知识对理解<span style="color: black;">文案</span>内容是至关重要的:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1. HTML/CSS/JS<span style="color: black;">基本</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2. PHP<span style="color: black;">基本</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">3. <span style="color: black;">怎样</span><span style="color: black;">运用</span>Wordpress</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">4. <span style="color: black;">怎样</span>搭建web环境</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">倘若</span>你<span style="color: black;">已然</span>知晓了以上<span style="color: black;">基本</span>知识,恭喜你,本系列的任何<span style="color: black;">文案</span>内容对你而言都<span style="color: black;">无</span>什么难度。</p>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">直奔主题</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">新版的wordpress<span style="color: black;">增多</span>了自定义主题设置的功能,并且官方<span style="color: black;">亦</span><span style="color: black;">举荐</span><span style="color: black;">研发</span>者把<span style="color: black;">关联</span>的设置放到主题自定义里。默认的<span style="color: black;">状况</span>下,主题会自动开启<span style="color: black;">有些</span>自定义参数(即使<span style="color: black;">咱们</span>的主题还<span style="color: black;">无</span><span style="color: black;">运用</span>它们):</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/7ebdb1acf907450fa7b364e265e3f15f~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1728363870&amp;x-signature=GCmjY8p5c8leTPzu8MP4T8eCTIw%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">咱们</span>在主题文件夹下<span style="color: black;">创立</span>一个<strong style="color: blue;">functions.php</strong>文件,这个文件会影响到主题的方方面面,<span style="color: black;">首要</span><span style="color: black;">咱们</span><span style="color: black;">创立</span>自定义的主题参数连接:</p>// Customizer

    function my_customize_register($wp_customize) {

    };

    add_action( customize_register, my_customize_register );<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">关联</span>的主题设定<span style="color: black;">咱们</span>都会写到my_customize_register里,<span style="color: black;">咱们</span>先来<span style="color: black;">认识</span>三个基本概念:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1. Section</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2. Settings</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">3. Control</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">为了方便理解,<span style="color: black;">咱们</span><span style="color: black;">能够</span>把section当成设置的一级菜单,control是菜单里的设置控件,settings则是存储设置的容器。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">经过</span>查阅wordpress文档,<span style="color: black;">咱们</span>找出了<span style="color: black;">哪些</span>默认开启的section的<span style="color: black;">独一</span>id,<span style="color: black;">而后</span><span style="color: black;">咱们</span>在代码里把它们去除:</p>// Customizer

    function my_customize_register($wp_customize) {

    // remove default section

    $wp_customize-&gt;remove_section(title_tagline);

    $wp_customize-&gt;remove_section(custom_css);

    $wp_customize-&gt;remove_section(static_front_page);

    };<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">而后</span><span style="color: black;">咱们</span>添加自己的section、control、settings,<span style="color: black;">这儿</span><span style="color: black;">咱们</span>先盘点一下<span style="color: black;">日前</span>的<span style="color: black;">需要</span>,<span style="color: black;">咱们</span>需要对页面的公共顶部进行设置,公共顶部含有以下元素:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1. 博客标题</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2. 主标语(slogan/tagline)</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">3. 副标语</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">那样</span><span style="color: black;">咱们</span>在functions.php里实现代码是像<span style="color: black;">这般</span>的:</p>$wp_customize-&gt;add_section( header_setting , array(

    title =&gt; __( Header Setting),

    priority =&gt; 10,

    ) );

    $wp_customize-&gt;add_setting( blog_title , array(

    default =&gt; get_bloginfo(name),

    transport =&gt; refresh,

    ) );

    $wp_customize-&gt;add_setting( main_tagline , array(

    default =&gt; Free the Internet,

    transport =&gt; refresh,

    ) );

    $wp_customize-&gt;add_setting( sub_tagline , array(

    default =&gt; Across the Great Wall we can reach every corner in the world,

    transport =&gt; refresh,

    ) );



    $wp_customize-&gt;add_control(

    input_blog_title,

    array(

    label =&gt; __( Blog Title),

    section =&gt; header_setting,

    settings =&gt; blog_title,

    type =&gt; text,

    )

    );

    $wp_customize-&gt;add_control(

    input_main_tagline,

    array(

    label =&gt; __( Main tagline),

    section =&gt; header_setting,

    settings =&gt; main_tagline,

    type =&gt; text,

    )

    );

    $wp_customize-&gt;add_control(

    input_sub_tagline,

    array(

    label =&gt; __( Sub tagline),

    section =&gt; header_setting,

    settings =&gt; sub_tagline,

    type =&gt; textarea,

    )

    );<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">这儿</span><span style="color: black;">咱们</span>用到了三个<span style="color: black;">办法</span>:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1. add_section</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2. add_settings</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">3. add_control</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">对应section、settings、control的添加,需要<span style="color: black;">重视</span>的是,<span style="color: black;">咱们</span>在设置文本的title输出那里用到了魔术<span style="color: black;">办法</span><strong style="color: blue;">__()</strong>,这是为后期主题的国际化做准备,魔术<span style="color: black;">办法</span>__()和魔术<span style="color: black;">办法</span>_e()都是为了主题多语言而存在的,它们之间的<span style="color: black;">区别</span><span style="color: black;">便是</span>__()返回字符串,而_e()直接把文本在页面里输出,当然,它们都是<span style="color: black;">按照</span>当前系统设定的语言找到对应的语言包输出的,主题国际化的<span style="color: black;">关联</span>内容<span style="color: black;">咱们</span>在本系列的后期进行展开。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">这般</span>设定之后,<span style="color: black;">咱们</span>就<span style="color: black;">能够</span>在主题的任意位置调用这些设置好的内容,<span style="color: black;">此刻</span><span style="color: black;">咱们</span>把它们在home.php里<span style="color: black;">表示</span>出来<span style="color: black;">瞧瞧</span>。</p>&lt;body&gt;

    &lt;header&gt;

    &lt;?php echo get_theme_mod(blog_title) ?&gt;

    &lt;?php echo get_theme_mod(main_tagline, Free the Internet) ?&gt;

    &lt;?php echo get_theme_mod(sub_tagline, Across the Great Wall we can reach every corner in the world) ?&gt;

    Header Area

    &lt;/header&gt;

    &lt;/body&gt;<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">这儿</span><span style="color: black;">咱们</span>用到的内置<span style="color: black;">办法</span>是get_theme_mod,<span style="color: black;">咱们</span>传两个参数给它,一个是主题设置的settings id,一个是默认值。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">还记得<span style="color: black;">咱们</span>在第1天<span style="color: black;">创立</span>框架时有添加的style amp-custom吗,<span style="color: black;">咱们</span>在里面添加一段css,让<span style="color: black;">咱们</span>的header凸显出来:</p>&lt;style amp-custom&gt;

    header {

    width: 100vw;

    height: 18.5185vw;

    bac<span style="color: black;">公斤</span>round: gray;

    }

    &lt;/style&gt;<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">刷新页面,<span style="color: black;">咱们</span>应该能看到<span style="color: black;">这般</span>的页面输出:</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p26-sign.toutiaoimg.com/pgc-image/f569aea6c35c4a34affb6427866a9ee8~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1728363870&amp;x-signature=uCeGAUqfFEgkAw7gCVoR0HhKuL8%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">这儿</span>,Hhackers blog是我的blog名<span style="color: black;">叫作</span>,其它的<span style="color: black;">便是</span>刚才<span style="color: black;">咱们</span>设定的主标语和副标语的默认值了。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">咱们</span>在主题自定义那里设定这些settings并点击发布时,页面里对应的部分就会发生变化了。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">接下来<span style="color: black;">咱们</span>顺便把主题的favicon参数<span style="color: black;">亦</span>实现了吧,favicon是<span style="color: black;">表示</span>在浏览器标签页上的小图标,<span style="color: black;">咱们</span><span style="color: black;">根据</span>之前的<span style="color: black;">办法</span>,<span style="color: black;">创立</span>一套settings和control,并绑定到<span style="color: black;">咱们</span>的section里:</p>$wp_customize-&gt;add_setting( favicon , array(

    default =&gt; ,

    transport =&gt; refresh,

    sanitize_callback =&gt; absint,

    ) );

    $wp_customize-&gt;add_control( new WP_Customize_Site_Icon_Control( $wp_customize, set_favicon,

    array(

    label =&gt; __( Favicon ),

    description =&gt; __( Favicon is what you see in &lt;strong&gt;browser tabs&lt;/strong&gt;, bookmark bars),

    section =&gt; header_setting,

    settings =&gt; favicon,

    width =&gt; 32,

    height =&gt; 32,

    )

    ) );<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">这儿</span><span style="color: black;">咱们</span>直接<span style="color: black;">运用</span>了wordpress的内置控件</p>WP_Customize_Site_Icon_Control,这个控件<span style="color: black;">能够</span>直接对<span style="color: black;">照片</span>进行上传和裁剪。

    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">而后</span><span style="color: black;">咱们</span>在header.php公共头里<span style="color: black;">运用</span><span style="color: black;">运用</span>这个favicon:</p>&lt;?php

    if ($icon = wp_get_attachment_url(get_theme_mod(favicon))) {

    echo &lt;link rel="shortcut icon" href=".$icon."&gt;;

    }

    ?&gt;<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">这儿</span>需要<span style="color: black;">重视</span>的是<span style="color: black;">咱们</span>直接get_theme_mod取到的内容是一个数字id,<span style="color: black;">咱们</span>需要<span style="color: black;">运用</span>wp_get_attachment_url获取<span style="color: black;">照片</span>id对应的url路径。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">这般</span><span style="color: black;">咱们</span>刷新页面 ,就能在标签页上看到<span style="color: black;">咱们</span>设置好的的favicon了。</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/fb820f98f41c4cd88f2521b261e0ef87~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1728363870&amp;x-signature=pl7HzzlD0US8zlh2tf39aGAS1N8%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">总结和预告</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">今天<span style="color: black;">咱们</span>定义了一套主题的设置选项,之后<span style="color: black;">咱们</span>会把所有跟主题<span style="color: black;">关联</span>的设置统统放到里面,wordpress的主题自定义设置设计得非常优雅,相<span style="color: black;">针对</span>以往的<span style="color: black;">创立</span>专门的主题设置页面而言,在自定义设置里添加相应的<span style="color: black;">掌控</span>选项非常简单和直接,让<span style="color: black;">咱们</span><span style="color: black;">能够</span>对主题设定进行统一管理。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">明天(第3天)<span style="color: black;">咱们</span>将真正将这一套公共顶部搭建起来,并且实现页面在PC、平板、手机上的响应式布局。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">倘若</span>你<span style="color: black;">爱好</span>这个系列的<span style="color: black;">文案</span>,赶快关注<span style="color: black;">咱们</span>吧,不要<span style="color: black;">错失</span>后续的<span style="color: black;">更加多</span>干货噢。</p>




qzmjef 发表于 2024-10-19 05:19:34

可以发布外链的网站 http://www.fok120.com/
页: [1]
查看完整版本: 真香,30天做一套wordpress主题(第2天):吃透主题设置