カテゴリの作成者を一覧表示するにはどうすればよいですか?
例えば:
CAT NAME: INTERNET AUTHOR: JOHN, DOE, ALEX
CAT NAME: TECH AUTHOR: JOHN
CAT NAME: CODE AUTHOR: ALEX
WordPressでこれを行う方法はありますか?
カテゴリの作成者を一覧表示するにはどうすればよいですか?
例えば:
CAT NAME: INTERNET AUTHOR: JOHN, DOE, ALEX
CAT NAME: TECH AUTHOR: JOHN
CAT NAME: CODE AUTHOR: ALEX
WordPressでこれを行う方法はありますか?
次のコードを使用できます。
<?php
$cat_arr = get_categories(); // Get the list of Categories
foreach ($cat_arr as $cat_obj) {
$term_id = $cat_obj->term_id;
// Print the Name
?>
<br>
CAT NAME: <?php echo $cat_obj->name ?>, AUTHOR:
<?php
// Get all Posts of that Category
$posts = get_posts(array('category'=>$term_id));
$authors_arr = array();
foreach ($posts as $post_obj) {
$author_id = $post_obj->post_author;
// In depends on where you put this code, the include of the file is required
if (!function_exists('get_userdata')) {
include '<your WP folder>/wp-includes/pluggable.php';
}
$user_obj = get_userdata($author_id);
// Only Add the author is isn't already added, to avoid printed twice
if (!in_array($user_obj->user_login, $authors_arr)) {
$authors_arr[] = $user_obj->user_login; // Instead of user_login you can use any Database field of the "Users" table
}
}
echo implode(', ', $authors_arr) . '<br>';
}
?>
ただし、「現在の」カテゴリ (すべてのカテゴリではなく) に対してこれを実行する場合は、次のコードを使用できます。
$cat_obj = get_the_category(); // This is what you put in your comment to get Current Category
$term_id = $cat_obj->term_id;
// Print the Name
?>
<br>
CAT NAME: <?php echo $cat_obj->name ?>, AUTHOR:
<?php
// Get all Posts of that Category
$posts = get_posts(array('category'=>$term_id));
$authors_arr = array();
foreach ($posts as $post_obj) {
$author_id = $post_obj->post_author;
// In depends on where you put this code, the include of the file is required
if (!function_exists('get_userdata')) {
include '<your WP folder>/wp-includes/pluggable.php';
}
$user_obj = get_userdata($author_id);
// Only Add the author is isn't already added, to avoid printed twice
if (!in_array($user_obj->user_login, $authors_arr)) {
$authors_arr[] = $user_obj->user_login; // Instead of user_login you can use any Database field of the "Users" table
}
}
echo implode(', ', $authors_arr) . '<br>';