在wordpress中获取帖子的作者ID [英] Get author ID of a post in wordpress

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

问题描述

我创建一个循环,它提取有关用户活动的信息。代码是在作者页上,我想要的是获取当前的作者ID,所以根据用户所在的作者页面将获取该作者的ID。



看下面的内容,我希望AUTHOR-ID成为当前作者的ID。

 < script type =text / javascript> 
var pieData = [
<?php

$ user_id = AUTHOR-ID;
$ b $ *获取所有类别* /
$ rand = array('0','1','2','3','4','5','6 '','7','8','9','a','b','c','d','e','f');


$ categories = get_terms('category','orderby = count& hide_empty = 0');

/ *为每个类别循环计算用户的职位* /
foreach($ categories as $ category)
{
$ color ='#' 。$兰特[兰特(0,15)]。$兰特[兰特(0,15)]。$兰特[兰特(0,15)]。$兰特[兰特(0,15)]。$兰特[兰特( 。0,15)] $兰特[兰特(0,15)];
$ cat_name = $ category-> name;
$ cat_id = $ category-> term_id;
$ post_count = count(get_posts(cat = $ cat_id& post_author = $ user_id));

echo

{
value:。$ post_count。,
color:'。$ color。',
标签:。$ user_id。
},;

}
?>
]
var myPie = new Chart(document.getElementById(piec)。getContext(2d))。Pie(pieData);
< / script>


解决方案

 <?php $ author_id = $ post-> post_author; ?> 

它会给你当前的作者ID。

<

  global $ current_user; 
get_currentuserinfo();

$ args = array(
'author'=> $ current_user-> ID,//我也可以使用$ user_ID,对吧?
);

//取得他的文章'ASC'
$ current_user_posts = get_posts($ args);

谢谢。


Im creating a loop where it pulls info about a users activity. the code is on the author page and what i want to to is get the current author ID, so based on what author page the user is on it will fetch the ID of that author.

See below, i would want AUTHOR-ID to be the ID of the current author.

<script type="text/javascript">
                            var pieData = [
                            <?php

                            $user_id = AUTHOR-ID;

                            /* Get all categories */
                            $rand = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f');


                            $categories = get_terms( 'category', 'orderby=count&hide_empty=0' );

                            /* Loop for each category to count the posts of the user */
                            foreach($categories as $category)
                            {
                            $color = '#'.$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)];
                               $cat_name = $category->name;
                               $cat_id = $category->term_id;
                               $post_count = count(get_posts("cat=$cat_id&post_author=$user_id"));

                               echo "

                                            {
                                                value: ".$post_count.",
                                                color:'".$color."',
                                                label: ".$user_id."
                                            },";

                            }
                            ?>
                            ]
                            var myPie = new Chart(document.getElementById("piec").getContext("2d")).Pie(pieData);
                        </script>

解决方案

Try with this :

<?php $author_id=$post->post_author; ?>

it will give you current author id.

or this one will helps you more:

global $current_user;
get_currentuserinfo();                      

$args = array(
    'author'        =>  $current_user->ID, // I could also use $user_ID, right?        
    );

// get his posts 'ASC'
$current_user_posts = get_posts( $args );

Thanks.

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

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