这是我正在处理的网站:http://canal.es/wordpress/
我遇到的问题是,如果你去“ Pasteleria dulce ”(这是一个category.php文件),它会显示一个带有该类别活动标题的左侧边栏,它显示中心的一个帖子,它在图像下方显示了同一类别的帖子列表。
我还没找到一个解决方案来突出显示当我进入该类别时自动打开的帖子标题。这是我在category.php文件中使用的代码:
<!-- Highlight menu from current category -->
<?php
if (is_category()) {
$this_category = get_category($cat);
}
if($this_category->category_parent)
$this_category = wp_list_categories('orderby=id&title_li=&child_of='.$this_category->category_parent."&echo=0"); else
$this_category = wp_list_categories('orderby=id&title_li=&child_of='.$this_category->cat_ID."&echo=0");
if ($this_category) { ?>
<ul class="sub-menu">
<?php echo $this_category; ?>
</ul>
<?php } ?>
<div class="producto_column">
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<div class="producto_image">
<img src="<?php the_field('imagen_producto'); ?>" alt=""/>
</div>
<div class="share_item">
<a class="minimal" id="premium">
<img src="<?php bloginfo('template_directory') ?>/images/share.png" alt="share">
</a>
<a class="maillink" href="http://canal.local/?page_id=220">
<img src="<?php bloginfo('template_directory') ?>/images/email.png" alt="email">
</a>
</div>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<!-- Post list from current category -->
<ul id="submenu_productos" class="clearfix">
<?php
$IDOutsideLoop = $post->ID;
while( have_posts() ) {
the_post();
foreach( ( get_the_category() ) as $category )
$my_query = new WP_Query('category_name=' . $category->category_nicename . '&orderby=title&order=asc&showposts=100');
if( $my_query ) {
while ( $my_query->have_posts() ) {
$my_query->the_post(); ?>
<li<?php print ( is_single() && $IDOutsideLoop == $post->ID ) ? ' class="test"' : ''; ?>>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a> |
</li>
<?php
}
}
}
?>
</ul>
如何激活打开类别页面时显示的当前帖子标题?
答案 0 :(得分:0)
尝试使用此解决方案:
<ul>
<?php
$lastposts = get_posts('numberposts=5&orderby=rand&cat=-52');
foreach($lastposts as $post) :
setup_postdata($post); ?>
<li<?php if ( $post->ID == $wp_query->post->ID ) { echo ' class="current"'; } else {} ?>>
<a href="<?php the_permalink() ?>"><?php the_title(); ?></a>
</li>
<?php endforeach; ?>
</ul>