admin管理员组文章数量:1122846
I'm having a hard time figuring out how to display the title of the childpages as the id of a div. Assuming that 'news' & 'othernews' are siblings of the parent page home (39)
I want something like this:
<div class="wrapper" id="news">
content
</div>
<div class="wrapper" id="othernews">
content
</div>
This is my function for my shortcode:
function content_block( $atts, $content = null ){
extract( shortcode_atts( array(
'type' => 'white'
), $atts));
$childArgs = array(
'post_type' => 'page',
'parent_page' => 39
);
$child = new WP_Query($childArgs);
if ($child->have_posts()) {
while ($child->have_posts()) {
$child->the_post();
if ($type == 'white') {
$return .= '<div class="col-white">';
$return .= '<div class="wrap" id="'. get_the_title() .'">';
$return .= do_shortcode($content);
$return .= '</div>';
$return .= '</div>';
} else
if ($type == 'grey') {
$return .= '<div class="col-grey">';
$return .= '<div class="wrap" id="'. get_the_title() .'">';
$return .= do_shortcode($content);
$return .= '</div>';
$return .= '</div>';
}
wp_reset_query();
return $return;
}
}
}
I'm having a hard time figuring out how to display the title of the childpages as the id of a div. Assuming that 'news' & 'othernews' are siblings of the parent page home (39)
I want something like this:
<div class="wrapper" id="news">
content
</div>
<div class="wrapper" id="othernews">
content
</div>
This is my function for my shortcode:
function content_block( $atts, $content = null ){
extract( shortcode_atts( array(
'type' => 'white'
), $atts));
$childArgs = array(
'post_type' => 'page',
'parent_page' => 39
);
$child = new WP_Query($childArgs);
if ($child->have_posts()) {
while ($child->have_posts()) {
$child->the_post();
if ($type == 'white') {
$return .= '<div class="col-white">';
$return .= '<div class="wrap" id="'. get_the_title() .'">';
$return .= do_shortcode($content);
$return .= '</div>';
$return .= '</div>';
} else
if ($type == 'grey') {
$return .= '<div class="col-grey">';
$return .= '<div class="wrap" id="'. get_the_title() .'">';
$return .= do_shortcode($content);
$return .= '</div>';
$return .= '</div>';
}
wp_reset_query();
return $return;
}
}
}
Share
Improve this question
asked Nov 20, 2014 at 14:34
Jeroen BellemansJeroen Bellemans
1011 bronze badge
2
|
1 Answer
Reset to default 0May I suggest you provide details as to the exact problem, and what the results are of your example code?
Also, it seems like you are saying that the pages you want to get the titles of are siblings of the parent, which is the home page, so in fact, they are not children at all (An interesting plot for a TV drama, perhaps).
In any case, instead of using the title for your ID, I would suggest using the slug (post_name) instead:
$current_id = get_the_id();
$post = get_post($current_id);
$post->post_name;
Using the title might results in spaces in your ID, which could result in an array of IDs, not just one.
本文标签: Display title of child pages in shortcode
版权声明:本文标题:Display title of child pages in shortcode 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736295449a1929572.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
parent_page
, it'spost_parent
. and you reset the query and return inside of your while loop, so it can't possibly run for more than 1 iteration before exiting. – Milo Commented Nov 20, 2014 at 16:35