【问题标题】:How to get author ID on post.php? $authordata returns NULL (Wordpress)如何在 post.php 上获取作者 ID? $authordata 返回 NULL (Wordpress)
【发布时间】:2020-02-09 13:46:12
【问题描述】:

我在functions.php上尝试了下面的代码


function some_func($post_ID) {
    global $authordata;
    if( !is_null($authordata) ) {
        wp_set_object_terms($post_ID,2,'item_cat'); // I want to get this result.
    } else {    
        wp_set_object_terms($post_ID,7,'item_cat'); // $authordata seems to return NULL, and I get this result.
    }
}
add_action('publish_my_post_type','some_func');

最终,我想获得编辑页面的作者 ID(由于某种原因不是当前用户 ID)。

【问题讨论】:

    标签: wordpress wordpress-admin wordpress-action


    【解决方案1】:

    我认为你在像 single.php 这样的模板中 $authordate 可以在循环内访问,所以你可能忘记使用了

    if (have_posts()):
        while(have_posts()):
            the_post();
        endwhile;
    endif;
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多