当前类别中的最近帖子?

时间:2013-04-11 作者:andy

在我的分类页面(不是single.php)上,我希望侧边栏中有该分类的最新帖子,它还需要是动态的。

我已经在单曲上做了类似的事情。php,但我不知道如何在类别上实现它。php页面。

    <div class="related">


    <?php


    $related = get_posts( array( \'category__in\' => wp_get_post_categories($post->ID), \'numberposts\' => 5, \'post__not_in\' => array($post->ID) ) );

    if( $related ) {

    echo \'<h3>Related Articles</h3> <ul>\';

    }
    if( $related ) foreach( $related as $post ) {
    setup_postdata($post); ?>




            <li>
            <span class="thetime">
               <?php the_time(\'F jS, Y\') ?>
            </span>
            <?php the_post_thumbnail(\'sidethumb\'); ?>


            <h4><a href="<?php the_permalink() ?>"><?php the_title (); ?></a></h4>

            <p><?php echo excerpt(25); ?></p>

            </li>


    <?php }

    wp_reset_postdata(); ?>
     </ul> 





    </div> <!-- end div related --> 
有没有可能说出最近20篇关于当前选定类别的帖子?

呃,所以我无法让你的代码工作。

                <?php
    $max_posts = 20;
    $cat_ID = get_cat_ID ( single_cat_title( \'\', false ) );
    $args = array(
        \'category\' => $cat_ID,
        \'numberposts\' => $max_posts,
    );
    $myposts = get_posts( $args );
    foreach( $myposts as $post ) {
        setup_postdata( $post );
        // display your posts with the_title(), the_excerpt(), etc
      <php the_title (); ?> 

    }
?>
如果我把<php the_title (); ?> 等在你写的地方显示你的文章标题等,它给我一个空白页。

我的类别。php如下所示

<?php get_header(); ?>


<?php get_sidebar(\'catbar\'); ?>



    <div class="main-content">

        <div class="center" id="scale1">
            <div id="content" class="longform">


        <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

            <article <?php post_class(\'item\') ?> id="post-<?php the_ID(); ?>">
                <div class="meta">
                    <div class="metastuff">
                        <div class="authorname">
                        <h4>By <a href="<?php echo get_author_posts_url(get_the_author_meta( \'ID\' )); ?>"><?php the_author_meta(\'display_name\'); ?></a></h4>
                        </div>

                                            </div>

                        <span class="popupcomments">

                                <?php comments_popup_link (\'0\', \'1\', \'%\'); ?>

                        </span> 

                </div> <!-- end div meta -->


                <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                <div class="authordate">
                        <span class="authordate-inner"><?php the_time(\'F jS, Y\') ?></span>
                </div>





                <?php
                    if(has_post_thumbnail()) :?>
                    <div class="figure">

                        <?php the_post_thumbnail(\'\'); ?>
                        <?php if( !empty( get_post( get_post_thumbnail_id() )->post_excerpt ) ) { ?>
                            <div class="postthumbnailtext">
                                <?php echo get_post( get_post_thumbnail_id() )->post_excerpt ?>
                            </div>
                        <?php } ?>
                    </div> <!-- end div figure //centers thumbnail -->
                <?php else :?>
                <?php endif;?>


                <div class="the-content-container">
                    <?php the_content(\'Read Full Article\'); ?>
                </div>



                <div class="the-sharing-field"> 

                 <label>Share this Article:</label>
                 <input type="text" value="<?php the_permalink (); ?>">

                <span class="reveal-share-bar">Show Sharing Options</span>                      
                </div>  

                    <div class="sharing-mp sharing-mp-hidden">
                    <ul>

            <li>    
                <a  class="social-email" rel="nofollow" title="Email this page" href="mailto:?Subject=<?php the_title(); ?>&body=<?php the_permalink(); ?>"></a>    
            </li>
            <li>    
                <a class="social-twitter" rel="nofollow" href="http://twitter.com/home?status=Currently reading <?php the_permalink(); ?>" title="Share page via Twitter" target="_blank"></a>
            </li>   
            <li>
                <a class="social-facebook" title="Share page via Facebook" rel="nofollow" href="http://www.facebook.com/sharer.php?u=<?php the_permalink();?>&t=<?php the_title(); ?>" target="blank"></a>
            </li>
            <li>    
                <a class="social-goo" title="Share page via Google+"  href="http://plus.google.com/share?url=<?php the_permalink (); ?>" rel="nofollow" target="_blank"></a>
            </li>
            <li>    
                <a class="social-linked" title="Share page via LinkedIn" rel="nofollow" href="http://www.linkedin.com/shareArticle?mini=true&url=<?php the_permalink (); ?>" target="_blank"></a>
            </li>

        </ul>



                </div> <!-- end div sharing-mp -->



                    <div class="postbottom"></div>

                    <div class="clearfix"></div>

                </article> <!-- end div post -->



        <?php endwhile; ?>


        <?php endif; ?>

            </div> <!-- end div content -->

            <div id="nav-below" class="navigation">

                <?php if(function_exists(\'wp_pagenavi\')) { ?>

                <?php wp_pagenavi(); ?>

                <?php } else { ?>  

                <div class="nav-previous"><?php next_posts_link(__(\'Load More Articles\')) ?></div>

                <div class="nav-next"><?php previous_posts_link(__(\'Newer posts\')) ?></div>

                <?php } ?>

            </div>  <!-- end div nav-below -->
        </div> <!-- end div center -->



        <div class="clearfix"></div>    


    </div> <!-- end div main-content -->




<?php get_footer(); ?>

1 个回复
最合适的回答,由SO网友:Pat J 整理而成

以下是我如何在category.php 页码:

<?php
    $max_posts = 20;
    $cat_ID = get_cat_ID ( single_cat_title( \'\', false ) );
    $args = array(
        \'category\' => $cat_ID,
        \'numberposts\' => $max_posts,
    );
    $myposts = get_posts( $args );
    foreach( $myposts as $post ) {
        setup_postdata( $post );
    ?>
    <!-- add your HTML here, including the_title(), the_excerpt(), etc -->
    <?php

    }
?>
代码使用get_cat_ID()single_cat_title() 获取当前类别的ID。我已经做了一些(非常)最低限度的测试,以确保它至少排除了当前类别中不包含的帖子。

结束

相关推荐

如何从wp_Dropdown_Categories获取类别插件

我正在自定义一个插件。我需要它来显示层次结构中的类别列表,这是我使用插件的所有功能得到的。我正在使用wp_dropdown_categories, 但我想将类别列表显示为它们的slug,而不是它们的类别名称。有什么建议吗?以下是我目前掌握的情况:function replace_id_for_slug( $option ) { $categories = get_categories( \"hide_empty=0\" ); preg_match( \'/value=\"(