I have been working on a new function where I can simply add a shortcode to show child pages of a specific page I did set. Everything worked fined and looked good until I wanted to order/sort them and also set a limit.
Here is my code
function whatwedo_function () { $dynamicVVG = url_to_postid( site_url('what-we-do') ); $args = array( 'parent' => $dynamicVVG, 'post_type' => 'page', 'post_status' => 'publish', 'order' => 'DESC', 'posts_per_page' => 2, ); $pages = get_pages($args); // Initialize a variable to build your output string $output =""; $output = '<ul class="four no-bullets container"><div class="row">'; foreach( $pages as $page ) { // Add to the string rather than return it $output .=' <li class="col-md-4"> <a href="'.get_permalink($page->ID).'" rel="bookmark" title="test"> <span class="thumbnail ">'.get_the_post_thumbnail($page->ID, "large", array("class"=>"img-fluid") ).'</span> </a> <a href="'.get_permalink($page->ID).'" rel="bookmark" title="test"> <h3><span class="title">'.$page->post_title.'</span></h3> </a> <a href="'.get_permalink($page->ID).'" rel="bookmark" title="test"> <span class="desc">'.get_the_excerpt($page->ID).'</span> </a> <a href="'.get_permalink($page->ID).'" rel="bookmark" title="test"> <button type="button" class="btn btn-primary btn-lg btn-block mt-4">Block level button</button> </a> </li>'; } $output .=' </ul> </div>'; wp_reset_postdata(); return $output; } add_shortcode('what-we-do','whatwedo_function');
I have tried with
- wp_reset_postdata();
- wp_reset_query();
- In $args adding posts_per_page
- In $args adding showposts
Does anyone see what I have missed here ?
Advertisement
Answer
Add parameter for sorting
$args = array( 'parent' => 2, 'post_type' => 'page', 'post_status' => 'publish', ... 'orderby' => 'date', 'order' => 'DESC', ... 'posts_per_page' => 2, );
Replace code from
$pages = get_pages($args);
TO
$query = new WP_Query( $args ); $pages = $query->posts;
Final Conclusion. Also change parent to post_parent
function whatwedo_function () { $dynamicVVG = url_to_postid( site_url('what-we-do') ); $args = array( 'post_parent' => $dynamicVVG, 'post_type' => 'page', 'post_status' => 'publish', 'orderby' => 'date', 'order' => 'DESC', 'posts_per_page' => 2, ); $query = new WP_Query( $args ); $pages = $query->posts; // Initialize a variable to build your output string $output =""; $output = '<ul class="four no-bullets container"><div class="row">'; foreach( $pages as $page ) { // Add to the string rather than return it $output .=' <li class="col-md-4"> <a href="'.get_permalink($page->ID).'" rel="bookmark" title="test"> <span class="thumbnail ">'.get_the_post_thumbnail($page->ID, "large", array("class"=>"img-fluid") ).'</span> </a> <a href="'.get_permalink($page->ID).'" rel="bookmark" title="test"> <h3><span class="title">'.$page->post_title.'</span></h3> </a> <a href="'.get_permalink($page->ID).'" rel="bookmark" title="test"> <span class="desc">'.get_the_excerpt($page->ID).'</span> </a> <a href="'.get_permalink($page->ID).'" rel="bookmark" title="test"> <button type="button" class="btn btn-primary btn-lg btn-block mt-4">Block level button</button> </a> </li>'; } $output .=' </ul> </div>'; wp_reset_postdata(); return $output; } add_shortcode('what-we-do','whatwedo_function');