最初の見出しまでの文章を取得する方法:WordPress
WordPressで最初の見出しまでの導入文をdescriptionとして自動で取得したかったので、その方法をメモ。
descriptionを書き出しているheader.php等に
<?php $content = get_the_content($pid); $end = strpos($content, "<h2"); $endcont = substr($content, 0, $end); $desctxt = strip_tags( strip_shortcodes( $endcont ) ); ?> <meta name="description" content="<?php echo str_replace("\r\n", '', $desctxt); ?>">
でOK
$end = strpos($content, "<h2");
の箇所は、見出しをh3にしているならh3に変更
$end = strpos($content, "<h2"); $endcont = substr($content, 0, $end);
↑この部分で最初の
WordPressのおすすめ参考書
bookfan 1号店 楽天市場店
¥3,300 (2023/11/24 17:19時点 | 楽天市場調べ)
ポチップ
ポチップ
bookfan 1号店 楽天市場店
¥3,300 (2023/11/24 17:19時点 | 楽天市場調べ)