WordPress的PHP:显示页面的子代(编辑现有代码)
问题描述:
我正在帮助朋友对wordpress主题进行一些更改。目前显示子页面仅适用于一个父母及其子页面(ID为31的子页面),但我们希望它适用于所有页面(及其子页面)。我想或多或少地保留代码,只需进行必要的更改即可应用于所有代码。WordPress的PHP:显示页面的子代(编辑现有代码)
虽然我对css和html很不错,但php并不是我的强项,所以我希望你的帮助能够解决这个问题。这是现有的代码:
$currentPageId = get_the_ID();
$pages = get_pages('child_of=31');
$i=0;
foreach($pages as $child) {
$childrenPageID[$i] = $child->ID;
$i++;
}
if ((get_the_ID() == 31) || (in_array($currentPageId, $childrenPageID))) {
$taxonomy = 'portfoliocat';
$orderby = 'name';
$show_count = 0; // 1 for yes, 0 for no
$pad_counts = 0; // 1 for yes, 0 for no
$child_of = 5;
$show_count = 1;
$title_li = null;
$args = array(
'taxonomy' => $taxonomy,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'child_of' => $child_of,
'show_count' => $show_count,
'title_li' => $title_li
);
?>
<div style="overflow:auto; margin-bottom:20px;">
<ul style="list-style-type:none; margin-left: 0px;">
<?php
$cat = get_query_var('cat');
// count all categories
$totalCount = count(get_categories($args));
$currentCount = 0;
foreach(get_categories($args) as $category) {
// get current number of stack
$currentCount = $currentCount + 1;
global $wpdb;
$lookForValue = $category->slug;
$querystr = "SELECT post_id
FROM $wpdb->postmeta
WHERE meta_key = 'category-include' AND meta_value = '".$lookForValue."';
";
$postid = $wpdb->get_var($wpdb->prepare($querystr));
// logic to remove last trunk
if ($totalCount != $currentCount ) {
echo '<li class="cat-item"><a class="name" style="font-weight:bold" href="/?page_id='.$postid.'">' . $category->name.'</a> <span style="color: #999;">|</span> </li>';
}
else {
echo '<li class="cat-item"><a class="name" style="font-weight:bold" href="/?page_id='.$postid.'">' . $category->name.'</a></li>';
}
}?>
</ul>
</div>
}
我猜我需要更改if子句,但没有线索要替换它。有什么建议么?
在此先感谢! Joanna
答
// If CHILD_OF is not NULL, then this page has a parent
// Therefore, list siblings i.e. subpages of this page's parent
if($post->post_parent){
wp_list_pages('title_li=&include='.$post->post_parent);
wp_list_pages('title_li=&child_of='.$post->post_parent);
}else{
// If CHILD_OF is zero, this is a top level page, so list subpages only.
wp_list_pages('title_li=&include='.$post->ID);
wp_list_pages('title_li=&child_of='.$post->ID);
}
谢谢您的回答。我试着简单地使用wp_list_pages,但我认为我在模板上的代码已经添加了更多的数据(例如它告诉哪个孩子是最后一个,所以不会包含正确的边框),我们需要这些数据。 – Joanna