首页 > 其他分享 >WordPress

WordPress

时间:2022-10-22 14:13:11浏览次数:39  
标签:function title hr filter add WordPress action

主题目录结构

image-20220925150241946

  • assets 存放img css js font

  • front-page 主页

    • get_header() get_footer() 调用头部与脚部
  • header 头部

  • footer 脚部

  • functions 函数文件

  • single 单个文章页面

  • template-parts 拆分single文件的,区分文章类型(图片,文字,视频)

  • comments 文章评论页面

  • archive 存档页面(归档)

设置引入的css和js

get_stylesheet_directory_uri() 
get_template_directory_uri()

image-20220925082700339

<?php wp_head() ?>
<?php wp_footer() ?>

functions.php

<?php
//  wp_head()
function followandrew_regsiter_styles() {
    wp_enqueue_style('followandrew',get_template_directory_uri()."/style.css", array(), '1.0', 'all');
    wp_enqueue_style('followandrew-bootstrap',"https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css", array(), '4.4.1', 'all');
    wp_enqueue_style('followandrew-font-awesome',"https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.13.0/css/all.min.css", array(), '5.13.0', 'all');
}
add_action('wp_enqueue_scripts', 'followandrew_regsiter_styles');   

// true代表放在文件底部,一般js放在底部
// wp_footer()
function followandrew_regsiter_scripts() {
    wp_enqueue_script('followandrew-jquery',"https://code.jquery.com/jquery-3.4.1.slim.min.js", array(), '3.4.1', true);
    wp_enqueue_script('followandrew-popper',"https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js", array(), '1.16.0', true);
    wp_enqueue_script('followandrew-bootstrap',"https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js", array(), '4.4.1', true);
    wp_enqueue_script('followandrew-main',get_template_directory_uri()."/assets/js/main.js", array(), '1.1.1', true);
}
add_action('wp_enqueue_scripts', 'followandrew_regsiter_scripts');   

获取header.php 文件 头部文件

<?php get_header() ?>

获取footer.php

<?php get_footer() ?>

设置网站标题

functions.php

function followandrew_theme_support() {
    add_theme_support('title-tag');
}
add_action('after_setup_theme', 'followandrew_theme_support');

image-20220925091220662

image-20220925091235553

设置默认静态页面

新建页面

image-20220925094456708

设置页面

image-20220925094608910

<?php get_header() ?>

    <article class="content px-3 py-5 p-md-5">
        <?php 
            // 判断有没有页面
            if(have_posts()) {
                while (have_posts()) {
                    // 获取到页面
                    the_post(); 
                    // 获取到页面标题
                    the_title();
                    // 获取到页面内容
                    the_content();
                }
            }
        ?>
    </article>
<?php get_footer() ?>

动态添加nav&menu

functions.php

function followandrew_menus() {
    $lcation = [
        'primary' => 'primary-test',
        'footer' => 'footer-test',
    ];
    register_nav_menus($lcation);
};
add_action('init','followandrew_menus');

写完上面代码,后台外观会出现菜单选项

image-20220925110135479

我们在这里面新建菜单

image-20220925110455914

将菜单渲染到页面上


