我有这个输出10个链接的函数,我想将链接分成两个div,但我认为当我调用函数时echo会丢失。
这就是我所拥有的:
function wpb_list_child_pages() {
$args = array(
'sort_order' => 'asc',
'sort_column' => 'post_title',
'hierarchical' => 1,
'meta_value' => '',
'child_of' => 108,
'parent' => -1,
'exclude_tree' => '',
'offset' => 0,
'post_type' => 'page',
'post_status' => 'publish'
);
$pages = get_pages($args);
if ( $pages)
{
$links = '';
$counter = 1;
foreach($pages as $page):
$links .= '<ul> <li> <a href=" ' . get_page_link( $page->ID ) . '"> ' .$page->post_title . ' </a> </li></ul>';
$counter++; // increment the counter variable for each loop
endforeach;
return $links;
}
}
这就是我试图做的原因:
function wpb_list_child_pages() {
$args = array(
'sort_order' => 'asc',
'sort_column' => 'post_title',
'hierarchical' => 1,
'meta_value' => '',
'child_of' => 108,
'parent' => -1,
'exclude_tree' => '',
'offset' => 0,
'post_type' => 'page',
'post_status' => 'publish'
);
$pages = get_pages($args);
if ( $pages)
{
$links = '';
$counter = 1;
foreach($pages as $page):
if($counter == 1) {
echo "<div class='left_class'>"; // open left div
}
$links .= '<ul> <li> <a href=" ' . get_page_link( $page->ID ) . '"> ' .$page->post_title . ' </a> </li></ul>';
//var_dump ($links);
if($counter==5){
echo "</div>"; //close left div
echo "<div class='right_class'>"; // open right div
}
if($counter==10){
echo "</div>"; //close right div
}
$counter++; // increment the counter variable for each loop
endforeach;
return $links;
}
}
这就是我调用函数的方法
echo '<div class="section-1">'.wpb_list_child_pages().'</div>';
答案 0 :(得分:0)
您的脚本似乎在输出之前收集变量中的链接。你的(缩进的)示例应该如下所示:
function wpb_list_child_pages() {
$args = array(
'sort_order' => 'asc',
'sort_column' => 'post_title',
'hierarchical' => 1,
'meta_value' => '',
'child_of' => 108,
'parent' => -1,
'exclude_tree' => '',
'offset' => 0,
'post_type' => 'page',
'post_status' => 'publish'
);
$pages = get_pages($args);
if ( $pages)
{
$links = '';
$counter = 1;
foreach($pages as $page):
if($counter == 1) {
$links .= "<div class='left_class'>"; // open left div
}
$links .= '<ul> <li> <a href=" ' . get_page_link( $page->ID ) . '"> ' .$page->post_title . ' </a> </li></ul>';
//var_dump ($links);
if($counter==5){
$links .= "</div>"; //close left div
$links .= "<div class='right_class'>"; // open right div
}
if($counter==10){
$links .= "</div>"; //close right div
}
$counter++; // increment the counter variable for each loop
endforeach;
return $links;
}
}
所以,出现错误的是你在回显链接之前回显了所有(开启和关闭)标签。