0

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

每个帖子有6个段落。

这是我想要实现的目标:

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

我想将每个帖子的第三段(最多 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>

现在我想展示句子“It is a long established....”中的内容

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

真的很谢谢你...

4

3 回答 3

1

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

请注意,该函数可以做一些工作来提高灵活性,例如,您可以完全用这个自定义摘录替换摘录,或者可能实现一些其他参数以允许更深入的搜索(例如用 标记摘录段落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 ) )
于 2013-06-03T17:49:15.007 回答
1

在 Wordpress 支持的您发布编辑器屏幕中

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

要在前端显示此内容,请使用the_excerpt函数。

于 2013-06-03T11:57:25.307 回答
0

将特定的 ID 放在

您想用作摘录的段落的标签,当您想使用摘录时,只需解析文章的内容并仅提取您的部分

用你确定的id。

检查这个:使用php获取html标签中的内容并在处理后替换它

于 2013-06-03T12:16:54.213 回答