首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在分类页面上显示三篇完整的帖子,然后将其余的帖子仅作为标题

在分类页面上显示三篇完整的帖子,然后将其余的帖子仅作为标题
EN

Stack Overflow用户
提问于 2016-11-19 09:29:08
回答 2查看 51关注 0票数 0

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

我的循环中有相当多的超文本标记语言,用于设置帖子的样式和添加自定义字段。很抱歉,所有代码都是这样的,但这就是我的category.php页面现在的样子。我尝试了一些不起作用的东西,所以我编辑了这段代码,以显示我的原始代码,它只有一个普通的帖子列表。我对编辑循环有些陌生,所以我希望尽可能多地解释/澄清。

代码语言:javascript
复制
    <?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(); ?>
EN

回答 2

Stack Overflow用户

发布于 2016-11-19 13:04:47

你可以使用下面的代码来完成上面的事情:首先,你必须循环所有的post和put计数器,当它超过2时,它打印content.but标题的停止将总是在那里。

代码语言:javascript
复制
      <?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

票数 0
EN

Stack Overflow用户

发布于 2016-11-22 04:14:19

我自己想出了一个变通的解决方案,尽管它依赖于使用插件/窗口小部件,这不是我喜欢的。

我只需将阅读设置设置为显示2个帖子,然后在Loop下面添加一个小部件区域,并使用最近的帖子扩展小部件来显示标题/链接列表。此小部件允许您跳过列表中一定数量的帖子,因此我将其设置为从帖子#3开始。没有仅显示当前类别的帖子的选项,因此我还必须使用widget上下文插件,并使具有特定类别的单个小部件显示在每个相应的类别页面上。正如我所说的,有点复杂的解决方案,但最终结果正是我想要实现的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40688306

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档