wordpress二次开发的点点滴滴

语法

循环数组时添加html

<?php
$mobileMenu=wp_nav_menu_source();
foreach($mobileMenu as $itemMenu){
   echo $itemMenu['url']; 
}
?>

获取特定页面的链接

  • 登录页面 <?php echo esc_url( wp_login_url() ); ?>

  • 忘记密码页面 <?php echo esc_url( wp_lostpassword_url() ); ?>

  • 站点根目录 <?php echo site_url(); ?>

申明对woocommerce插件的支持

在主题的functions.php文件中的任意位置添加代码

add_theme_support( 'woocommerce' );

后台侧边栏添加一级菜单

在自定义主题的functions.php中,使用add_menu_page()函数来添加菜单的内容

// 添加自定义菜单
function my_add_pages() {
    // 第一个参数'Help page'为菜单名称,
    // 第二个参数'使用帮助'为菜单标题
    // 'manage_options' 参数为用户权限
    // 'my_toplevel_page' 参数用于调用my_toplevel_page()函数,来显示菜单内容
    add_menu_page('Help page', '我的菜单', 'manage_options', '自定义参数', 'my_toplevel_page');
}
function my_toplevel_page() {
    echo '这里填菜单页面的HTML代码';
   
}
// 通过add_action来自动调用my_add_pages函数
add_action('admin_menu', 'my_add_pages');

后台侧边栏添加二级菜单

和添加一级菜单类似,如果是为一级菜单设置添加,只需要将add_menu_page()函数换成add_options_page()即可,其他更多的选项,查询开发手册

add_options_page('Help page', '我的菜单', 'manage_options', '自定义参数', 'my_toplevel_page');

为新增菜单添加页面

关闭wordpress后台更新通知

在主题的functions.php增加下面的代码

add_action('after_setup_theme','remove_core_updates');
function remove_core_updates()
{
 if(! current_user_can('update_core')){return;}
 add_action('init', create_function('$a',"remove_action( 'init', 'wp_version_check' );"),2);
 add_filter('pre_option_update_core','__return_null');
 add_filter('pre_site_transient_update_core','__return_null');
}

用户

判断是否登陆

is_user_logged_in()

获取当前登录用户信息

$user = wp_get_current_user();
echo $user->display_name;
echo $user->ID

菜单

获取菜单数组

请输入代码

移动端

判断当前设备类型

wordpress自带函数,函数定义在wp-includesvars.php中,如果是移动端,则返回true,不是返回false

wp_is_mobile()

文章

在文章循环中获取文章发布时间

<?php the_time();?>

获取最新发表的10篇文章

wp_get_recent_posts()函数用俩获取最新发表的文章,参数仫为10,我们可以传递自定义的参数进去,函数返回一个数组,数组元素就是文章的详情,包括了和文章相关的各种信息

 $recent_posts = wp_get_recent_posts('10');
    array
      'ID' => int 1
      'post_author' => string '1' (length=1)
      'post_date' => string '2015-11-11 16:38:45' (length=19)
      'post_date_gmt' => string '2015-11-11 08:38:45' (length=19)
      'post_content' => string '欢迎使用WordPress。' (length=99)
      'post_title' => string '世界,您好!' (length=18)
      'post_excerpt' => string '' (length=0)
      'post_status' => string 'publish' (length=7)
      'comment_status' => string 'open' (length=4)
      'ping_status' => string 'open' (length=4)
      'post_password' => string '' (length=0)
      'post_name' => string 'hello-world' (length=11)
      'to_ping' => string '' (length=0)
      'pinged' => string '' (length=0)
      'post_modified' => string '2015-11-11 16:38:45' (length=19)
      'post_modified_gmt' => string '2015-11-11 08:38:45' (length=19)
      'post_content_filtered' => string '' (length=0)
      'post_parent' => int 0
      'guid' => string 'http://localhost/wordpress/?p=1' (length=31)
      'menu_order' => int 0
      'post_type' => string 'post' (length=4)
      'post_mime_type' => string '' (length=0)
      'comment_count' => string '1' (length=1)
      'filter' => string 'raw' (length=3)

