▽1
●
rouge_2008 ●500ポイント ベストアンサー |
データベースに直接問い合わせる方法になります。
利用しているテーマのfunctions.phpに以下の関数※(関数名は何でもいいのですが・・・)を追加します。
function get_usergroups() { global $wpdb, $current_user; $groups = array(); if($current_user) { $q = "SELECT ID, groupname FROM ". $wpdb->prefix . "uam_accessgroups AS acg JOIN " . $wpdb->prefix . "uam_accessgroup_to_object AS acgto ON acg.ID = acgto.group_id WHERE acgto.object_type = 'user' AND acgto.object_id = " . $current_user->ID; $res = $wpdb->get_results($q); if(isset($res) && !empty($res)) { foreach($res as $grp) { $groups[] = esc_html($grp->groupname); } } } return $groups; }
次のトップページのテンプレートの任意の箇所で次のようにします。
$uam_grp = get_usergroups(); if(!empty($uam_grp) && in_array("表示を許可するグループ名", $uam_grp)) { echo "<div>\n"; $page_id = 10; $page = get_page($page_id); echo apply_filters('the_content', $page->post_content); echo "</div>\n"; } else { /* 許可されないユーザーに対する処理 */ }
【※追記※】
サイト管理人にも表示する場合は次のようにするといいです。
if($current_user->ID == 1 || !empty($uam_grp) && in_array("許可するグループ名", $uam_grp)) {
表示許可するグループが複数ある場合は次のようにしてみてください。
if($current_user->ID == 1 || !empty($uam_grp) && (in_array("グループ名1", $uam_grp) || in_array("グループ名2", $uam_grp))) {
※許可するグループが3つ以上ある場合も同じように追記します。