WP ワードプレスリファレンス

ワードプレスで権限者ごとに特定の機能を付与する

// ユーザー権限を付与する
function add_theme_caps(){
  $role = get_role( 'editor' ); //'権限者'
  $role->add_cap( 'list_users' ); //付与する機能
}
add_action( 'admin_init', 'add_theme_caps' );

管理者 : administrator
編集  : editor
投稿者 : author
寄稿者 : contributor
購読者 : subscriber
権限を付与する記述
権限を付与する記述
権限を付与の一覧

付与する権限一覧

ワードプレスでユーザー一覧を表示させる方法

<?php $users = get_users(array(
      'orderby'=>ID,
      'order'=>ASC,
      'exclude'=>array(1,3) //表示除外したいユーザーID
      ));?>
//表示箇所
<ul>
  <?php foreach($users as $user):
    $uid = $user->ID;
    ?>
  <li><a href="<?php echo get_bloginfo("url") . '/?author=' . $uid ?>">
    <strong><?php echo $user->display_name ;?></strong>
  </a></li>

</ul>

ユーザー情報改変

ユーザー情報に項目を登録する

function my_user_meta($item){
//項目の追加
$item['twitter'] = 'twitter';
$item['facebook'] = 'フェイスブック';
$item['insta'] = 'インスタグラム';
return $item;
}
add_filter('user_contactmethods', 'my_user_meta', 10, 1);

ユーザー情報に登録した情報を出力する方法

※author.phpでしか取得できません

<?php the_author_meta(twitter, $author);?> //ツイッター情報
<?php the_author_meta(facebook, $author);?> //フェイスブック情報
<?php the_author_meta(insta, $author);?> //インスタグラム情報

他のテンプレートファイルでの出力方法

<ul>
  <?php $args = array(
  'orderby'=>ID, //ユーザーIDを取得
  'order'=>ASC, //新着順
  'role__not_in'=>array('editor','administrator'), //特定の権限のユーザーは表示させない。
  );
  $user_query = new WP_User_Query( $args, );
  ?>

  <?php if(!empty($user_query->results)): foreach($user_query->results as $user):?>
  <li><a href="<?php echo get_bloginfo("url") . '/?author=' . $user->ID;?>">
  <?php
  $field01 = get_field('cast_img01', 'user_' . $user->ID); //カスタムフィールドユーザー取得表示
  $url01 = $field01['url'];
  ?>
    <img src="<?php echo $url01;?>">
    <strong><?php echo $user->display_name;?></strong>// ユーザー名
    <strong><?php echo $user->description;?></strong> //プロフィール情報
    <strong><?php echo $user->twitter;?></strong>//ツイッター
  </a></li>
<?php endforeach; else:?>
<li>ただいま準備中</li>
<?php endif;?>
</ul>

参考サイト

ユーザー情報で不要な入力項目を非表示にする方法

管理画面ユーザー情報で検証ボタンでidやclassを確認して非表示の指定をする

function user_profile_hide_style() {
  echo '<style>
  #your-profile .user-rich-editing-wrap, /* ビジュアルエディター */
  #your-profile .user-comment-shortcuts-wrap, /* キーボードショートカット */
  #your-profile .show-admin-bar, /* ツールバー */
  #your-profile .user-first-name-wrap, /* 名 */
  #your-profile .user-last-name-wrap, /* 姓 */
  #your-profile .user-profile-picture, /* プロフィール写真 */
  #your-profile .user-url-wrap, /* url */
  #your-profile .user-sessions-wrap /* セッション */ {
    display: none;
  }
  </style>'.PHP_EOL;
}
add_action('admin_print_styles', 'user_profile_hide_style');

ワードプレス用語

・タクソノミー
左メニューの親項目の子項目
・ターム
子項目内のカテゴリー
参考サイト

記事本文の文字省略の記述方法

the_excerpt()で取得した抜粋は記事本文の最初も文字を抜粋し記事一覧で表示可能
・自動的に110文字で省略され、以降の文字は…で表示される。
記述方法

//表示させたいファイルに以下を記述
<?php the_excerpt(); ?>

抜粋する文字数を変更する

抜粋する文字を変更したい場合は以下の記述をfunction.phpに記述する

function twpp_change_excerpt_length($length) {
  return 50;
}
add_filter('excerpt_length', 'twpp_change_excerpt_length', 999);

記事一覧ページャーの記述方法

記事一覧での番号付ページャー
paginate_links関数を使った記述を使います。
注意点
※必ず管理画面内の表示件数とファイルに記述したposts_per_pageの表示件数を同じにすること
(ページ送りがうまくいかない。)
参考サイト

// 記事一覧ページャー
if ( ! function_exists('wpex_pagination') ) {
  function wp_pagination() {
    global $wp_query;
    $total = $wp_query->max_num_pages;
    $big = 999999999; // need an unlikely integer
    if( $total > 1 )  {
       if( !$current_page = get_query_var('paged') )
         $current_page = 1;
       if( get_option('permalink_structure') ) {
         $format = 'page/%#%/';
       } else {
         $format = '&paged=%#%';
       }
      echo paginate_links(array(
        'base'    => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
        'format'  => $format,
        'current' => max( 1, get_query_var('paged') ),
        'total'   => $total,
        'mid_size'  => 4,
        'type'    => 'list',
        'prev_text' => '<i class="fa fa-angle-left"></i>',
        'next_text' => '<i class="fa fa-angle-right"></i>',
       ));
    }
  }
}

// 表示したいファイルに以下の関数を記述
<?php wp_pagination(); ?>

paginate_linksのリファレンス

カスタム投稿名を出力

以下の記述で出力

<?php echo esc_html(get_post_type_object(get_post_type())->label); ?>