wordpress从侧面小部件获取帖子ID [英] wordpress get post id from side widget

查看:40
本文介绍了wordpress从侧面小部件获取帖子ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从我放入侧边栏的小部件中获取帖子 ID,但无论我尝试什么,我似乎都从其他帖子中获取了随机帖子 ID.

I am trying to get the post id from a widget I am putting into the sidebar, but what ever I try I seem to get random post id's from other posts.

我试过了:

            $id = get_the_id();

            $post_id = $GLOBALS['post']->ID;

            global $wp_query;

            $thePostID = $wp_query->post->ID;

我使用的模板是 classipress,我将我的小部件放入他们提供的侧边栏中.他们已经在侧边栏顶部有一个使用帖子 ID 的表单.他们使用

The template I am using is classipress and I am putting my widget into the sidebar they provide. They already have a form at the top of the sidebar which uses the post id. They use

            global $post;

我也试过这个,它也给了我一个不同的帖子 ID,但不是正确的.

I have tried this also and it too gives my a different post id, but not the correct one.

在这个阶段我完全没有想法.

I am all out of ideas at this stage.

推荐答案

确保您的插件添加了:

add_action("widgets_init", "your_init");

否则 $post 不会被初始化.

otherwise $post won't be initialized at the time.

这篇关于wordpress从侧面小部件获取帖子ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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