在catgegory页面上显示三个完整帖子,然后将其余帖子作为标题显示

时间:2016-11-19 01:29:08

标签: php wordpress themes

我正在建立一个主题,在我的category.php页面上,我想要显示几个完整的帖子(让我们说3,但需要能够轻松地将其更改为2或1),以及然后该类别中的其余帖子作为标题链接。

我的循环中有相当多的HTML用于样式化我的帖子并添加自定义字段以对所有代码抱歉,但这就是我的category.php页面现在的样子。我已经尝试了一些无法工作的东西,所以编辑了这个以显示我的原始代码,其中只有一个正常的帖子列表。我对编辑The Loop有些新意,所以尽可能多地理解解释/清晰度。

    <?php
     /**
     * The template for displaying Category Archive pages.
     */

    get_header(); ?>

    <div id="primary" class="<?php
    $category = get_the_category();
            echo $category[0]->cat_name;
            ?>">


    <div id="feature-container" class="full-width-container">
        <div class="full-width-container content-page" id="tagline-wrapper">
                <div id="left-black"></div>
                <div class="page-width-container">
                    <div id="tagline-box">  
                        <h1 class="category-title">Transactions</h1>
                    </div>  
                </div>
            </div>
    </div>          

    <div id="content-wrapper">

        <div id="project-menu" class="page-width-container">
            <?php wp_nav_menu( array( 'theme_location' => 'project-types' ) ); ?>
        </div>



        <div id="content" role="main" >



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


            <div class="story-container" class="module-container">
                <div class="our-story">
                    <div class="story-image">

            <?php
                    // check if the post has a Post Thumbnail assigned to it.
                if ( has_post_thumbnail() ) {
                    the_post_thumbnail();
                } 
            ?>

                    </div>
                    <div class="story-text">
                        <article class="post" id="post-<?php the_ID(); ?>">
                            <div class="entry-container">

                <h2><a href="<?php the_permalink() ?>#content" rel="bookmark"   title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>

                <div class="project-details">

                    <p><span class="details-location"><?php
                        global $wp_query;
                        $postid = $wp_query->post->ID;
                        echo get_post_meta($postid, '_project-location', true);
                        wp_reset_query();
                        ?></span><br />
                       <span class="details-funding"><?php
                        global $wp_query;
                        $postid = $wp_query->post->ID;
                        echo get_post_meta($postid, '_funding-type', true);
                        wp_reset_query();
                        ?> | <?php
                        global $wp_query;
                        $postid = $wp_query->post->ID;
                        echo get_post_meta($postid, '_funding-source', true);
                        wp_reset_query();
                        ?></span><br />
                       <span class="details-value"><?php
                        global $wp_query;
                        $postid = $wp_query->post->ID;
                        echo get_post_meta($postid, '_project-value', true);
                        wp_reset_query();
                        ?></span></p>

                </div>



            <div class="entry">



                <?php the_content(); ?>



                <?php wp_link_pages(array('before' => __('Pages: ','html5reset'), 'next_or_number' => 'number')); ?>

            </div>

            <?php edit_post_link(__('Edit this entry','html5reset'), '<p>', '</p>'); ?>

            </div>

        </article>

                    </div>
                </div>

            </div>


            <?php endwhile; endif; ?>


</div><!-- #content -->
        </div>
        </div><!-- #primary -->

<?php get_footer(); ?>

2 个答案:

答案 0 :(得分:0)

您可以使用以下代码实现上述目标: 首先你必须循环所有的帖子,并在它达到2以上时停止打印内容。但是标题将始终存在。

      <?php $countPost=1;?>
      <?php if (have_posts()) : ?>
       <?php while (have_posts()) : the_post(); ?>
         <div class="post">
       <h2 id="post-<?php the_ID(); ?>">
 <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>

       <?php  if($countPost>2) : /*Condition for Content*/
         the_content(); 
         endif;
       ?>
          </div>
        <?php endwhile; ?>
    <div class="navigation">
    <div class="alignleft">
    <?php posts_nav_link('','','&laquo; Previous Entries') ?>
    </div>
    <div class="alignright">
    <?php posts_nav_link('','Next Entries &raquo;','') ?>
    </div>
      </div>
    <?php else : ?>
      <h2 class="center">Not Found</h2>
     <p class="center"><?php _e("Sorry, but you are looking for something that isn't here."); ?></p>
      <?php endif; ?>
    </div>

有关详细信息,请参阅: https://codex.wordpress.org/The_Loop_in_Action

答案 1 :(得分:0)

我自己想出了一个解决方法,虽然它依赖于使用我不喜欢的插件/小部件。

我只是将阅读设置设置为显示2个帖子,然后在循环下方添加了一个小部件区域并使用Recent Posts Extended小部件显示标题/链接列表。此小部件允许您跳过列表中的一定数量的帖子,因此我将其设置为从第3个帖子开始。没有选项只显示当前类别的帖子,因此我也必须使用Widget Context插件,并使用特定类别制作单独的小部件以显示在每个相应的类别页面上。正如我所说,有点复杂的解决方案,但最终的结果正是我想要实现的。

相关问题