0

So I have decided on using the Child Pages Shortcode plugin, a plugin that pulls the ID, tittle, thumbnail, excerpt, etc of the child pages. The plugins author explains that he encourages customization of his plugin.

The templates author describes that you should be able to build your own template, something that I don´t understand to 100%, but I have been able to change the existing template so that it generates <li> of the individual children. Now I need to add a container for all of my list items, it would look something like

<div class="row-fluid">
<ul class="thumbnails">
       <li class="span3">
       <li class="span3">
       <li class="span3">
       <li class="span3">
    </ul>
</div>

EDIT: I added the suggested changes, but it gives me the following error: Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'wpse_113253_child_pages_shortcode_template' not found or invalid function name....

Source code of the Child Pages Shortcode plugin with my customizations and the suggested fix:

<?php
/*
Plugin Name: Child Pages Shortcode
Author: Takayuki Miyauchi
Plugin URI: http://wpist.me/wp/child-pages-shortcode/
Description: You can use shortcode for display child pages from the page.
Version: 1.7.0
Author URI: http://wpist.me/
Domain Path: /languages
Text Domain: child-pages-shortcode
*/

new childPagesShortcode();

class childPagesShortcode {

private $ver = '1.1.4';

function __construct()
{
    add_shortcode("child_pages", array(&$this, "shortcode"));
    add_action("init", array(&$this, "init"));
    add_action("wp_enqueue_scripts", array(&$this, "wp_enqueue_scripts"));
    add_filter("plugin_row_meta", array(&$this, "plugin_row_meta"), 10, 2);
    // Wrap <li> tags around each template item.
    add_filter( 'child-pages-shortcode-template', 'wpse_113253_child_pages_shortcode_template' );
    // Wrap <div> and <ul> tags around everything.
    add_filter( 'child-pages-shortcode-output','wpse_113253_child_pages_shortcode_output' );
}

public function init()
{
    add_post_type_support('page', 'excerpt');
}

public function wp_enqueue_scripts()
{
    $css = apply_filters(
        "child-pages-shortcode-stylesheet",
        plugins_url("style.css", __FILE__)
    );
wp_register_style(
    'child-pages-shortcode-css',
    $css,
    array(),
    $this->ver,
    'all'
);
wp_enqueue_style('child-pages-shortcode-css');

$js = apply_filters(
    "child-pages-shortcode-js",
    plugins_url("script.js", __FILE__)
);
wp_register_script(
    'child-pages-shortcode',
    $js,
    array('jquery'),
    $this->ver,
    false
);
wp_enqueue_script('child-pages-shortcode');
}

public function shortcode($p, $template = null)
{
if( !isset($p['id']) || !intval($p['id']) ){
    $p['id'] = get_the_ID();
}
if (!isset($p['size']) || !$p['size']) {
    $p['size'] = 'thumbnail';
}
if (!isset($p['width']) || !intval($p['width'])) {
    $p['width'] = "50%";
}
if (!isset($p['disable_shortcode']) || !$p['disable_shortcode']) {
    add_filter("child-pages-shortcode-output", "do_shortcode");
}
return $this->display($p, $template);
}

private function display($p, $block_template)
{
global $post;

$html = '';

if ($block_template) {
    $template = $block_template;
    $template = str_replace('<p>', '', $template);
    $template = str_replace('</p>', '', $template);
    $template = apply_filters(
        'child-pages-shortcode-template',
        $template,
        $p
    );
} else {
    $template = apply_filters(
        'child-pages-shortcode-template',
        $this->get_template(),
        $p
    );
    $html = sprintf(
        '<div class="row-fluid">'
    );
}

$args = array(
    'post_status' => 'publish',
    'post_type' => 'page',
    'post_parent' => $p['id'],
    'orderby' => 'menu_order',
    'order' => 'ASC',
    'nopaging' => true,
);
$args = apply_filters('child-pages-shortcode-query', $args, $p);

$pages = get_posts($args);
foreach ($pages as $post) {
    setup_postdata($post);
    $post = apply_filters('child_pages_shortcode_post', $post);
    $url = get_permalink($post->ID);
    $img = get_the_post_thumbnail($post->ID, $p['size']);
    $img = preg_replace( '/(width|height)="\d*"\s/', "", $img);
    $tpl = $template;
    $tpl = str_replace('%width%', esc_attr($p['width']), $tpl);
    $tpl = str_replace('%post_id%', intval($post->ID), $tpl);
    $tpl = str_replace('%post_title%', $post->post_title, $tpl);
    $tpl = str_replace('%post_url%', esc_url($url), $tpl);
    $tpl = str_replace('%post_thumb%', $img, $tpl);
    if (isset($p['disabled_excerpt_filters']) && $p['disabled_excerpt_filters']) {
        $tpl = str_replace('%post_excerpt%', $post->post_excerpt, $tpl);
    } else {
        $tpl = str_replace('%post_excerpt%', get_the_excerpt(), $tpl);
    }
    $html .= $tpl;
}

wp_reset_postdata();

if (!$block_template) {
    $html .= '</div><!--row-fluid Child Pages Shortcode-->';
}

return apply_filters("child-pages-shortcode-output", $html);
}

private function get_template()
{
$html = "\n";
$html .= '<li class="span3">';
$html .= '<div id="child_page-%post_id%" class="thumbnail">';
$html .= '<div class="post_thumb"><a href="%post_url%">%post_thumb%</a></div>';
$html .= '<div class="caption">';
$html .= '<h4><a href="%post_url%">%post_title%</a></h4>';
$html .= '<p>%post_excerpt%</p>';
$html .= '</div><!-- .caption  -->';
$html .= '</div><!-- .child_page-container -->';
$html .= '</div><!-- #child_page-%post_id%" -->';
$html .= "\n";

if ($tpl = get_post_meta(get_the_ID(), 'child-pages-template', true)) {
    $html = $tpl;
}

return $html;
}
/**
 * Wrap <li> tags around each template item.
 */
function wpse_113253_child_pages_shortcode_template( $template ) {
return "<li>$template</li>\n";
}

/**
* Wrap a <div> and <ul> tags around everything.
*/
function wpse_113253_child_pages_shortcode_output( $html ) {
return "
<div class='row-fluid'>
<ul class='thumbnails'>
    $html
</ul><!-- end .thumbnails -->
</div><!-- end .row-fluid -->
";
}

public function plugin_row_meta($links, $file)
{
$pname = plugin_basename(__FILE__);
if ($pname === $file) {
    $links[] = sprintf(
        '<a href="%s">Donate</a>',
        'https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclickhosted_button_id=8RADH554RPKDU'
    );
}
return $links;
}

} // end childPagesShortcode()

