Support » Fixing WordPress » Add current page id into a function

  • wvanderzee

    (@wouter-van-der-zee)



    I’d like to create a custom loop function that displays child posts of the current page. That function is used in a custom page template. Whenever that page template is set, it must show the child pages.

    For now the page id can only be set manually in the function. Is it possible to determine the parent id and set it accordingly in the function? Hope you understand what I mean :).

    Here’s the current function, see below. It would be great if I can set the post_parent id to the current page, where the page template is set.

    Any suggestions?

    PS. The function and page template is for a Genesis framework based site. But that doesn’t matter for posting the question here I think…

    function wz_custom_loop() {
            //  based on function of Bill Erickson as on https://gist.github.com/billerickson/3218052
    
     	global $post;
    
     	// arguments, adjust as needed
     	$args = array(
     		'post_type'     => 'page',
     		'post_parent' 	=> 32, // id is current page?
     		'post_status'   => 'publish',
     	);
    
     	// Use $loop, a custom variable we made up, so it doesn't overwrite anything
     	$loop = new WP_Query( $args );
    
     	// have_posts() is a wrapper function for $wp_query->have_posts(). Since we
     	// don't want to use $wp_query, use our custom variable instead.
     	if ( $loop->have_posts() ) :
     		while ( $loop->have_posts() ) : $loop->the_post(); 
    
                           echo '<li>' . get_the_title() . '</li>'; 
    
                   endwhile;
     	endif;
    
     	// We only need to reset the $post variable. If we overwrote $wp_query,
     	// we'd need to use wp_reset_query() which does both.
     	wp_reset_postdata();
    
     }
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘Add current page id into a function’ is closed to new replies.