Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

This is my first plugin from more or less scratch and I think I almost got it working, but not quite there yet. The point of the plugin is to get all the child pages, display their thumbnail, title, etc. Now that I wrote the plugin it gives me 0 output... Why? There can be many things I probably have done wrong, but I decided to start looking for mistakes in the get_pages part. If I add print_r('$child_query') and $child_query = get_pages($args); should this not at least output something?

Question why does print_r('$child_query') return nothing?

My code problematic code...

<?php
/*
Plugin Name: Child Pages Querry
Author: Sten Winroth
Plugin URI: 
Description: 
Version: 1.0
Author URI: 
Domain Path: /languages
Text Domain: 
*/

new childPagesQuerry();
class childPagesQuerry {

private $ver = '1.0';

function __construct(){
add_action("init", array(&$this, "init"));
add_shortcode('child_query', 'child_query_frontend');
}

public function init()
{
add_post_type_support('page', 'excerpt');
}
private function child_query(){
    // Determine parent page ID
    $parent_page_id = ( '0' != $post->post_parent ? $post->post_parent : $post->ID );

    // Get child pages as array
    $args = array(
        'child_of' => $parent_page_id,
        'post_type' => 'page',
        'orderby' => 'menu_order',
        'order' => 'ASC',
        'posts_per_page' => 100,
     );
    $child_query = get_pages($args);
    print_r($child_query);

    $child_pages = array();

    while ( $child_query->have_posts()) {
        $child_query->the_post();
            if ( '' != get_the_title() ) {
        $title = get_the_title();
        $excerpt = get_the_excerpt();
        $id = get_the_ID();
        $thumbnail = get_the_post_thumbnail('(thumbnail');
        $permalink = get_permalink();
        $child_pages[] = array('title' => $title, 'excerpt' => $excerpt, 'id' => $id, 'thumbnail' => $thumbnail, 'permalink' => $permalink);
        }
    }
} //end child_query
private function child_query_frontend($child_pages) {
        echo '<div class="row-fluid">';
        echo '<ul class="thumbnails">';
        foreach ($child_pages as $key => $value) {
            ?>
            <li class="span3">
        <div class="thumbnail">
            <img src="<?php echo $value['thumbnail']; ?>" alt="" height="42" width="42">
            <div class="caption">
            <h3><?php echo $value['title']; ?></h3>
            <p><?php echo $value['excerpt'] ?></p>
            <p><a class="btn btn-primary" href="<?php echo $value['permalink']; ?>">Läs mer.</a></p>
            </div>
        </div>
    </li>
        <?php }
        echo '</ul><!--thumbnails-->';
        echo '</div><!--row-fluid-->';
} //end frontend

}// end class
share|improve this question
1  
Your print_r is inside the child_query method, and I don't see anywhere in your code where that method is used. Am I missing something? –  s_ha_dum Sep 12 '13 at 22:15
1  
(1) In: $parent_page_id = ( '0' != $post->post_parent ? $post->post_parent : $post->ID ); you use the $post variable? Ask yourself: who set this variable inside your function? (2) get_the_post_thumbnail('(thumbnail'); don't you note anything strange here? However, this question seems to me a code review request more than a real question... –  G. M. Sep 12 '13 at 22:17
    
I don´t think you are missing anything, I think I just don´t understand why I have this problem. I am quite new to development and have tried to follow the recommendations that are out there. –  StenW Sep 12 '13 at 22:18
    
(3) see the @s_ha_dum comment. (4) you cannot use private methods to be called by an hook –  G. M. Sep 12 '13 at 22:20
1  
It's not a dumb question. But if you ask..., or better, if you don't ask anything is very difficult help you. I or someone else can rewrite this code and make it works, but.. this is a Q&A site, and if I rewite your code, where is the Q, and where is the A? If the question is "why does print_r('$child_query') return nothing?" the only possible answer is: because code is wrong. And in previous comments I start to say where I see code is wrong. –  G. M. Sep 12 '13 at 22:32
show 2 more comments

1 Answer 1

up vote 2 down vote accepted

Take a look at this to get you started. Then work from here. There were a number of things you had wrong, and I think this will clarify.

<?php
/*
Plugin Name: Child Pages Query
Author: Sten Winroth
Plugin URI: 
Description: 
Version: 1.0
Author URI: 
Domain Path: /languages
Text Domain: 
*/

class childPagesQuery
{
    private $ver = '1.0';

    function __construct()
    {
        add_action( 'init', array($this, 'init') );
    }

    public function init()
    {
        add_shortcode( 'child_query', array($this, 'child_query_frontend') );
    }

    public function child_query()
    {
        global $post;

        // Determine parent page ID
        $parent_page_id = ( '0' != $post->post_parent ? $post->post_parent : $post->ID );

        // Get child pages as array
        $args = array(
            'child_of' => $parent_page_id,
            'post_type' => 'page',
            'orderby' => 'menu_order',
            'order' => 'ASC',
            'posts_per_page' => 100,
        );
        $child_query = get_pages($args);
        print_r($child_query);
    }

    public function child_query_frontend( $atts )
    {
        /**/
        $child_query = $this->child_query();
        echo '<pre style="font-size:0.7em;">';
        print_r($child_query);
        echo "</pre>\n";
        /**/
    }
}

new childPagesQuery();

EDIT: To clarify about the $atts variable passed to child_query_frontend().

If you used your shortcode like this:

[child_query]

Nothing will be passed to that variable, it will be empty, but it should still be there, because the shortcode functions will try to pass an empty array to it.

If you use your shortcode like this:

[child_query post_id="5"]

Then $atts would be something like $atts['post_id']. Take a look at the Shortcode API documentation on Wordpress.org for more about how attributes work and better examples of how to use them.

share|improve this answer
1  
Thank you man, really cool of you! –  StenW Sep 12 '13 at 22:41
    
I know its a little late to ask, but what was your thoughts on the argruments that should be passed to the public function child_query_frontend( $atts ) –  StenW Sep 13 '13 at 19:15
    
That is a standard variable passed through from the shortcode script. –  Jake Sep 13 '13 at 21:24
    
See edit above. –  Jake Sep 13 '13 at 21:30
    
Thats was my guess as well, thanks man! –  StenW Sep 13 '13 at 21:45
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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