I have ripped this out of one of my Funbox Child themes and stitched into a WordPress plugin. Enjoy!
The list pages shortcode is designed to allow you to put a list of pages into a page or post. This can create a table of contents to show child pages or a list of child pages for a particular page. The page list created dynamically so the links and page titles should always be updated to the content saved in WordPress.
The simplest example is to just use the following shortcode on a page that has sub-pages or child pages. This will create a simple list with a Contents header.
[shortcode][list-pages][/shortcode]
The page list can be manipulated with some attributes to do more complicated page lists.
[shortcode][list-pages child_of=0 toc_title_text="About & Stuff"][/shortcode]
[list-pages child_of=0 toc_title_text="About & Stuff"]
The list is more complicated, and you may need help to determine the proper ID number, but this can be useful to list pages without having to manually create links. Here is a list of the attributes and short descriptions.
- show_date – Shows the date the page was published.
- date_format – Sets the date for show_date.
- depth -Hierarchy depth (default of 0 is infinite).
- child_of -The page ID to use as the main parent (default is the current page).
- exclude -List of page ID numbers separated by commas to exclude.
- include – List of page ID numbers spearated by commas to include.
- title_li – Puts a line of text and an additional list wrapped around the page list.
- authors – To show pages by a certain author.
- sort_column – Sets the order of the pages (default is page order then alphabetically by title).
- link_before – Adds extra text or HTML before links.
- link_after – Adds extra text or HTML after links.
- exclude_tree – List of page ID numbers separated by commas that will exclude the parent and all child pages of that parent.
- toc_container_tag – Sets the container HTML tag for the whole page list (default is a DIV).
- toc_container_class – Sets the HTML CSS class for the container tag (default is list-pages-container).
- toc_title_tag – Sets the HTML tag used for the title of the whole page list (default is H3).
- toc_title_class – Sets the HTML CSS class for the title HTML tag (default is list-pages-title).
- toc_title_text – Sets the text used as the title for the whole page list (default is Contents).
- toc_wrapper_tag – Sets the tag wrapped around the list of pages (default is UL).
To use make a WordPress plugin file out of the following PHP source code. For easy organization, I’d make a file at /wp-content/plugins/ftf-list-pages/ftf-list-pages.php. Upload it to your WordPress site and activate. I’d put this in the WordPress plugin repository, but I’d prefer to put a nice editor interface on it.
<?php
/*
Plugin Name: FTF List Pages Shortcode
Plugin URI: https://swampthings.org/projects/
Description: A shortcode for listing page lists and child pages.
Author: Jess Planck
Version: 1.0
Author URI: http://swampthings-docker-local.ddev.site
Copyright (c) Jess Planck (http://swampthings-docker-local.ddev.site)
THIS is released under the GNU General Public
License: http://www.gnu.org/licenses/gpl.txt
This is a WordPress plugin (http://wordpress.org). WordPress is
free software; you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at your
option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
For a copy of the GNU General Public License, write to:
Free Software Foundation, Inc.
59 Temple Place, Suite 330
Boston, MA 02111-1307
USA
You can also view a copy of the HTML version of the GNU General
Public License at http://www.gnu.org/copyleft/gpl.html
*/
/**
* Funbox HTML Tag
*
* Core utility function for the writing and manipulation of HTML tags.
*
* @since 1.0
* @echo string
*/
if ( !function_exists( 'ftf_html_tag' ) ) {
function ftf_html_tag( $html = array() ) {
$attributes = '';
$composite = '';
$spacer = '';
if ( !isset( $html['return'] ) ) $html['return'] = false;
$reserved = array(
'tag', 'tag_type', 'attributes', 'tag_content', 'tag_content_before', 'tag_content_after', 'return'
);
foreach ( $html as $name => $option ) {
if ( in_array( $name, $reserved ) ) continue;
$attributes .= $name . '="' . $option . '" ';
}
if ( isset( $html['attributes'] ) ) $attributes .= $html['attributes'] . ' ' . $attributes;
if ( $attributes != '' ) {
$attributes = rtrim( $attributes );
$spacer = ' ';
}
if ( !isset( $html['tag_type'] ) ) $html['tag_type'] = 'default';
if ( isset( $html['tag_content_before'] ) ) $composite .= $html['tag_content_before'];
switch ( $html['tag_type'] ) {
case 'single':
if ( isset( $html['tag_content'] ) ) $composite .= $html['tag_content'];
if ( isset( $html['tag'] ) ) $composite .= '<' . $html['tag'] . $spacer . $attributes . '/>';
break;
case 'open':
if ( isset( $html['tag'] ) ) $composite .= '<' . $html['tag'] . $spacer . $attributes . '>';
if ( isset( $html['tag_content'] ) ) $composite .= $html['tag_content'];
break;
case 'close':
if ( isset( $html['tag_content'] ) ) $composite .= $html['tag_content'];
if ( isset( $html['tag'] ) ) $composite .= '</' . $html['tag'] . '>';
break;
case 'attributes':
$composite = $attributes;
break;
case 'default':
if ( isset( $html['tag'] ) ) $composite .= '<' . $html['tag'] . $spacer . $attributes . '>';
if ( isset( $html['tag_content'] ) ) $composite .= $html['tag_content'];
if ( isset( $html['tag'] ) ) $composite .= '</' . $html['tag'] . '>';
break;
}
if ( isset( $html['tag_content_after'] ) ) $composite .= $html['tag_content_after'];
if ( $html['return'] == true ) return $composite ;
echo $composite;
}
}
/*
* Page list shortcode
*
* Creates lists of pages especially good for creating
* table of contents for child pages.
* [column width= padding= class= style=] content [/column]
* Use width and padding for style attribute columns
* Use style to control full div style attribute (default is below)
* eliminate all and use class to make custom css controled columns
*
* @since 1.0
*/
if ( !function_exists( 'ftf_list_pages_shortcode' ) ) {
function ftf_list_pages_shortcode( $atts, $content = null ) {
// Defaults
$the_toc_arr_defaults = array(
'depth' => 0,
'show_date' => false,
'date_format' => get_option('date_format'),
'child_of' => get_the_id(),
'exclude' => false,
'include' => false,
'title_li' => '',
'authors' => false,
'sort_column' => 'menu_order, post_title',
'link_before' => false,
'link_after' => false,
'exclude_tree' => false,
'toc_container_tag' => 'div',
'toc_container_class' => 'list-pages-container',
'toc_title_tag' => 'h3',
'toc_title_class' => 'list-pages-title',
'toc_title_text' => 'Contents',
'toc_wrapper_tag' => 'ul'
);
$toc_settings = array();
$toc_query = array();
$the_toc_arr = shortcode_atts( $the_toc_arr_defaults, $atts );
foreach( $the_toc_arr as $the_toc_key => $the_toc_value ) {
if ( !$the_toc_value && $the_toc_key != 'title_li' ) continue;
if ( !strstr( $the_toc_key, 'toc' ) )
$toc_query[$the_toc_key] = $the_toc_value;
else
$toc_settings[$the_toc_key] = $the_toc_value;
}
if ( isset( $toc_settings['toc_title_text'] ) ) {
$toc_title_defaults = array(
'tag' => $toc_settings['toc_title_tag'],
'tag_content' => $toc_settings['toc_title_text'],
'return' => true
);
if ( isset( $toc_settings['toc_title_class'] ) ) $toc_title_defaults['class'] = $toc_settings['toc_title_class'];
$toc_title = ftf_html_tag( $toc_title_defaults );
}
// Never Echo
$toc_query['echo'] = 0;
$toc_list = wp_list_pages( $toc_query );
$toc = ftf_html_tag( array(
'tag' => $toc_settings['toc_wrapper_tag'],
'tag_content' => $toc_list,
'return' => true
) );
if ( isset( $toc_settings['toc_container_tag'] ) ) {
$toc_defaults = array(
'tag' => $toc_settings['toc_container_tag'],
'return' => true
);
if ( isset( $toc_settings['container_class'] ) ) $toc_defaults['class'] = $toc_settings['toc_container_class'];
$toc_defaults['tag_content'] = $toc_title . $toc;
$contents = ftf_html_tag( $toc_defaults );
} else {
$contents = $toc_title . $toc;
}
return $contents;
}
add_shortcode('list-pages', 'ftf_list_pages_shortcode');
}
?>
Leave a Reply