Bezpečné vytváření vlastních funkcí ve functions.php

aneb

**Fatal error: Cannot redeclare function() in /somefile.php on line 1**

Při vytváření vlastních funkcí pro vaši šablonu, zvláště pak, pokud se snažíte vytvořit odvozenou šablonu, anglicky child theme, můžete na výše zmíněnou chybu narazit.
Znamená to jediné, funkce, kterou jste pro svou šablonu vytvořili, již existuje. Takže buď se snažíte vytvořit funkci s názvem, který nějaká nativní PHP či WordPress funkce již využívá, nebo je vytvořena uživatelem právě v souboru functions.php vaší šablony, nebo šablony, kterou využíváte pro svou odvozenou šablonu.

Problém s funkcí query_post při stránkování

Jestliže v šabloně z nějakých důvodů nevystačíme se standardním načtením článků pomocí konstrukce

/—code php
if (have_posts()) : while (have_posts()) : the_post();
\—

a potřebujeme využít funkci „query_posts()“:http://codex.wordpress.org/Function_Reference/query_posts například pro vyloučení některých kategorií, tagů, autora a podobně, typicky v podobě:

/—code php
// výběr článků z vybraných rubrik
query_posts(‚cat=2,6,17,38‘);

// výběr článků patřících do několika rubrik současně
query_posts(array(‚category__and‘ => array(2,6)));

// výběr článků s vyloučením určité rubriky
query_posts(‚cat=-3‘);
\—

Rychle zjistíme, že tento postup sice vede k cíli, nicméně **přestane fungovat stránkování** a tím i pluginy jako např. výborný „WP-PageNavi“:222.

Náprava je jednoduchá:

/—code php
if (have_posts()):

// obnovíme parametr ‚paged‘
$paged = (get_query_var(‚paged‘)) ? get_query_var(‚paged‘) : 1;
query_posts(„cat=-3&paged=$paged“);

// a pak už pokračujeme standardním
while (have_posts()) : the_post();
\—

Viz též „Separatista“:http://www.separatista.net/wordpress/tipy-a-triky/problemy-se-strankovanim-prispevku-pri-pouziti-funkce-query_posts nebo „David Binda“:http://david.binda.cz/wordpress-nefunkcni-strankovani-v-dusledku-query_posts.

„Properly using wp_reset_postdata(), wp_reset_query() and variable naming in queries »“:http://www.poststat.us/properly-reset-wordpress-query/

„How to fix pagination for custom loops »“:http://wordpress.stackexchange.com/questions/120407/how-to-fix-pagination-for-custom-loops

Zobrazení obsahu pouze přihlášeným uživatelům

Do souboru **functions.php** (Vzhled > Upravit soubory) stačí přidat následující:

/—code php
add_shortcode( ‚member‘, ‚member_check_shortcode‘ );

function member_check_shortcode( $atts, $content = null ) {
if ( is_user_logged_in() && !is_null( $content ) && !is_feed() )
return $content;
return “;
}
\—

Pak už stačí kdekoli v příspěvku „obalit“ text určený pouze registrovaným uživatelům tagem [member]:

/—code php
[member]
Toto se zobrazí pouze přihlášeným uživatelům
[/member]
\—

Je-li potřeba to nastavit už v šabloně vzhledu, je třeba použít

/—code php

\—

„Více příkladů »“:http://justintadlock.com/archives/2009/05/09/using-shortcodes-to-show-members-only-content

Jak modifikovat vzhled a délku anotace

Zdroj: „Improving WordPress’ the_excerpt() template tag“:http://www.aaronrussell.co.uk/blog/improving-wordpress-the_excerpt/

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/formatting.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)

/—code php
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**.

/—code php
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/Customizing_the_Read_More“:http://codex.wordpress.org/Customizing_the_Read_More

změna textu odkazu:

/—code php

\—

„Useful ways to customize and format the WordPress more tag »“:http://digwp.com/2010/01/wordpress-more-tag-tricks/

**Advanced Excerpt Plugin**
„http://wordpress.org/extend/plugins/advanced-excerpt/“:http://wordpress.org/extend/plugins/advanced-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_excerpt() for even more control (see the „FAQ“:http://wordpress.org/extend/plugins/advanced-excerpt/faq/)