WordPress 投稿の必須入力アラート

Filed under: functions.php,wordpress — kdcs @ 2018年1月9日 火曜日

デフォルトでは未入力でも下書き保存・公開ができてしまうので
必須入力の項目を指定し、未入力の場合はアラートを出す
function.php内に記述

デフォルトの投稿

function post_edit_required() {
?>
<script>
jQuery(function($) {
  if( 'post' == $('#post_type').val() ) {
    $('#post').submit(function(e) {
      // タイトル
      if ( '' == $('#title').val() ) {
        alert('タイトルを入力してください');
        $('.spinner').css('visibility', 'hidden');
        $('#publish').removeClass('button-primary-disabled');
        $('#title').focus();
        return false;
      }
      // コンテンツ(エディタ)
      if ( $('.wp-editor-area').val().length < 1 ) {
        alert('コンテンツを入力してください');
        $('.spinner').css('visibility', 'hidden');
        $('#publish').removeClass('button-primary-disabled');
        return false;
      }
      // 抜粋
      if ( '' == $('#excerpt').val() ) {
        alert('抜粋を入力してください');
        $('.spinner').css('visibility', 'hidden');
        $('#publish').removeClass('button-primary-disabled');
        $('#excerpt').focus();
        return false;
      }
      // カテゴリー
      if ( $('#taxonomy-category input:checked').length < 1 ) {
        alert('カテゴリーを選択してください');
        $('.spinner').css('visibility', 'hidden');
        $('#publish').removeClass('button-primary-disabled');
        $('#taxonomy-category a[href="#category-all"]').focus();
        return false;
      }
      // タグ
      if ( $('#tagsdiv-post_tag .tagchecklist span').length < 1 ) {
        alert('タグを選択してください');
        $('.spinner').css('visibility', 'hidden');
        $('#publish').removeClass('button-primary-disabled');
        $('#new-tag-post_tag').focus();
        return false;
      }
      // アイキャッチ
      if ( $('#set-post-thumbnail img').length < 1 ) {
        alert('アイキャッチ画像を設定してください');
        $('.spinner').css('visibility', 'hidden');
        $('#publish').removeClass('button-primary-disabled');
        $('#set-post-thumbnail').focus();
        return false;
      }
    });
  }
});
</script>
<?php
}
add_action( 'admin_head-post-new.php', 'post_edit_required' );
add_action( 'admin_head-post.php', 'post_edit_required' );

(続きを読む…)

WordPress カスタム投稿タイプの月別アーカイブ・カテゴリーアーカイブ

Filed under: wordpress — kdcs @ 2018年1月8日 月曜日

WordPress カスタム投稿タイプの月別アーカイブ

アーカイブページ

<select name="archive-dropdown" onChange='document.location.href=this.options[this.selectedIndex].value;'> 
  <option value=""><?php echo attribute_escape(__('Select Month')); ?></option> 
  <?php wp_get_archives('post_type=カスタム投稿名&type=monthly&format=option&show_post_count=1'); ?>
</select>

カテゴリーページ(未検証)

<select name="archive-dropdown" onChange='document.location.href=this.options[this.selectedIndex].value;'> 
  <option value=""><?php echo attribute_escape(__('Select Month')); ?></option> 
  <?php wp_get_archives('type=monthly&format=option&show_post_count=1'); ?>
</select>

WordPress カスタム投稿でカテゴリー名取得とcssクラスにスラッグ付ける

Filed under: wordpress — kdcs @ 2017年12月29日 金曜日

カスタム投稿タイプの記事でカテゴリー(ターム)名を取得
さらに、ccsのクラスにスラッグ名を付けて色分けする

タクソノミー名は’information_category’

<?php
    if ($terms = get_the_terms($post->ID, 'information_category')) {
        foreach ( $terms as $term ) {
            $term_slug = $term -> slug;
            echo ('<span class="') ;
            echo esc_html($term_slug) ;
            echo ('">') ;
            echo esc_html($term->name)  ;
            echo ('</span>') ;
        }
    }
?>

出力はこちら

<span class="スラッグ名">ターム名</span>

