Užitečné pluginy, návody a šablony pro WordPress CMS

Jak modifikovat vzhled a délku anotace

Zdroj: Improving WordPress’ the_excerpt() template tag

WordPress’ the_excerpt() template tag is used in most themes for browsing the archives and categories of a blog. Rather than displaying the full content of the post, the excerpt displays a short snippet of the content. Unless you manually enter in an excerpt when writing each post, WordPress grabs the first 55 words of the post and uses that as the excerpt.

So far so good, but there are problems with the way WordPress does this. These include:

  • Word count – 55 words is a good number, but what if you want more or less?
  • Formatting – WordPress strips out all HTML tags. This gets rid of images and links, but can also get rid of paragraph formatting, making the entire excerpt one long paragraph without any line breaks.
  • JavaScript – Unfortunately JavaScript isn’t stripped out, which can result in some plugins’ messy script appearing in your excepts. Not only does this look rubbish, it can be a vulnerability too.

Stačí z wp-includes/format­ting.php zkopírovat funkci wp_trim_excerpt, přejmenovat ji třeba na better_excerpt a vložit ji do functions.php (Vzhled > Upravit soubory)

function better_excerpt($text) {
        global $post;
        if ( '' == $text ) {
                $text = get_the_content('');
                $text = apply_filters('the_content', $text);
                $text = str_replace(']]>', ']]>', $text);
                $text = strip_tags($text); // můžu zakázat
                $excerpt_length = 55; // můžu změnit délku
                $words = explode(' ', $text, $excerpt_length + 1);
                if (count($words)> $excerpt_length) {
                        array_pop($words);
                        array_push($words, '[...]');
                        $text = implode(' ', $words);
                }
        }
        return $text;
}

Na závěr je nutno na konec functions.php vložit ještě následující 2 řádky, které způsobí, že WP místo vestavěné funkce wp_trim_excerpt použije naši vylepšenou better_excerpt.

remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'better_excerpt');

Implementováno na tomto webu.

Customizing the Read More
http://codex.wordpress.org/…he_Read_More

změna textu odkazu:

<!-- more Tady tenhle článek pokračuje -->

Useful ways to customize and format the WordPress more tag »

Advanced Excerpt Plugin
http://wordpress.org/…ced-excerpt/

This plugin adds several improvements to WordPress' default way of creating excerpts.

  • Keeps HTML markup in the excerpt (and you get to choose which tags are included)
  • Trims the excerpt to a given length using either character count or word count
  • Only the ‚real‘ text is counted (HTML is ignored but kept)
  • Customizes the excerpt length and the ellipsis character that are used
  • Completes the last word or sentence in an excerpt (no weird cuts)
  • Adds a read-more link to the text
  • Ignores custom excerpts and use the generated one instead
  • Theme developers can use the_advanced_ex­cerpt() for even more control (see the FAQ)
OVX.cz 2003 | Přihlásit se
Bez cookies už dnes těžko provozovat plnohodnotný web. Setrváním zde tudíž vyjadřujete souhlas s jejich používáním. Více informací. | Zavřít.