別のWordPressからfeedを取得して新着記事にNEW!マークを付ける

Filed under: php,wordpress — kdcs @ 18年9月9日 日曜日

別サイト(ブログなど)WordPressからfeedを取得して新着記事にNEW!マークを付ける
※feedから更新日時・タイトル・カテゴリーを取得する
※一週間(7日)ほどNEW!マークをタイトル前に表示させる

<?php /*--------------ブログフィード取得ここから----------------------*/ ?>
<div id="blogFeed">
<div id="feedBox" class="clearfix">
<h2>◇新着情報◇</h2>
<?php
$rss = simplexml_load_file('http://hogehoge.com/?feed=rss2');
echo '<dl>';
foreach($rss->channel->item as $item){
    $title = $item->title;
    $date = date("Y年 m月 d日", strtotime($item->pubDate));
    $link = $item->link;
    $categorys = $item->category;
    $description = mb_strimwidth (strip_tags($item->description), 0 , 110, "…Read More", "utf-8");
?>
<dt><span class="date pc_block"><?php echo $date; ?></span></dt>
<dd><a href="<?php echo $link; ?>"><span class="date sp_block"><?php echo $date; ?>-<?php echo $categorys; ?></span>
<?php 
if ($date >= date('Y年 m月 d日', strtotime( '- 7 day' ) ) ) { echo '<span class="newmark">NEW!</span>'; }
 ?>
<span class="title">&nbsp;<?php echo $title; ?></span><span class="category">【<?php echo $categorys; ?>】</span></a></dd>
<?php }  echo '</dl>'; ?>
</div><!--/feedBox-->
</div><!--/blogFeed-->
<?php /*--------------ブログフィード取得ここまで----------------------*/ ?>

WordPress 記事からの抜粋文字表示で不要なものを取り除く

Filed under: php,wordpress — kdcs @ 18年6月13日 水曜日

記事の一覧表示をさせる時に文章(テキスト)だけを文字数制限して抜粋する場合、
タグやショートコードまでそのまま表示されてしまうのを防ぐ手段。

例:150文字の制限で改行・タグ・ショートコードを取り除くもの

<p><?php
if(mb_strlen($post->post_content,'UTF-8')>150){
	$content= str_replace('\n', '', mb_substr(strip_tags(strip_shortcodes($post-> post_content)), 0, 150,'UTF-8'));
	echo $content.'...';
}else{
	echo str_replace('\n', '', strip_tags($post->post_content));
}
?>
</p>

str_replaceは改行コード、strip_tagsはタグ、strip_shortcodesはショートコード
(続きを読む…)

wordpressのheader.phpに入れる「meta property ogp」

Filed under: php,wordpress — kdcs @ 18年3月9日 金曜日

meta propertyはsns用の記述
※特に記事ごとにタイトルやディスクリプション、画像を個別に設定すると効果的なので
条件分岐(is_single)で対応。
また、アイキャッチ画像があれば取得し、無ければ記事内の画像を取得する
まったく画像が無ければデフォルトで用意した画像(sns_image.jpg)を取得する。

2022.4.21追記
17行目がエラーになるので修正
こちらが修正版

$str = isset($post->post_content);

こちらも使える”??” 演算子 (null 合体演算子)

$str = $post->post_content ?? 'none';
<meta property='og:locale' content='ja_JP'>
<meta property='og:site_name' content='<?php bloginfo('name'); ?>'>
<?php
if (is_single()){
if(have_posts()): while(have_posts()): the_post();
echo '<meta property="og:title" content="'; the_title(); echo '">';echo "\n";
echo '<meta property="og:description" content="'.mb_substr(get_the_excerpt(), 0, 120).'">';echo "\n";
echo '<meta property="og:url" content="'; the_permalink(); echo '">';echo "\n";
echo '<meta property="og:type" content="article">';echo "\n";
endwhile; endif;
} else {
echo '<meta property="og:title" content="'; bloginfo('name'); echo '">';echo "\n";
echo '<meta property="og:description" content="'; bloginfo('description'); echo '">';echo "\n";
echo '<meta property="og:url" content="'; bloginfo('url'); echo '">';echo "\n";
echo '<meta property="og:type" content="website">';echo "\n";
}
$str = $post->post_content;
$searchPattern = '/<img.*?src=(["\'])(.+?)\1.*?>/i';
if (is_single()){
if (has_post_thumbnail()){
$image_id = get_post_thumbnail_id();
$image = wp_get_attachment_image_src( $image_id, 'full');
echo '<meta property="og:image" content="'.$image[0].'">';echo "\n";
} else if ( preg_match( $searchPattern, $str, $imgurl )){
echo '<meta property="og:image" content="'.$imgurl[2].'">';echo "\n";
} else {
echo '<meta property="og:image" content="'; bloginfo('template_url'); echo '/images/sns_image.jpg'; echo '">';echo "\n";
}
} else {
echo '<meta property="og:image" content="'; bloginfo('template_url'); echo '/images/sns_image.jpg'; echo '">';echo "\n";
}
?>
<meta name="twitter:card" content="summary_large_image">

WordPressに外部ブログの更新情報を掲載する

Filed under: php,wordpress — kdcs @ 17年12月6日 水曜日

外部で運営中のブログ(アメーバブログ)のfeedから情報を取得し
WordPressのページに更新情報を掲載する方法をphpで行う
※PR(広告記事は除外する)amebloIDの部分を書き換える

<?php
  include_once(ABSPATH . WPINC . '/feed.php');

  $display_posts_count = 5;
  $get_posts_count = 10;
  $ameblo_feed = fetch_feed('http://rssblog.ameba.jp/AmebloID/rss20.xml');

  $counter = 0;
  if (!is_wp_error( $ameblo_feed ) ) :
    $maxitems = $ameblo_feed->get_item_quantity($get_posts_count);
    $feed_items = $ameblo_feed->get_items(0, $maxitems);
  endif;
?>
<ul>
  <?php if ( $feed_items == 0 ) echo '<li>新しい記事はありません</li>';
    else
    foreach ( $feed_items as $item ) :
    if( !preg_match('/^PR:/', $item->get_title() ) && $counter < $display_posts_count ):
  ?>
  <li>
    <span><?php echo $item->get_date('Y.m.d'); ?></span><br />
    <a href="<?php echo $item->get_permalink(); ?>" target="_blank"><?php echo $item->get_title(); ?></a>
  </li>
  <?php
    $counter++;
    endif;
    endforeach;
  ?>
</ul>

(続きを読む…)

wordpress カスタムフィールド出力の文字数制限

Filed under: php,wordpress — kdcs @ 17年2月26日 日曜日

wordpressでテキスト入力のカスタムフィールドを出力する場合にある程度文字出力をそろえたい。
※文字数制限の後に「…」を付ける

「mb_substr()」を使用する方法

<?php $text = mb_substr(get_field('カスタムフィールド名'),0,30,'utf-8'); echo $text.'...'; ?>

「mb_strimwidth()」を使用する方法

<?php echo mb_strimwidth(nl2br(post_custom('カスタムフィールド名')),0,100,'...','utf-8'); ?>

「mb_strimwidth()」は1バイト文字(半角)を1とカウント、2バイト文字(全角)を2とカウントするので文字出力がそろえやすい。

サイト内検索

カテゴリー

最近の投稿

↑上に戻る