WordPress カテゴリー名を色分けして表示する

Filed under: wordpress — kdcs @ 2017年12月29日 金曜日

カテゴリースラッグを取得してcssのクラスにする

新着記事を5件表示する

<?php query_posts("post_type=post&posts_per_page=5"); ?>
<?php if(have_posts()): ?>
<ul>
<?php while(have_posts()): the_post(); ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><span class="ymd">(<?php the_time('Y年m月d日'); ?>)</span></li>
<?php endwhile; ?>
</ul>
<?php else: ?>
<p>現在投稿記事はありません。</p>
<?php endif; wp_reset_query(); ?>

投稿カテゴリー名とスラッグを取得する

<?php
$cat = get_the_category();
$catname = $cat[0]->cat_name; //カテゴリー名
$catslug = $cat[0]->slug; //スラッグ名
?>

でカテゴリー名を囲む

<span class="<?php echo $catslug; ?>"><?php echo $catname; ?></span>

全部まとめる

<?php query_posts("post_type=post&posts_per_page=5"); ?>
<?php
$cat = get_the_category();
$catname = $cat[0]->cat_name;
$catslug = $cat[0]->slug;
?>
<?php if(have_posts()): ?>
<ul>
<?php while(have_posts()):  the_post(); ?>
<li><span class="<?php echo $catslug; ?>"><?php echo $catname; ?></span> <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><span class="ymd">(<?php the_time('Y年m月d日'); ?>)</span></li>
<?php endwhile; ?>
</ul>
<?php else: ?>
<p>現在投稿記事はありません。</p>
<?php endif; wp_reset_query(); ?>

WordPressにSNSボタンを設置する

Filed under: wordpress — kdcs @ 2017年12月19日 火曜日

twitter facebook google+ hatena poket line

※コード内に「target=”_blank”」有り(rel=”noopener noreferrer”を付けて対応)

<ul id="sns_btn">
<li class="sns_btnInner"><a class="twitter" href="http://twitter.com/intent/tweet?text=<?php echo urlencode(the_title("","",0)); ?>&amp;<?php echo urlencode(get_permalink()); ?>&amp;url=<?php echo urlencode(get_permalink()); ?>" target="_blank" rel="noopener noreferrer" title="Twitterで共有">Twitter</a></li>
<li class="sns_btnInner"><a class="facebook" href="http://www.facebook.com/sharer.php?u=<?php echo urlencode(get_permalink()); ?>&amp;t=<?php echo urlencode(the_title("","",0)); ?>" target="_blank" rel="noopener noreferrer" title="facebookで共有">facebook</a></li>
<li class="sns_btnInner"><a class="google_plus" href="https://plus.google.com/share?url=<?php echo urlencode(get_permalink()); ?>" target="_blank" rel="noopener noreferrer" title="Google+で共有">Google+</a></li>
<li class="sns_btnInner"><a class="hatena" href="http://b.hatena.ne.jp/add?mode=confirm&amp;url=<?php echo urlencode(get_permalink()); ?>&amp;title=<?php echo urlencode(the_title("","",0)); ?>" target="_blank" rel="noopener noreferrer" data-hatena-bookmark-title="<?php the_permalink(); ?>" title="このエントリーをはてなブックマークに追加">はてブ</a></li>
<li class="sns_btnInner"><a class="pocket" href="http://getpocket.com/edit?url=<?php echo urlencode(get_permalink()); ?>" target="_blank" rel="noopener noreferrer" title="pocketで共有">pocket</a></li>
<li class="sns_btnInner"><a class="line" href="http://line.naver.jp/R/msg/text/?<?php the_title(); ?>%0D%0A<?php the_permalink(); ?>" target="_blank" rel="noopener noreferrer" title="LINEで送る">LINE</a></li>
</ul>

css

ul#sns_btn {
    width:100%;
    float: left;
    clear: both;
    padding: 0px;
    margin: 0px 0px 10px 0px;
}
ul#sns_btn:after {
    content: ".";
    display: block;
    height: 0;
    clear: both;
    visibility: hidden;
}
ul#sns_btn li.sns_btnInner {
    width:15%;
    float: left;
    padding: 0px;
    margin: 0px 2% 0px 0px;
    list-style-type: none;
}
ul#sns_btn li.sns_btnInner:nth-child(6n) {
    width:15%;
    margin: 0px 0px 0px 0px;
}