<?php 
    wp_nav_menu(
    [
        'menu' => 'primary',
        'container' => '',
        'theme_location' => 'primary',
        // 外层包裹的标签
        'items_wrap' => '<ul class="navbar-nav flex-column text-sm-center text-md-left">%3$s</ul>'
    ]
 
    ?>

添加网站logo

functions.php

function followandrew_theme_support() {
    add_theme_support('title-tag');
    add_theme_support('custom-logo');
}
add_action('after_setup_theme', 'followandrew_theme_support');

image-20220925140418045

image-20220925140438569

image-20220925140457845

<?php 
    if(function_exists('get_custom_logo')) {
        $custom_logo_id =  get_theme_mod('custom_logo');
        $logo = wp_get_attachment_image_src($custom_logo_id);
    }
?>    

<img class="mb-3 mx-auto logo" src="<?php echo $logo[0] ?>" alt="logo" >

网站name(title)

外观->主题->自定义->站点身份->站点标题

<?php echo get_bloginfo('name') ?>

文章支持图片-缩略图

写文章页面默认是没有插入图片的

image-20220925145603667

functions.php

function followandrew_theme_support() {
    // 标题
    add_theme_support('title-tag');
    // 网站logo
    add_theme_support('custom-logo');
    // 支持图片缩略图
    add_theme_support('post-thumbnails');
}
add_action('after_setup_theme', 'followandrew_theme_support');

可以看到已经有选择图片的

image-20220925145802635

文章

image-20220925152923957

此时会发现浏览器是空白的,那是我们还没有编写single的代码

single.php

<!--  文章内容 -->
<?php get_header() ?>
<article class="content px-3 py-5 p-md-5">
    <?php 
    // 判断有没有页面
    if(have_posts()) {
        while (have_posts()) {
            // 获取到页面
            the_post(); 
            get_template_part('template-parts/content', 'article');
        }
    }
    ?>
</article>
<?php get_footer() ?>

template-ports/content-article.php

<div class="container">
    <header class="content-header">
        <div class="meta mb-3">
            <span class="date">Published 3 months ago</span>
            <span class="tag"><i class='fa fa-tag'></i> tag</span>
            <span class="tag"><i class='fa fa-tag'></i> category</span>
            <span class="comment"><a href="#comments"><i class='fa fa-comment'></i> 3 comments</a></span>
        </div>
    </header>
    <?php 
    the_content();
    ?>
</div>

文章发布时间

跟PHP语法相似,可以传入参数

the_date();
the_date('D'); // 周几
the_date('Y.m.d'); // 2022.09.25

文章标签

1.直接获取

<span class="tag"><i class='fa fa-tag'></i><?php the_tags() ?> </span>

image-20220925154630672

2.分离每个标签

<?php 
the_tags("<span class='tag'><i class='fa fa-tag'></i>","</span><span class='tag'><i class='fa fa-tag'></i>","</span>"); 
?>

image-20220925155103569

分类

the_category();

评论数量

comments_number()

文章评论模板

获取comments.php 文件模板

在template-parts文件下

<?php 
   comments_template();
?>

评论列表

<?php
    wp_list_comments(
    [
        'avater_size' => 120, //头像大小
        'style' => 'div'
    ]
);
?>

评论文章的表格控件

<?php
    // 检查当前文章是否允许评论 
    if(comments_open()){
        comment_form([
            'class_form' => '',
            'title_reply_before' => '<h2 id="reply-title" class="comment-reply-title">',
            'title_reply_after' => '</h2>'
        ]);
    }
?>

插件

插件的创建及使用

插件的目录及入口文件

wp-content/plugin

定义一个插件要在插件首部定义插件的信息,我们的后台才能识别到这是一个插件

<?php
/*
Plugin Name: copyright
Description: this is copyright plugin
Version:  1.0
Author: code_haoran
Author URI: http://codehaoran.top
*/

插件的启用函数

// 定义插件启动时候调用的方法
register_activation_hook(__FILE__,'copyright_install');
function copyright_install() {
  // 插件启动,添加一个默认的版权信息
  update_option("copyright_text","<p style='color: red'>本站点所有文章均为原创,转载请注明出处</p>");
}

插件的停用函数

// 定义插件停用时候调用的方法
register_deactivation_hook(__FILE__,'copyright_deactivate');
function copyright_deactivate() {
  // 设置一个copyright_deactivate_text选项为yes
  update_option("copyright_deactivate_text","yes");
}

插件删除后执行的操作

在同目录下创建uninstall.php文件

<?php
// 如果uninstall不是从WordPress中调用的 则退出
if (!defined('WP_UNINSTALL_PLUGIN')) {
  exit();
}

// 删除插件创建的项目
delete_option('copyright_text');
delete_option('copyright_deactivate_text');

钩子:

动作(add_action与do_action)

function wp_head() {   
    do_action('wp_head');   
}  

do_action函数:do_action执行了一个“动作”,这个动作名称就是这个参数"wp_head",这个“wp_head动作”是不需要定义的,do_action含有自动定义的功能。也就是不需要事先定义这个参数wp_head是什么、要干嘛,而do_action就会自动定义一个名叫wp_head的动作,并执行它。

add_action()函数可以给“动作”添加具体事务,比如我们已经定义并执行了一个动作“饭前”,然后我们add_action('饭前 ','洗手'),就给这个“饭前”动作添加了具体内容,在程序中来讲,“洗手”这个参数应该是个函数名称,也就是‘饭前’动作要执行这个函数。

do_action里面的动作——'wp_head',叫做动作钩子(hook),钩子可以挂东西,所以add_action就是往这个钩子上挂函数,然后执行那个钩子的时候,也就是执行所有挂在这个钩子上的函数!

函数:

<?php      

    do_action($tag, $arg);      

    //$tag  必需、你想创建的动作钩子的名称      

    //$arg  传递给已有的动作钩子的值      

?>  

<?php      

    add_action( $tag, $function_to_add, $priority, $accepted_args );      

    //$tag  参数必需,动作钩子的名称      

    //$function_to_add  参数为必需,要执行的函数      

    //$priority  参数为int(整数),数字越小越先调用      

    //$accepted_args  参数为int(整数),函数需要的参数个数,默认1;自己理解:就是$function_to_add函数需要的参数个数      

 ?>

动作(加载过程)

init

// 在输出内容之前,给页面管理添加摘要功能
add_action('init','add_excerpts_to_pages');
function add_excerpts_to_pages() {
  // 给页面管理添加摘要功能
  add_post_type_support('page',array('excerpt'));
}

image-20220928104440306

wp_enqueue_scripts

​ 添加引用文件的

​ wp_enqueue_script( $handle, $src, $deps, $ver, $in_footer );

  • $handle – 调用的脚本名称,用于区别其它js,因此不能和其它js文件命名相同。

  • $src – js文件的路径(即url),不要直接使用域名url,要使用路径函数,如parent theme使用get_template_directory_uri,child theme使用get_stylesheet_directory_uri。(WP模板路径相关的函数中,通常带有template的是指parent theme,带有stylesheet的指向child theme)

  • $deps – 依赖关系,加载的js文件所依存的其它js的标识字串数组(array:string),即需要在本代码之前加载的代码的名称(如js脚本依赖jquery库,那么这里要用数组的形式写上jquery),非必需。

  • $ver – 加载js文件的版本号,作为查询字串附加在路径的末尾,作用是确保正确的版本信息传递给了客户端,以免受到缓存的影响(如js脚本发生变化时,通过更改版本号可以强制客户浏览器更新缓存),默认为false,调用当前wordpress程序的版本号,如果不想显示,则设置为NULL(不推荐)。

  • $in_footer – boolean类型,设置js文件调用代码是否放置在html底部,设置为ture则放在底部,设置为false则放置在head部分。提示需要模板正确放置wp_footer()函数。

    使用方法

    通过wp_enqueue_script函数加载js文件时,应该将它分配给一个钩子,如只在前台调用,使用钩子wp_enqueue_script;只在后台调用,使用admin_enqueue_scripts。

    //自定义引用样式表
    function copyright_enqueue_styles() {
      wp_enqueue_style('core',plugins_url('css/style.css'),false);
    }
    // 自定义引用脚本文件
    function copyright_enqueue_scripts() {
      wp_enqueue_script('my-js',plugins_url('js/script.css',__FILE__),false);
    }
    
    add_action('wp_enqueue_scripts','copyright_enqueue_styles');
    add_action('wp_enqueue_scripts','copyright_enqueue_scripts');
    
    

动作(文章)

save_post

文章保存或更新的时候触发

wp_trash_post

文章被放入回收站时触发

delete_post

删除文章时触发

移除动作

remove_action()

remove_all_actions()

过滤器

apply_filters

$value = '123456';
$myvar = apply_filters('hr_filter', $value);
echo $myvar; // 123456

add_filter

add_filter( string $hook_name, callable $callback, int $priority = 10, int $accepted_args = 1 )

过滤器挂载方法

$value = '123456';
add_filter('hr_filter', 'filter_fun');
function filter_fun($val) {
  return $val . '----filter';
}
$myvar = apply_filters('hr_filter', $value);
echo $myvar; // 123456----filter

可以给过滤器挂载多个方法

$value = '123456';
add_filter('hr_filter', 'filter_fun');
add_filter('hr_filter', 'filter_fun_add_time');
function filter_fun($val) {
  return $val . '----filter';
}
function filter_fun_add_time($val) {
  return $val . '---' . date('Y-m-d');
}
$myvar = apply_filters('hr_filter', $value);
echo $myvar; // 123456----filter---2022-10-01

也可以给WordPress方法添加过滤方法

function footer_filter_fun($value) {
  return date('Y-m-d') . $value;
}
add_filter('the_content','footer_filter_fun');

带参数,给title添加过滤器

function title_filter_if_hr($title, $id = null) {
  // 如果文章是hr分类下的
  if(in_category('hr', $id)) {
    return 'hr分类__:'. $title;
  }
  // 否则
  return $title;
}
add_filter('the_title', 'title_filter_if_hr', 10, 2);

在文章保存之前过滤

// 在文章保存之前过滤
function hr_auto_link($conten) {
  return str_replace('浩然', "<a href='baidu.com'>浩然</a>",$conten);
}
add_filter('content_save_pre','hr_auto_link');

后台整合:

创建顶级菜单

add_menu_page(page_title, menu_title, capability, menu_slug, function, icon_url, position);

  • page_title 页面的title,和显示在</strong>标签里的一样</li> <li>menu_title 在控制面板中显示的名称</li> <li>capability 要引用该菜单所要的最低权限</li> <li>menu_slug 要引用该菜单的别名,必须是唯一的</li> <li>function 要显示菜单对应的页面内容所调用的函数</li> <li>icon_url 菜单的icon图片的URL</li> <li>position 出现在菜单列表中的次序</li> </ul> <p>要在admin_menu钩子中使用</p> <pre><code class="language-php">function create_banner_menu() { add_menu_page( 'banner', 'banner', 'manage_options', 'hr_banner', 'hr_settings_page', plugins_url('/images/icon.png', __FILE__) ); } add_action('admin_menu', 'create_banner_menu'); function hr_settings_page() { } </code></pre> <h3 id="创建子菜单">创建子菜单</h3> <blockquote> <p>add_submenu_page(parent_slug, page_title, menu_title, capability, menu_slug, function);</p> </blockquote> <ul> <li>parent_slug 父级别名</li> </ul> <pre><code class="language-php">function create_banner_menu() { add_menu_page( 'banner', 'banner', 'manage_options', 'hr_banner', 'hr_settings_page', plugins_url('/images/icon.png', __FILE__) ); add_submenu_page( 'hr_banner', 'sub_banner', 'sub_banner', 'manage_options', 'hr_sub_banner', 'hr_settings_page_sub' ); } add_action('admin_menu', 'create_banner_menu'); function hr_settings_page() { echo '<h2>banner</h2>'; } function hr_settings_page_sub() { echo '<h2>sub_banner</h2>'; } </code></pre> <h3 id="创建小工具">创建小工具</h3> <p><a href="https://codex.wordpress.org/zh-cn:%E5%B0%8F%E5%B7%A5%E5%85%B7%E6%8E%A5%E5%8F%A3" target="_blank">https://codex.wordpress.org/zh-cn:小工具接口</a></p>

标签:function,title,hr,filter,add,WordPress,action
From: https://www.cnblogs.com/codehaoran/p/16816004.html

相关文章

  • WordPress彻底禁用上传媒体图片自动生成缩略图及多尺寸图片(亲测可用)
    WordPress默认上传图片的时候会自动生成缩略图及多尺寸的图片文件,大部分网站都用不到这些多余的图片,不仅仅占用空间,而且上传的时候还会消耗额外的性能。下面仅需两段函数......
  • WordPress 优化 MySQL 数据库慢查询
    搭建WordPress网站会占用大量的内存,还需要一些好些的服务器配置之外,我们如果想更好的给用户有更好的访问体验,对网站优化也是必不可少的。很多时候用WordPress越久,数据......
  • 记录|在WordPress设立HTTPS
    TL;DR:根据HTTPSforWordPress–WordPress.orgForums上的指示操作即可。获取HTTPS证书到Certbot上去,填MyHTTPwebsiteisrunning[Nginx]on[Ubuntu20]......
  • WordPress自定义仪表盘
    •介绍本文介绍如果自定义一个仪表盘。[codesyntaxlang="php"]add_action('wp_dashboard_setup','suren_welcome_panel');functionsuren_welcome_panel(){wp_......
  • WordPress自定义字段
    介绍本文介绍如何添加、使用WordPress文章中的自定义字段。玉照[captionid="attachment_3482"align="aligncenter"width="923"]​​​​WordPress文章自定义字段[/capt......
  • WordPress给附件添加属性
    •介绍WordPress的附件(多媒体)默认已经有一些对应的属性了,我们还可以再添加一些信息。•玉照[captionid="attachment_3523"align="aligncenter"width="399"]​​​​w......
  • WordPress在插件管理页面添加超链接
    •介绍如果你创建了一个插件,WordPress的插件管理页面中就可以看到,而且会有启用、停用、编辑等默认的超级链接按钮。那么,怎么才能添加一个自定义的呢?•玉照[captionid="a......
  • WordPress修订版本
    介绍WordPress会自动维护文章(post)的修订版本,就类似SVN这样的版本管理功能,这本来是一件很惊艳的事情。可是,默认情况下修订版本的数量是没有限制的,这样会导致数据库中有大量无......
  • 单机安装基于LNMP结构的WordPress网站
    单机安装基于LNMP结构的WordPress网站基本环境准备配置nginx配置数据库服务器部署wordpressweb与数据库服务分离准备数据库服务器单机安装基于LNMP结构的WordPress网......
  • wordpress 开发相关函数
    首页判断is_home()is_front_page()当你的首页不是默认的index.php的时候,而是在后台指定了一个page页面。这种情况下is_home()会失效,也就是说这样子的情况下就不能再用is......