shortcode — Jak wyświetlić listę stron podrzędnych z fragmentami, np [child-pages depth=”1″ excerpt=”1″]
Kiedy tworzę skróty z dużą ilością kodu HTML, używam dwóch funkcji. Jedna funkcja szablonu, która zawiera cały kod skrótu. I jedna prosta funkcja do tworzenia krótkiego kodu z pierwszej funkcji. Zwykle używam tej konfiguracji, ponieważ funkcja shortcode musi return
Treść.
Najpierw musimy stworzyć funkcję szablonu, która przechowuje kod HTML, a także atrybuty shortcode.
function child_pages_shortcode_template( $atts ) {
// Attributes
// these are the attributes you can insert in the shortcode
// i.e. [show_child_pages sort_order="desc"]
// see codex for get_pages for all attributs
// these are also used as default attribute settings
$atts = shortcode_atts(array(
'sort_order' => 'asc',
'sort_column' => 'post_title',
'show_excerpt' => 1, // excerpt displays as default, set to any other number to disable
), $atts );
extract( $atts );
// get ID of current page
$current_page_id = get_the_ID();
// set the ID of the current page as child_of parameter
// that means we are only showing children of the current page
$args = array(
'child_of' => $current_page_id,
'parent' => $current_page_id, // add this to only show 1 level deep, remove to also show grand children
'post_type' => 'page',
'sort_order' => $sort_order, //this comes from the shortcode_atts above, so you can change it with the shortcode
'sort_column' => $sort_column, //this comes from the shortcode_atts above, so you can change it with the shortcode
);
$child_pages = get_pages( $args );
// only start if we have some children
if ($child_pages) {
echo '<ul>';
foreach ($child_pages as $child_page) {
echo '<li>';
$child_page_title = $child_page->post_title; // get children page title
$child_page_excerpt = $child_page->post_excerpt; // get children page excerpt
$child_page_id = $child_page->ID; // get children ID to create the permalink
$child_page_permalink = get_permalink($child_page_id); // get permalink to children page
echo '<h3><a href="'.$child_page_permalink.'">'.$child_page_title.'</a></h3>';
// only show excerpt if $show_excerpt is 1
if ($show_excerpt == 1) {
echo '<p>'.$child_page_excerpt.'</p>';
}
echo '</li>';
}
echo '</ul>';
}
}
Następnie pobieramy pierwszą funkcję i tworzymy z niej krótki kod:
function child_pages_shortcode($atts, $content = null){
ob_start();
$content = child_pages_shortcode_template($atts);
$content = ob_get_contents();
ob_end_clean();
return $content;
}
add_shortcode('show_child_pages', 'child_pages_shortcode');
Teraz możesz użyć np [show_child_pages show_excerpt="0"]
Z ob_start()
, ob_get_contents()
I ob_end_clean()
możemy przechwycić zawartość naszej pierwszej funkcji. Tylko z tą funkcją nasz shortcode zostanie przetworzony. Zawartość naszej pierwszej funkcji zostanie tutaj zbuforowana.
Kilka wyjaśnień:
$atts = shortcode_atts(array(
'sort_order' => 'asc',
'sort_column' => 'post_title',
'show_excerpt' => 1, // excerpt displays as default, set to any other number to disable
), $atts );
extract( $atts );
To są wszystkie atrybuty shortcode. Jak widać, możemy tutaj mieszać atrybuty. Na przykład, sort_order
I sort_column
to atrybuty, które wstawimy do naszego get_pages()
zapytanie jako argumenty. Ale show_excerpt
jest tylko atrybutem niestandardowym. W dalszej części kodu możemy sprawdzić czy i co zostało wpisane jako atrybut.
if ($show_excerpt == 1) {
echo '<p>'.$child_page_excerpt.'</p>';
}
Tutaj po prostu sprawdzamy, czy atrybut $show_excerpt
jest ustawiona na 1. Jeśli wprowadzono jakąkolwiek inną liczbę lub ciąg znaków, w ogóle nie wyświetlamy fragmentu.
$args = array(
'child_of' => $current_page_id,
'parent' => $current_page_id, // add this to only show 1 level deep, remove to also show grand children
'post_type' => 'page',
'sort_order' => $sort_order, //this comes from the shortcode_atts above, so you can change it with the shortcode
'sort_column' => $sort_column, //this comes from the shortcode_atts above, so you can change it with the shortcode
);
To są argumenty naszej funkcji get_pages. Z 'child_of' => $current_page_id,
upewniamy się, że wyświetlamy tylko elementy podrzędne bieżącej strony.
Z 'parent' => $current_page_id,
możemy wpływać na głębokość. Jeśli ustawimy ją również na bieżącą stronę, to żadne wnuki nie zostaną wyświetlone. Musimy użyć tego jako get_pages
nie mam depth
argument.