douyue1926 2018-09-28 05:07
浏览 29
已采纳

输出Wordpress帖子中的第一段

I want to put just the first paragraph of my posts on my index.php

in my functions.php I have

<?php

{
global $post;

$output = get_the_content();

$wanted_number_of_paragraph = 1;

$tmp = explode ('</p>', $output);
for ($i = 0; $i < $wanted_number_of_paragraph; ++$i) {
if (isset($tmp[$i]) && $tmp[$i] != '') {
   $tmp_to_add[$i] = $tmp[$i];
}
}
$output = implode('</p>', $tmp_to_add) . '</p>';

echo $output;

}
?>

then in my index.php

<?php wpden_excerpt(); ?>

However it posts the entire post (including pictures) and not just the first paragraph.

  • 写回答

1条回答 默认 最新

  • douyangcheng4965 2018-09-28 06:18
    关注

    I wold recommend you to use the Wordpress get_extended() function for this purpose. In your post you split the content with the "more" tag, after inside your template you can have something like:

    global $post;
    // gets the content of your post as an array of 2 parts
    $content_parts = get_extended( $post->post_content ); 
    

    and after this you can echo the part before the "more" tag like:

    <?=$content_parts['main'];?>
    

    the part after the "more" tag you can echo like:

    <?=$content_parts['extended'];?>
    

    Also, if you chose this option I'll recommend you to check the wpautop() , since you may need to wrap these 2 parts with it, for example:

    <?=wpautop($content_parts['extended']);?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求用stm32f103c6t6在lcd1206上显示Door is open和password:
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类