Images at the top of the post

Added by George Notaras 11 months ago

Hello,

Thanks for the great plugin first. But I got some problem to have the meta description display correctly.

Since I usually post around 5-6 images before the article. It seems the plugin can't get the meta description if I do it in this way. Is it possible to ignore the <img> tag in the future version? Thanks a lot.

Max

Hi Max,

The algorithm which tries to retrieve a description of about 250 characters fails when there are a lot of <img> elements in the beginning of the post. I'll keep this in mind as it is a reasonable feature request.

Until then, please write a description by hand, or paste the first 1-2 sentences of the post in a custom field named "description". Note that it does not matter if the text contains HTML tags. They will be stripped.

Thanks for your feedback.

Note: This thread has been transfered from the G-Loaded forums or comment system.


Replies (3)

RE: Images at the top of the post - Added by A J 5 months ago

I have a simple fix for this issue. Just replace the entire functions below:

@function amt_get_the_excerpt($excerpt_max_len = 300, $desc_avg_length = 250, $desc_min_length = 150) {
/*
Returns the post's excerpt.
This was written in order to get the excerpt outside the loop
because the get_the_excerpt() function does not work there any more.
This function makes the retrieval of the excerpt independent from the
WordPress function in order not to break compatibility with older WP versions.

Also, this is even better as the algorithm tries to get text of average
length 250 characters, which is more SEO friendly. The algorithm is not
perfect, but will do for now.
*/
global $posts;
if ( empty($posts[0]->post_excerpt) ) {
/*
Get the initial data for the excerpt
*/
$amt_excerpt = strip_tags($posts[0]->post_content);
$amt_excerpt = preg_replace('/&lt;a(.*?)&gt;(.*?)<\/a>/', "\\2", $amt_excerpt); // remove links
$amt_excerpt = preg_replace('|\[(.+?)\](.+?\[/\\1\])?|s', '', $amt_excerpt); // remove WP caption code
$amt_excerpt = strip_tags(substr($amt_excerpt, 0, $excerpt_max_len));
/*
If this was not enough, try to get some more clean data for the description (nasty hack)
*/
if ( strlen($amt_excerpt) < $desc_avg_length ) {
$amt_excerpt = strip_tags(substr($amt_excerpt, 0, (int) ($excerpt_max_len * 1.5)));
if ( strlen($amt_excerpt) < $desc_avg_length ) {
$amt_excerpt = strip_tags(substr($amt_excerpt, 0, (int) ($excerpt_max_len * 2)));
}
}
$end_of_excerpt = strrpos($amt_excerpt, ".");
if ($end_of_excerpt) {
/*
if there are sentences, end the description at the end of a sentence.
*/
$amt_excerpt_test = substr($amt_excerpt, 0, $end_of_excerpt + 1);
if ( strlen($amt_excerpt_test) < $desc_min_length ) {
/*
don't end at the end of the sentence because the description would be too small
/
$amt_excerpt .= "...";
} else {
/

If after ending at the end of a sentence the description has an acceptable length, use this
/
$amt_excerpt = $amt_excerpt_test;
}
} else {
/

otherwise (no end-of-sentence in the excerpt) add this stuff at the end of the description.
*/
$amt_excerpt .= "...";
}
} else {
/*
When the post excerpt has been set explicitly, then it has priority.
*/
$amt_excerpt = $posts[0]->post_excerpt;
}
return $amt_excerpt;
}@

Hope that helps - would be great to see this (or similar) in the main code.

RE: Images at the top of the post - Added by A J 5 months ago

Sorry - don't know why the function was split apart like that - this should be easier to copy:

function amt_get_the_excerpt($excerpt_max_len = 300, $desc_avg_length = 250, $desc_min_length = 150) {
    /*
    Returns the post's excerpt.
    This was written in order to get the excerpt *outside* the loop
    because the get_the_excerpt() function does not work there any more.
    This function makes the retrieval of the excerpt independent from the
    WordPress function in order not to break compatibility with older WP versions.

    Also, this is even better as the algorithm tries to get text of average
    length 250 characters, which is more SEO friendly. The algorithm is not
    perfect, but will do for now.
    */
    global $posts;

    if ( empty($posts[0]->post_excerpt) ) {

        /*
        Get the initial data for the excerpt
        */

        $amt_excerpt = strip_tags($posts[0]->post_content);
        $amt_excerpt = preg_replace('/<a(.*?)>(.*?)<\/a>/', "\\2", $amt_excerpt); // remove links        
        $amt_excerpt = preg_replace('|\[(.+?)\](.+?\[/\\1\])?|s', '', $amt_excerpt); // remove WP caption code

        $amt_excerpt = strip_tags(substr($amt_excerpt, 0, $excerpt_max_len));

        /*
        If this was not enough, try to get some more clean data for the description (nasty hack)
        */
        if ( strlen($amt_excerpt) < $desc_avg_length ) {
            $amt_excerpt = strip_tags(substr($amt_excerpt, 0, (int) ($excerpt_max_len * 1.5)));
            if ( strlen($amt_excerpt) < $desc_avg_length ) {
                $amt_excerpt = strip_tags(substr($amt_excerpt, 0, (int) ($excerpt_max_len * 2)));
            }
        }

        $end_of_excerpt = strrpos($amt_excerpt, ".");

        if ($end_of_excerpt) {
            /*
            if there are sentences, end the description at the end of a sentence.
            */
            $amt_excerpt_test = substr($amt_excerpt, 0, $end_of_excerpt + 1);

            if ( strlen($amt_excerpt_test) < $desc_min_length ) {
                /*
                don't end at the end of the sentence because the description would be too small
                */
                $amt_excerpt .= "...";
            } else {
                /*
                If after ending at the end of a sentence the description has an acceptable length, use this
                */
                $amt_excerpt = $amt_excerpt_test;
            }
        } else {
            /*
            otherwise (no end-of-sentence in the excerpt) add this stuff at the end of the description.
            */
            $amt_excerpt .= "...";
        }

    } else {
        /*
        When the post excerpt has been set explicitly, then it has priority.
        */
        $amt_excerpt = $posts[0]->post_excerpt;
    }

    return $amt_excerpt;
}

RE: Images at the top of the post - Added by George Notaras 5 months ago

Thanks for the fix. I will include it in the next release.

(1-3/3)