Skip to main content

十二、wordpress自定义侧边栏制作

1、wordpress分类列表页侧边栏

<?php if ( is_single() ) :
global $post;
$categories = get_the_category();
foreach ($categories as $category) :
    ?>
    <li class="widget widget_recent_entries" id="<?php $category -> term_id; ?>-posts">
        <h2 class="widgettitle"><?php echo $category -> name; ?></h2>
        <ul>
        <?php
        $posts = get_posts('numberposts=5&category='. $category->term_id);
        foreach($posts as $post) :
        ?>
            <li>
            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
            </li>
        <?php endforeach; ?>
        </ul>
    </li>
<?php
endforeach; endif ;
 ?>

 

2、wordpress侧边栏,当前文章页所在分类栏目下的文章列表

 
<?php if ( is_single() ) :
global $post;
$categories = get_the_category();
foreach ($categories as $category) :
    ?>
    <li class="widget widget_recent_entries" id="<?php $category -> term_id; ?>-posts">
        <h2 class="widgettitle"><?php echo $category -> name; ?></h2>  //当前文章所在栏目
        <ul>//下方为循环输出 所在栏目分类下的文章列表
        <?php
        $posts = get_posts('numberposts=5&category='. $category->term_id); //5为循环条数
        foreach($posts as $post) :
        ?>
            <li>
            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
            </li>
        <?php endforeach; ?>
        </ul>
    </li>
<?php
endforeach; endif ;
 ?>