首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >发布自定义post类型时在Wordpress中运行函数

发布自定义post类型时在Wordpress中运行函数
EN

Stack Overflow用户
提问于 2012-05-04 09:30:33
回答 2查看 4.7K关注 0票数 0

每当发布wordpress "Jobs“自定义帖子时,我都会尝试运行以下函数。当代码放在我的主题模板中时,它可以工作(第2-7行),但它只在文章被查看时运行。我希望代码在发布帖子时运行,因此我尝试在functions.php内的函数中添加代码,但在发布每个自定义帖子时没有任何反应。有什么建议吗?

代码语言:javascript
运行
复制
function indeedgeo(){
    $indeedgeo = get_post_meta($post>ID, indeedgeo, true);
    $indeedgeos=explode(' ',$indeedgeo);
    $_jr_geo_latitude = $indeedgeos[0];
    $_jr_geo_longitude = $indeedgeos[1];
    update_post_meta($post->ID, _jr_geo_latitude, $_jr_geo_latitude);
    update_post_meta($post->ID, _jr_geo_longitude, $_jr_geo_longitude);
    }
add_action('publish_Jobs', 'indeedgeo');
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-05-04 13:45:27

你应该钩住三个动作中的一个;

代码语言:javascript
运行
复制
do_action('edit_post', $post_id, $post);
do_action('save_post', $post_id, $post);
do_action('wp_insert_post', $post_id, $post);

在保存帖子或更新帖子状态时运行。下面这样的代码应该可以解决这个问题。

代码语言:javascript
运行
复制
function se_10441543_save_post($post_id, $post){
    //determine post type
    if(get_post_type( $post_id ) == 'your_post_type'){
        //run your code
        $indeedgeo = get_post_meta($post_id, indeedgeo, true);
        $indeedgeos=explode(' ',$indeedgeo);
        $_jr_geo_latitude = $indeedgeos[0];
        $_jr_geo_longitude = $indeedgeos[1];
        update_post_meta($post_id, _jr_geo_latitude, $_jr_geo_latitude);
        update_post_meta($post_id, _jr_geo_longitude, $_jr_geo_longitude);
    }
}

add_action('save_post', 'se_10441543_save_post', 10, 2);

http://codex.wordpress.org/Plugin_API/Action_Reference

票数 3
EN

Stack Overflow用户

发布于 2012-05-04 13:03:46

不是很确定你的"publish_jobs“钩子是如何工作的,但是如果你把这个函数放在你的functions.php中,你需要给它一个上下文。将$post>ID替换为帖子编号(整数)。如果这适用于许多帖子,那么您可能希望使用另一种查询帖子数据的方法:http://codex.wordpress.org/Class_Reference/WP_Query。如果这有帮助,请告诉我。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10441543

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档