循环显示文章标题带链接

get_permalink()接受一个文章的id,返回文章的链接

 $recent_posts = wp_get_recent_posts('10');
 foreach( $recent_posts as $recent ){
  echo '<li><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' .   $recent["post_title"].'</a> </li> ';
 }

获取指定分类news下面文章的数量

<?php  get_category_by_slug('news')->count; ?>

获取分类news的分类id

<?php  get_category_by_slug('news')->cat_ID; ?>

获取指定分类下面的文章列表

请输入代码

获取分类名列表

wp_list_categories();

获取指定id的文章的分类名

$post=get_posts();
get_the_category($post[0]->ID);

自定义页面

添加一个新的页面

首先在主题的根目录下新建一个php文件,文件开头加上如下代码,然后将page.php中的内容全部拷贝到该文件中,在后台中新建页面,在右下角的页面属性中,模板下拉框选择新添加的这个文件

<?php
/*
Template Name:you page name
*/
?>

获取当前页面中所有的图片链接和描述

在functions.php中添加下面的代码,在前端页面中使用 <?php echo hui_get_thumbnail(false,true);?>直接调用

function hui_get_thumbnail( $single=true, $must=true ) {
    global $post;
    $html = '';
    if ( has_post_thumbnail() ) {
        $domsxe = simplexml_load_string(get_the_post_thumbnail());
        $src = $domsxe->attributes()->src;
        $src_array = wp_get_attachment_image_src(hui_get_attachment_id_from_src($src), 'thumbnail');
        $html = sprintf('<li><img src="%s" /></li>', $src_array[0]);
    } else {
        $content = $post->post_content;
        preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $content, $strResult, PREG_PATTERN_ORDER);
        $images = $strResult[1];
        $counter = count($strResult[1]);
        $i = 0;
        foreach($images as $src){
            $i++;
            $src2 = wp_get_attachment_image_src(hui_get_attachment_id_from_src($src), 'thumbnail');
            $src2 = $src2[0];
            if( !$src2 && true ){
                $src = $src;
            }else{
                $src = $src2;
            }
            $item = sprintf('<li><img src="%s" /></li>', $src);
            if( $single){
                return $item;
                break;
            }
            $html .= $item;
            if(
                ($counter >= 4 && $counter < 8 && $i >= 4) ||
                ($counter >= 8 && $i >= 8) ||
                ($counter > 0 && $counter < 4 && $i >= $counter)
            ){
                break;
            }
        }
    }
    return $html;
}

function hui_get_attachment_id_from_src ($link) {
    global $wpdb;
    $link = preg_replace('/-\d+x\d+(?=\.(jpg|jpeg|png|gif)$)/i', '', $link);
    return $wpdb->get_var("SELECT ID FROM {$wpdb->posts} WHERE guid='$link'");
}

在页面中获取指定分类名下面文章的数量

get_category_by_slug()函数中的参数为我们指定的分类名

<?php echo get_category_by_slug('name')->count; ?>

循环显示指定分类名下面的文章

注意cat参数的值就是要显示分类id,page参数是每页要显示的数量

<?php query_posts('cat=59&posts_per_page=100'); while(have_posts()): the_post(); ?>   
     <li><a href="<?php the_permalink(); ?>" target="_blank"><?php the_title();?></a></li>
<?php endwhile; wp_reset_query(); ?>

侧边栏

添加侧边栏

在页面任何位置添加get_sidebar()函数,即可输出侧边栏

<?php get_sidebar(); ?>

删除页面中的侧边栏

将页面中的get_sidebar()函数注释

<?//php get_sidebar(); ?>

自定义样式

在页面中添加js代码

直接在php的最后面添加js代码

<script src="/wp-content/themes/主题名字/js/name.js"></script>

在页面中添加css样式

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值