一,插件制作
1.首先在plugin文件夹下创建一个php文件,我以制作一个banner插件为例,把以下代码拷贝到php文件中
<?php add_action("init",function(){ register_post_type("banner",[ "label"=>"banner", //后台显示标签 "public"=>true, ]); }); ?>
2.在需要使用插件的地方调用插件
<?php $Query = new WP_Query([ "post_type" => "banner", ]); while($Query->have_posts()): $Query->the_post(); ?> <div> <?php the_title(); the_field("image"); //在advanced-custom-fields插件中自定义的field值 ?> </div> <?php endwhile; ?>
3.在wordpress后台安装插件advanced-custom-fields
3.1 安装
3.2 设置field值
3.3 add field添加,设置field值,在这以新建image为例
勾选hide(不显示其他的field)
3.4 单击update完成设置,并发布
至此一个简单的插件就制作完成
二,常用函数
1. 调用css,img,js等链接
<?php echo the_stylesheet_directory_url(); ?> <?php echo time(); ?>
2.菜单(page页面)
<?php wp_nav_menu(); ?>
3.短代码(一般配合插件使用)
<?php echo do_shortcode("短代码内容"); ?>
4.post
<?php if(have_posts()): while (have_posts()): the_post(); ?> <div> <?php the_title(); //标题 the_post_thumbnail_url(); //图片 the_content(); //内容 the_time("l,F,j,Y"); //时间 the_permalink(); //原文链接 previous_post_link(); //上一页 next_post_link(); //下一页 ?> </div> <?php endwhile; endif; ?>
5.sidebar(侧边栏动态显示)
<?php dynamic_sidebar("primary-widget-area"); ?>
在后台的widget设置