【发布时间】:2019-04-29 10:50:12
【问题描述】:
我目前正在开发一个 WordPress 网站,该网站在大多数页面上都有一个侧边栏,这个侧边栏应该显示正在查看的当前父页面下的任何子页面的小导航,以及其中两个最近的帖子。
目前,我无法在侧边栏上显示链接;它们只显示在网站的主要父页面上,而不是我需要的子页面上。
我创建了以下内容:
<?php
$args = array(
'post_type' => 'page',
'posts_per_page' => 10,
'post_parent' => $post->ID,
'order' => 'ASC',
'orderby' => 'menu_order'
);
$parent = new WP_Query( $args );
if ( $parent->have_posts() ) : ?>
<section class="links border shadow">
<ul>
<?php while ( $parent->have_posts() ) : $parent->the_post(); ?>
<li class="child-title">
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
<?php
$args2 = array(
'post_type' => 'page',
'posts_per_page' => 10,
'post_parent' => $post->ID,
'order' => 'ASC',
'orderby' => 'menu_order'
);
$parent2 = new WP_Query( $args2 );
if ( $parent2->have_posts() ) : ?>
<?php while ( $parent2->have_posts() ) : $parent2->the_post(); ?>
<div id="sub-<?php the_ID(); ?>" class="child-sub">
<p class="sub-title"><a href="<?php the_permalink(); ?>"
title="<?php the_title(); ?>"><?php the_title(); ?></a></p>
</div>
<?php endwhile; ?>
</li>
<?php endif; wp_reset_postdata(); ?>
<?php endwhile; ?>
</ul>
</section>
我也从这里的另一个问题中偶然发现了这个 sn-p 代码;它做同样的事情(仅显示在父页面上),但也显示到实际父页面的链接。
<?php
if($post->post_parent){
$children = get_pages("child_of=".$post->post_parent);
$parent_title = get_the_title($post->post_parent);
$link = get_permalink($post->post_parent);
}
else{
$children = get_pages("child_of=".$post->ID);
$parent_title = get_the_title($post->ID);
$link = get_permalink($post->ID);
$parent_page = $post->ID;
}
if ($children) {
?>
<li <?php if( !empty($parent_page) && $parent_page==$post->ID){echo 'class="current-menu-item"';} ?>><a href="<?php echo $link; ?>"><?php echo $parent_title;?></a></li>
<?php
foreach( $children as $post ) : setup_postdata($post);
?>
<li <?php if(is_page($post->ID)){echo 'class="current-menu-item"';} ?>>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</li>
<?php
endforeach;
?>
<?php
}
?>
感谢任何帮助。谢谢。
【问题讨论】:
标签: wordpress