@media (max-width: 659px) {
ul#sns_btn li.sns_btnInner {
  width:32%;
  margin: 0px 2% 5px 0px;
  }
}

@media (max-width: 659px) {
ul#sns_btn li.sns_btnInner:nth-child(3n) {
  width:32%;
  margin: 0px 0px 5px 0px;
  }
ul#sns_btn li.sns_btnInner:nth-child(6n) {
  width:32%;
  margin: 0px 0px 5px 0px;
  }
}

ul#sns_btn li.sns_btnInner a {
    font-size: 12px;
    font-weight: bold;
    line-height: 100%;
    color:#ffffff;
    display: block;
    padding: 8px 0px;
    margin: 0px;
    background-color:#f5f5f5;
    text-align:center;
    text-decoration: none;
    border-radius:5px;
}

@media (max-width: 659px) {
ul#sns_btn li.sns_btnInner a {
  font-size: 14px;
  padding: 10px 0px;
  }
}
/*ボタンカラー*/
ul#sns_btn li.sns_btnInner a.twitter {
    background-color: #00acee;
}
ul#sns_btn li.sns_btnInner a.facebook {
    background-color: #3b5998;
}
ul#sns_btn li.sns_btnInner a.google_plus {
    background-color: #dd4b39;
}
ul#sns_btn li.sns_btnInner a.hatena {
    background-color: #2D4C86;
}
ul#sns_btn li.sns_btnInner a.pocket {
    background-color: #EE4056;
}
ul#sns_btn li.sns_btnInner a.line {
    background-color: #5ae628;
}

target=”_blank”を使わず、javascriptでウインドウを開くコード

<?php
$url_encode=urlencode(get_permalink());
$title_encode=urlencode(get_the_title()).'|'.get_bloginfo('name');
?>
<div class="share">
<ul>
<!--Facebookボタン-->
<li class="facebook">
<a href="//www.facebook.com/sharer.php?src=bm&u=<?php echo $url_encode;?>&t=<?php echo $title_encode;?>" onclick="javascript:window.open(this.href, '', 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=300,width=600');return false;">
<i class="fa fa-facebook"></i><span> facebook</span>
<?php if(function_exists('scc_get_share_facebook')) echo (scc_get_share_facebook()==0)?'':scc_get_share_facebook(); ?>
</a>
</li>
<!--ツイートボタン-->
<li class="tweet">
<a href="//twitter.com/intent/tweet?url=<?php echo $url_encode ?>&text=<?php echo $title_encode ?>&tw_p=tweetbutton" onclick="javascript:window.open(this.href, '', 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=300,width=600');return false;">
<i class="fa fa-twitter"></i><span> tweet</span>
<?php if(function_exists('scc_get_share_twitter')) echo (scc_get_share_twitter()==0)?'':scc_get_share_twitter(); ?>
</a>
</li>
<!--Google+ボタン-->
<li class="googleplus">
<a href="//plus.google.com/share?url=<?php echo $url_encode;?>" onclick="javascript:window.open(this.href, '', 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=600,width=500');return false;">
<i class="fa fa-google-plus"></i><span> Google+</span>
<?php if(function_exists('scc_get_share_gplus')) echo (scc_get_share_gplus()==0)?'':scc_get_share_gplus(); ?>
</a>
</li>
<!--はてなボタン-->
<li class="hatena">
<a href="//b.hatena.ne.jp/entry/<?php echo $url_encode ?>" onclick="javascript:window.open(this.href, '', 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=400,width=510');return false;"><i class="fa fa-hatena"></i><span> はてブ</span>
<?php if(function_exists('scc_get_share_hatebu')) echo (scc_get_share_hatebu()==0)?'':scc_get_share_hatebu(); ?></a>
</li>
</ul>
</div>

サイト内検索

カテゴリー

最近の投稿

↑上に戻る