如何使用jQuery获取Wordpress中的当前页面ID [英] how to get the current page id in wordpress using jquery

查看:82
本文介绍了如何使用jQuery获取Wordpress中的当前页面ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何单独使用jquery在wordpress中获取页面ID.我打算使用需要了解页面ID的自定义脚本来更改页面的某些样式.

how to get a page id in wordpress using jquery alone.I am planing to change some styles of a page using a custom script for which i need to know page id.

推荐答案

function get_current_page_id() {
    var page_body = $('body.page');

    var id = 0;

    if(page_body) {
        var classList = page_body.attr('class').split(/\s+/);

        $.each(classList, function(index, item) {
            if (item.indexOf('page-id') >= 0) {
                var item_arr = item.split('-');
                id =  item_arr[item_arr.length -1];
                return false;
            }
        });
    }
    return id;
}

将此功能添加到您的代码中. 您现在可以使用以下方法获取页面ID:

Add this function to your code. You can now get the page id by using:

var id = get_current_page_id();

这篇关于如何使用jQuery获取Wordpress中的当前页面ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