无法设置 posts_per_page 或 order/sort 我的短代码功能
Can't set posts_per_page nor order/sort my shortcode function
我一直在开发一个新功能,我可以简单地添加一个短代码来显示我设置的特定页面的子页面。
一切都很好,看起来不错,直到我想 order/sort 它们并设置一个限制。
这是我的代码
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');
我试过
- wp_reset_postdata();
- wp_reset_query();
- 在 $args 添加 posts_per_page
- 在 $args 中添加展示贴
有人看到我在这里遗漏了什么吗?
添加排序参数
$args = array(
'parent' => 2,
'post_type' => 'page',
'post_status' => 'publish',
...
'orderby' => 'date',
'order' => 'DESC',
...
'posts_per_page' => 2,
);
替换
中的代码
$pages = get_pages($args);
到
$query = new WP_Query( $args );
$pages = $query->posts;
最终结论。
同时将父级更改为 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');
我一直在开发一个新功能,我可以简单地添加一个短代码来显示我设置的特定页面的子页面。 一切都很好,看起来不错,直到我想 order/sort 它们并设置一个限制。
这是我的代码
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');
我试过
- wp_reset_postdata();
- wp_reset_query();
- 在 $args 添加 posts_per_page
- 在 $args 中添加展示贴
有人看到我在这里遗漏了什么吗?
添加排序参数
$args = array(
'parent' => 2,
'post_type' => 'page',
'post_status' => 'publish',
...
'orderby' => 'date',
'order' => 'DESC',
...
'posts_per_page' => 2,
);
替换
中的代码$pages = get_pages($args);
到
$query = new WP_Query( $args );
$pages = $query->posts;
最终结论。 同时将父级更改为 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');