提取某些帖子作为WordPress的摘录

时间:2013-06-03 11:51:11

标签: wordpress wordpress-theming

我有一个WordPress网站,我在其中创建了多个帖子并将它们绑定到某个类别。

每个帖子有6个段落。

以下是我要实现的目标:

我创建了一个儿童wordpress主题文件,我在其中检索特定类别的所有帖子。

我想在每个帖子中显示第3段(最多40个字)作为摘录。

任何人都可以帮我实现这个目标吗?

任何帮助将不胜感激......

谢谢

编辑:

其中一篇帖子的内容:

<div id="grey">
<div id="title">
<h1 id="divtest">Title</h1>
</div>
<div id="divContentText1"><a href="#" class="button5"><span class="button5_hover"></span><strong>Back To Test Page</strong></a></div>
</div>
<div class="wrap" id="divPgInfo">
<div id="divInnerImgSliderWrap">[meteor_slideshow slideshow="test"]</div>
<div id="divImgTxtWrap">
<h4 class="innerSlideHd">Test Title</h4>
<p class="innerSlideText">It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English.</p>
<p class="innerSlideText">Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).</p>
<p class="innerSlideText">Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).</p>
<p class="innerSlideText">Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).</p>
<p class="innerSlideText">Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).</p>
</div>
<div class="divClr"></div>
</div>
<div class="divClr"></div>

现在我要显示句子“这是一个历史悠久的......”的内容。

任何帮助将不胜感激....

真的很感谢你......

3 个答案:

答案 0 :(得分:1)

在wordpress支持的You Post Editor屏幕

在“摘录”文本区域中写下您的第3段。 (从编辑屏幕页面顶部启用)

要在fronend中使用the_excerpt函数显示此内容。

答案 1 :(得分:1)

受到这个问题的启发以及Wordpress'excerpt通常不够灵活以满足用户需求的事实,我记下了一个允许在帖子内容中使用任何HTML标签的功能作为这个任意的帖子摘录。< / p>

请注意,该函数可以获得一些改进灵活性的工作,例如,您可以完全用这个自定义摘录替换摘录,或者可能实现一些其他参数以允许更深入的搜索(类似于标记摘录段落的内容) id="excerpt")。

然而,我找到了解决问题的具体方案,并详细阐述了一些灵活性。您可以将此代码放在functions.php

/*
 * Defines an arbitrary excerpt by using any post content HTML element
 *
 * The function accepts an optional array of arguments.
 * Optional $args contents:
 *
 *     * id  - The id of the post we want the excerpt of. Defaults to the current post.
 *     * tag - The HTML tag we want to use as an excerpt.
 *     * idx - The index of that tag as calculated considering the post_content as the root.
 *
 * @param array|string $args See above description.
 */

function my_get_the_excerpt($args = array()) {

    $defaults = array(
        'id'  => null,
        'tag' => 'p',
        'idx' => 0
    );

    $args = wp_parse_args($args, $defaults);
    $args = (object) $args;

    $post = get_post($args->id);

    if ( post_password_required($post) ) {

        return __( 'There is no excerpt because this is a protected post.' );

    }

    $post_content = '<html>' . apply_filters('the_content', $post->post_content) . '</html>';
    $post_html = new DOMDocument();
    $post_html->loadHTML($post_content);
    $paragraphs = $post_html->getElementsByTagName($args->tag);

    return $post_html->saveHTML($paragraphs->item($index));

}

完成此操作后,您可以在模板中使用此功能而不是the_excerpt来解决问题。像这样:

// This gets the third paragraph of your post.
echo my_get_the_excerpt( array( 'idx' => 2 ) )

答案 2 :(得分:0)

将特定ID放在要用作摘录的段落的

标记上,当您想要使用摘录时,只需解析文章的内容并仅提取< / p>

您已确定的身份。

检查一下:Get content within a html tag using php and replace it after processing