WordPress教程
模板兔提供专业的wordpress建站教程、wordpress主题开发教程、wordpress插件开发教程、wordpress二次开发教程等。
WordPress教程:// remove the old box
function remove_default_categories_box() {
remove_meta_box('categorydiv', 'post', 'side');
}
add_action( 'admin_head', 'remove_default_categories_box' );
// add the new box
function add_custom_categories_box() {
add_meta_box('customcategorydiv', 'Categories', 'custom_post_categories_meta_box', 'post', 'side', 'low', array( 'taxonomy' => 'category' ));
}
add_action('admin_menu', 'add_custom_categories_box');
/**
* Display CUSTOM post categories form fields.
*
* @since 2.6.0
*
* @param object $post
*/
function custom_post_categories_meta_b...
WordPress教程:最近给某个客户定制主题时需要实现无限下拉加载功能,以往模板兔都是用某个js插件来实现了,今天教大家一个不用插件实现的方法:
首页,在php里加上相关代码:
<div class="article-list mobantu" id="article-list">
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'caller_get_posts' => 1,
'paged' => $paged
);
query_posts($args);
while ( have_posts() ) : the_post();
get_template_part( 'content', get_post_format() );
endwhile; wp_reset_query();
?>
</div>
<?php
$next_page = get_next_posts_link('加载更多');
if...
WordPress教程:有时候我们需要为网站添加一个自定义文章类型,但是当访问此文章类型页面时,如何获取相关联的文章呢?请看以下代码:
$terms = get_the_terms( $post->ID , 'product_tags', 'string');
$term_ids = wp_list_pluck($terms,'term_id');
$second_query = new WP_Query( array(
'post_type' => 'products',
'tax_query' => array(
array(
'taxonomy' => 'product_tags',
'field' => 'id',
'terms' => $term_ids,
'operator'=> 'IN' //Or 'AND' or 'NOT IN'
)),
'posts_per_page' => 3,
'ignore_sticky_posts' => 1,
'orderby' => 'rand',
'post__not_in'=>array($post->ID...
WordPress教程:网站要是置顶文章多了,就会堆积在网站首页,那么怎么不显示置顶的文章呢?
<?php
$the_query = new WP_Query( array( 'post__not_in' => get_option( 'sticky_posts' ) ) );
if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();
?>
WordPress教程:面包屑导航对于一个网站SEO来说还是挺重要的,可以使用以下代码来给自己的主题添加面包屑,将以下代码加入到主题的functions.php里:
function MBThemes_breadcrumbs() {
/* === OPTIONS === */
$text['home'] = '首页';
$text['category'] = '%s';
$text['search'] = '搜索结果:%s';
$text['tag'] = '标签:%s';
$text['author'] = '作者:%s';
$text['404'] = '404';
$text['page'] = '%s';
$text['cpage'] = '%s';
$wrap_before = '<div class="breadcrumbs">';
$wrap_after = '</div>';
$sep = '>';
$sep_before = '<span class="sep">';
$sep_after = '</span>'; ...
WordPress教程:最近给一个用户做手机端的滑动菜单,用到某个js,但是其中需要往item写一个固定的class才行,所以必须使用wp_nav_menu来给li添加自定义的class,那么将以下代码加入主题的functions.php里即可:
function MBT_menu_classes($classes, $item, $args) {
if($args->theme_location == 'mobile') { //这里的mobile是菜单的id
$classes[] = 'custom-class';
}
return $classes;
}
add_filter('nav_menu_css_class','MBT_menu_classes',1,3);
WordPress教程:在本站购买下载了erphpdown wordpress下载插件之后,解压压缩包,看到一个erphpdown.zip,在网站后台(插件-安装插件)上传erphpdown.zip安装包,或者解压erphpdown.zip通过FTP/SFTP上传到网站目录(/wp-content/plugins/)下,然后启用插件。
以后升级插件是在本站下载最新版后直接FTP覆盖即可,不会影响网站数据。
启用插件后,后台左侧会出现一个Erphpdown菜单,设置下 基础设置、支付设置、显示设置、VIP设置,然后发布文章的时候你会在编辑框下面看到Erphpdown属性来设置下载信息的选项。
如果你需要采集,请看https://www.mobantu.com/9107.html
具体截图:(看不清楚的可以把图片下载下来看)
图片下载链接: https://pan.ba...
WordPress教程:WordPress 3.5以上的版本,隐藏了后台的媒体(Media)设置页面 上传路径(upload_path)和文件 URL 地址(upload_url_path)的设定。
直接将下面的代码添加到主题的 functions.php,就可以恢复设置界面了:
if(get_option('upload_path')=='wp-content/uploads' || get_option('upload_path')==null) {
update_option('upload_path',WP_CONTENT_DIR.'/uploads');
}
推荐使用这个方法,最简单有效。
通过代码直接定义
将下面的代码添加到主题的 functions.php 的最后一个 ?> 前面:
add_filter( 'upload_dir', 'MBT_custom_upload_dir' );
function MBT_custom_upload_dir( $uploads ) {
$upload_path = '';
$upload_url...
WordPress教程:有很多人找模板兔做筛选功能,不管是自定义字段筛选还是分类法筛选,或者分类与标签筛选,其实都大同小异,这里模板兔提供自定义分类法筛选文章的功能教程。
首先,新建自定义分类法,这里举例两个:
add_action('init', 'MBT_post_type');
function MBT_post_type() {
register_taxonomy(
'people',
'post',
array(
'label' => '人物',
'rewrite' => array( 'slug' => 'people' ),
'hierarchical' => true
)
);
register_taxonomy(
'company',
'post',
array(
'label' => '企业',
'rewrite' => array( 'slug' => 'company' ),
'hierarchical' => true
)
);
}
加入以上...
WordPress教程:很多用户使用的虚拟主机都禁用了mail函数以至于网站无法发邮件(注册用户、找回密码等一些操作都需要发送邮件),虽然有很多SMTP插件可以解决此问题,但是,模板兔的原则是,能不用插件就坚决不用插件,特比是一些小功能,也没必要使用插件。
将以下代码插入到主题的functions.php里:
//使用smtp发邮件
function MBT_mail_smtp( $phpmailer ) {
$phpmailer->IsSMTP();
$phpmailer->SMTPAuth = true;//启用SMTPAuth服务
$phpmailer->Port = 465;//MTP邮件发送端口,这个和下面的对应,如果这里填写25,则下面为空白
$phpmailer->SMTPSecure ="ssl";//是否验证 ssl,这个和上面的对应,如果不填写,则上面的端口须为2...
WordPress教程:有时候会发现wordperss后台越来越卡,是什么原因呢?原因可能很多,但是一些更新的获取可能是其中之一。那么可以按需关系这些更新。
将以下代码加入主题的functions.php里(最后一个 ?> 之前):
class OS_Disable_WordPress_Updates {
private $__pluginsFiles;
private $__themeFiles;
/**
* The OS_Disable_WordPress_Updates class constructor
* initializing required stuff for the plugin
*
* PHP 5 Constructor
*
* @since 1.3
* @author scripts@schloebe.de
*/
function __construct() {
$this->__pluginsFiles = array();
$this->__themeFiles = array();
add_action( 'admin_ini...
WordPress教程:最近给某个客户二次开发时有这样一个需求,客户使用的wp插件wp user frontend pro来实现前端投稿,可是这个插件的pro版在文章正文上传图片时调用的不是系统自带的媒体库上传按钮,而是单独弄的一个上传控件,这对于处女座的人来说无疑是一个不可接受的用户体验。
也有人向插件作者提出这个问题,作者的回复是:(看不懂的用户请自行Google翻译)
“As people use the plugin for public post submission, most of them didn’t wanted to expose others uploaded image to other users. Thats why it’s removed and replaced with a plugin native media uploader.”
意思就是说他不想让用户看到所有的媒体文件!!!
难道他不知道wordpr...
WordPress教程:有时候我们开放用户自己发布文章的权限,但是又不想让媒体库里的所有图片/文件让作者及其以下角色权限的用户看到,怎么办呢?要解决这个问题,将下面的代码添加到当前主题的 functions.php 文件中:
add_action('pre_get_posts','MBT_restrict_media_library');
function MBT_restrict_media_library( $wp_query_obj ) {
global $current_user, $pagenow;
if( !is_a( $current_user, 'WP_User') )
return;
if( 'admin-ajax.php' != $pagenow || $_REQUEST['action'] != 'query-attachments' )
return;
if( !current_user_can('manage_media_library') )
$wp_query_obj->set('au...
WordPress教程:这款插件功能很强大:
1.wordpress 分类目录 转换为 子域名 (绑定二级域名)形式
2.wordpress 页面转换为 子域名 (绑定二级域名) 形式
3.wordpress 作者页转换为 子域名 (绑定二级域名)形式
4.为不同子域名 (绑定二级域名) 选用不同的 wordpress 主题
下面详细介绍 给 WordPress 分类目录 绑定 二级域名 的方法:
需要将*.domain.com A记录指向你的服务器IP,然后服务器需要绑定*.domain.com
WP Subdomains插件设置
(1)Wordpress 分类目录 子域名 url (绑定二级域名)可以直接在 WP Subdomains插件 设置界面完成
(2)对于Wordpress页面来说需要在页面编辑是添加 wps_page_subdomain 的自定义字段,赋值为true。...
WordPress教程:有时候网站后台发布文章时莫名其妙的有些按钮点击失效了,例如编辑器下滑时无法固定,编辑可视化/文本模式无法切换等等,导致这些bug的原因在于更新了系统却没有更新语言包。
解决方案:
将wp-content目录下的languages文件夹删除,然后进后台的 仪表盘-更新(Dushboard-Updates) (后台路径是http://你的域名/wp-admin/update-core.php)页面点击下面的update translations按钮,看到语言包更新完毕后即可。