0

複数の投稿を作成し、それらを特定のカテゴリにバインドした WordPress Web サイトがあります。

各投稿には 6 つの段落があります。

これが私が達成しようとしていることです:

特定のカテゴリからすべての投稿を取得する子ワードプレス テーマ ファイルを作成しました。

各投稿の第 3 段落 (最大 40 ワード) を抜粋として表示したい。

誰でも私がこれを達成するのを助けることができますか?

どんな助けでも大歓迎です...

ありがとうございました

編集:

投稿の1つの内容:

<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>

では、「老舗ですね・・・」という文から内容をお見せしたいと思います。

どんな助けでも大歓迎です....

本当にありがとうございました...

4

3 に答える 3

1

この質問と、Wordpressexcerptがユーザーのニーズに対して十分な柔軟性を備えていないことが多いという事実に触発されて、投稿コンテンツ内で任意の 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 に答える
0

特定の ID を

抜粋として使用する段落のタグと、抜粋を使用する場合は、記事のコンテンツを解析して、その部分のみを抽出するだけです

あなたが決めたIDで。

これをチェックしてください:phpを使用してhtmlタグ内のコンテンツを取得し、処理後にそれを置き換えます

于 2013-06-03T12:16:54.213 に答える