// eof
2
  • What do you want in each list item (<li></li>) inside the unordered list (<ul class="thumbnails"></ul>)? Just whatever is in the general template? Commented Sep 6, 2013 at 23:17
  • To clarify: to change the individual "children" template so that it generates list items is something I have been able to do. But to add a <div> and <UL> around all of the children have I not been able to do. How would you do it?
    – StenW
    Commented Sep 6, 2013 at 23:40

1 Answer 1

1

Try this:

// Wrap <li> tags around each template item.
add_filter( 'child-pages-shortcode-template', 'wpse_113253_child_pages_shortcode_template' );

// Wrap <div> and <ul> tags around everything.
add_filter( 'child-pages-shortcode-output', 'wpse_113253_child_pages_shortcode_output' );

/**
 * Wrap <li> tags around each template item.
 */
function wpse_113253_child_pages_shortcode_template( $template ) {
    return "<li>$template</li>\n";
}

/**
 * Wrap a <div> and <ul> tags around everything.
 */
function wpse_113253_child_pages_shortcode_output( $html ) {
    return "
<div class='row-fluid'>
    <ul class='thumbnails'>
        $html
    </ul><!-- end .thumbnails -->
</div><!-- end .row-fluid -->
";
}

Note: I didn't install the plugin to test this code.

3
  • I did something wrong in the implementation, you can see the error in the question. I am not sure what I did wrong? BTw I much appreciate your help!
    – StenW
    Commented Sep 7, 2013 at 0:10
  • The code I provided should go in the functions.php child theme file, not inside the plugin code. Never edit plugin code. It will be wiped out when the plugin updates. Commented Sep 7, 2013 at 0:15
  • Ok then I understand :)
    – StenW
    Commented Sep 7, 2013 at 0:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.