限制帖子摘录并在Wordpress中发布内容

时间:2015-09-01 10:26:16

标签: php wordpress

我有以下剪辑:

   <html xmlns="http://www.w3.org/1999/xhtml">
       <head runat="server">
        <title></title>
    <script src="../Scripts/jquery-2.1.4.min.js"></script>
    <script>
        $(document).ready(function () {
        $("#button1").click(function () {
            $("#div1").empty();
        });
    });
</script>
</head>
<body>
<form id="form1" runat="server">
    <div id="div1" style="background-color: lightpink; height: 200px; width: 300px;">
        <p>
            This is first paragraph
        </p>
        <p>
            This is second paragraph
        </p>
        <p>
            This is third paragraph
        </p>
    </div>
    <asp:Button ID="button1" runat="server" Text="Remove Content" />
</form>

这是display_post_excerpt函数:

<div class="content">
                            <h2><?php echo $article->post_title ?></h2>
                            <h3><?php echo get_field( 'subtitle', $article->ID ); ?></h3>
                            <p><?php echo display_post_excerpt( $article->post_excerpt, $article->post_content ); ?></p>

                        </div>

}

我想限制帖子摘录(如果帖子没有摘录,也要限制内容)。我不知道我应该怎么做以及在哪里(在function.php?模板中?我想我应该使用类似的东西:

function display_post_excerpt( $excerpt, $content ){
    if( $excerpt != '' ){
        $text = $excerpt;
    }else{
        $text = $content;
    }

    $text = strip_tags( $text );

    return $text;

但由于我是一个大菜鸟,我不确定。 你能帮帮忙吗?

2 个答案:

答案 0 :(得分:1)

function display_post_excerpt( $excerpt, $content ){
    if( $excerpt != '' ){
        $text = $excerpt;
        if (strlen($text) > 20) {
          $text = substr($text,0,strpos($text,' ',20)) . ' ... <a href="' . get_permalink() . '">[ read more ]</a>'; } ;
         }
         else{
            $text = $content;
         }
    }

    return $text;

}

答案 1 :(得分:0)

我认为更好的解决方案是在代码下面添加functions.php文件:

function wpt_excerpt_length($length) {
    return 16;
  }
  add_filter('excerpt_length', 'wpt_excerpt_length', 999);

您可以在此处详细了解摘录Function Reference/the excerpt

相关问题