http://b.hishitu.net/3814.html
<?php
$my_content = get_the_content(); コンテンツ取得
$my_content = preg_replace("|(<img[^>]+>)|si","",$my_content); //イメージ要素をのぞく
?>
しかし moreタグとセットでしかつかえない
すべての画像が消える訳だから。
…もまだだせんな
http://b.hishitu.net/3814.html
<?php
$my_content = get_the_content(); コンテンツ取得
$my_content = preg_replace("|(<img[^>]+>)|si","",$my_content); //イメージ要素をのぞく
?>
しかし moreタグとセットでしかつかえない
すべての画像が消える訳だから。
…もまだだせんな
最終 最初に改行が入ってしまう。
コード
<?php
$my_content = get_the_content(); //コンテンツ取得
$my_content = preg_replace("|(<img[^>]+>)|si","",$my_content); //イメージ要素をのぞく
$my_content2 = strip_tags($my_content); //タグをのぞく
?>
<?php echo nl2br(get_trim_str(array('str'=>$my_content2, 'len'=>100,'echo' => false))); ?>
//改行されてるところにbrを挿入する さらに独自関数 get_trim_strを使う echoをファルスにする。
とおもったができた。
<?php
$my_content = get_the_content(); //コンテンツ取得
$my_content = preg_replace("|(<img[^>]+>)|si","",$my_content); //イメージ要素をのぞく
$my_content = wpautop($my_content); //br p を調整
$my_content = strip_tags($my_content); //タグをのぞく
?>
<?php echo nl2br(get_trim_str(array('str'=>$my_content, 'len'=>100,'echo' => false))); ?>
//改行されてるところにbrを挿入する さらに独自関数 get_trim_strを使う echoをファルスにする。
http://qiita.com/hisa_k/items/cb96565068426e261690
ファンクション
<?php
/**
* @function get_trim_str
* @param str(string), len(int), suffix(string), echo(bool)
* @return string
*/
if(!function_exists('get_trim_str')){
function get_trim_str($args = ''){
$defaults = array(
'str' => '',
'len' => 30,
'suffix' => '..',
'echo' => true
);
$args = wp_parse_args($args, $defaults);
extract($args, EXTR_SKIP);
$str = esc_html($str);
$len = intval($len);
$suffix = esc_html($suffix);
if(mb_strlen($str, 'UTF-8') > $len){
$output = mb_substr($str, 0, $len, 'UTF-8').$suffix;
}else{
$output = $str;
}
if($echo){
echo $output;
}else{
return $output;
}
}
}
?>
パラメータ
str(取り出したい文字列を含む文字列)
len(取り出す文字数を指定)
30(正数)初期値
suffix(接尾辞)
..(文字列)初期値
echo (出力)
1(true)初期値
2(false)
使用例
抜粋の文字数を指定して表示したい場合
<?php get_trim_str(array('str'=>get_the_excerpt(), 'len'=>100)); ?>
<?php get_trim_str(array('str'=>get_post_meta($post->ID,"jirei_disp",true), 'len'=>100)); ?>
<div style="text-align:right; padding-right:40px; margin-top:-20px; font-size:10px; color:#E7AC80; margin-bottom:70px;">名古屋市昭和区御器所の美容室/ヘアサロン/美容院 R advance</div>
ブログ投稿の
上と下に
アーカイブ シングルとも