0

I have a WordPress website, in which I have created multiple posts and bound them to a certain category.

Each posts has 6 paragraphs.

Here is what I am trying to achieve:

I have created a child wordpress theme file, in which I am retrieving all the posts from a particular category.

I want to display the 3rd paragraph from each posts ( upto 40 words ) as an excerpt.

Anyone can help me to achieve this?

Any help will be appreciated...

Thank You

Edited:

Contents of one of the posts:

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

Now I want to show the contents from the sentence "It is a long established...."

Any help will be appreciated....

Really Thank you...

mkb
  • 81
  • 4
  • 5
  • 20
  • So far I have tried using WordPress in-build the_excerpt function. I dnt know how to tell the_excerpt() to start from the 3rd paragraph. – mkb Jun 03 '13 at 11:54

3 Answers3

1

In You Post Editor screen at wordpress backed

Write your 3rd paragraph in "excerpt" text area. (Enable it from top of edit screen page)

For display this content in fronend use the_excerpt function.

Denish
  • 2,800
  • 2
  • 23
  • 33
  • thanks for the super solution. Just wanted to know if there a way to extract it from the content area itself??? This will help the website owner from adding data twice.. – mkb Jun 03 '13 at 12:01
  • mkb, It is possible to extract it from Content are itself, but it should be fix like 1st Para : 50 words 2nd Para: 40 words 3rd Para: 30 words so by using substr function (String Function) you can make sub string of 3rd Para only – Denish Jun 03 '13 at 12:05
  • When using the excerpt text area from wordpress backend, no read more link can be added.... – mkb Jun 03 '13 at 18:19
1

Inspired by this question and by the fact that Wordpress' excerpt is often not flexible enough for user needs, I jotted down a function allowing to use any HTML tag inside the post content as this arbitrary post excerpt.

Mind that the function can get some work to improve flexibility, for example you could substitute the excerpt with this custom excerpt altogether, or perhaps implement some other parameter to allow a more in-depth searching (something like marking the excerpt paragraph with an id="excerpt").

I went for a specific solution to your problem, however, and elaborated a bit more to allow some flexibility. You can put this code in your 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));

}

After doing that, in your case you could solve your problem by simply using this function in your template, instead of the_excerpt. Like so:

// This gets the third paragraph of your post.
echo my_get_the_excerpt( array( 'idx' => 2 ) )
Sunyatasattva
  • 5,619
  • 3
  • 27
  • 37
  • Thanks for the solution... I have wrapped the paragraph within multiple divs..... Unable to make this function work... What am i doing wrong??? Plz help.... – mkb Jun 03 '13 at 18:11
  • Without seeing exactly how your `post_content` looks like I am not able to help you much. Edit your question adding your post content and perhaps debug a little and tell me when it goes wrong. – Sunyatasattva Jun 03 '13 at 18:29
  • I have updated my posts with one of the post content.... i want to display from the sentence from "It is a long established..." Can you please help.. Really appreciated the support.... – mkb Jun 03 '13 at 18:42
  • If I copy+paste your edit in the HTML section of my post content, and then add to my template just `echo my_get_the_excerpt()` I get the result you are looking for. What is not working for you? – Sunyatasattva Jun 03 '13 at 18:50
  • Sorry for not replying back... I will retry your code and get back to you tomorrow... – mkb Jun 05 '13 at 12:30
0

Put a specific ID on the

tag of the paragraph you want to use as an excerpt and when you want to use the excerpt just parse the content of the article and extract only the part in your

with the id you've determined.

check this : Get content within a html tag using php and replace it after processing

Community
  • 1
  • 1