• Resolved Tiago ADPS

    (@tiago-adps)


    Hi,

    Is it possible to display, on a parent page, a list (with title and featured image) of it’s child pages that were published in the current month?

    I know at least how to call all children from the current page, but I’m having trouble with the rest:

    <ul>
    <?php
    global $post;
    $current_page_parent = ( $post->post_parent ? $post->post_parent : $post->ID );
    
    wp_list_pages( array(
         'title_li' => '',
         'child_of' => $current_page_parent,
         'depth' => '1')
    );
    ?>
    </ul>

    thanks

Viewing 7 replies - 1 through 7 (of 7 total)
  • Hello,

    Instead of using wp_list_page(), I would try using WP_Query().

    Here is an example of how to accomplish this:

    $year = date('Y');
    $month = date('m');
    
    $args = array (
    		'post_type' => 'page',
    		'post_parent' => $parent_page_id,
    		'year' => $year,
    		'month' => $month,
    		'posts_per_page' => -1
    	);
    
    $query = new WP_Query( $args );
    
    // The Loop
    if ( $query->have_posts() ) {
    	echo '<ul>';
    	while ( $query->have_posts() ) {
    		$query->the_post();
    		echo '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
    	}
    	echo '</ul>';
    } else {
    	// no posts found
    }
    /* Restore original Post Data */
    wp_reset_postdata();

    I did not test this code, but it should get you on the right track.

    -Brad

    Thread Starter Tiago ADPS

    (@tiago-adps)

    Thank you for the answer Brad.

    But please tell me, do you know if I can run this code on a widget? (the widget runs php)

    It looks good, but I tried it and nothing loaded beyond the header of the page.

    I’m not 100% sure what your asking about regarding running the code on a widget. Did you create your own widget and try to use my code in it?

    If nothing is loading after your header, there is most likely a PHP error. Check your error logs or turn on error displaying.

    I can test, the code I supplied quick and see if there is an error with it.

    I just tested the code I supplied above and it worked as expected. Did you replace the “$parent_page_id” variable with your own?

    I added this code to one of my site’s page.php and replaced $parent_page_id with $post->ID.

    If you are loading this code outside the loop, try using get_the_ID() to get the current page ID.

    I had to make a small change to the code I gave you because it was still loading pages that weren’t in the current month.

    Change was to the arguments:

    $args = array (
    		'post_type' => 'page',
    		'post_parent' => $post->ID,
    		'year' => $year,
    		'monthnum' => $month,
    		'posts_per_page' => -1
    	);

    ‘monthnum’ => $month,

    Thread Starter Tiago ADPS

    (@tiago-adps)

    Brad, thank you very much once again! That’s it!

    I probably messed up the variable the first time, of course.
    It works 🙂

    Just for the record, the final code I used:

    <?php
    global $post;
    $current_page_parent = ( $post->post_parent ? $post->post_parent : $post->ID );
    $year = date('Y');
    $month = date('m');
    
    $args = array (
    		'post_type' => 'page',
    		'post_parent' => $current_page_parent,
    		'year' => $year,
                    'monthnum' => $month,
    		'posts_per_page' => -1
    	);
    
    $query = new WP_Query( $args );
    
    // The Loop
    if ( $query->have_posts() ) {
    	echo '<ul>';
    	while ( $query->have_posts() ) {
    		$query->the_post();
    		echo '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
    	}
    	echo '</ul>';
    } else {
    	// no posts found
    }
    /* Restore original Post Data */
    wp_reset_postdata();
    
    ?>

    Glad to hear it worked for you!

    Can you please mark this topic as resolved?

    Thanks!

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Display list of child pages published in the current month only’ is closed to new replies.