【问题标题】:WordPress: First post full content, rest excerpts Type ThemeWordPress:第一次发布完整内容,其余摘录类型主题
【发布时间】:2018-05-11 01:27:58
【问题描述】:

是的,我知道以前有人问过这个问题,并且我尝试了其他帖子提供的各种解决方案,但没有成功。我希望让我最近的帖子显示全部内容,然后其余的只显示摘录。出于某种原因,counter++ 和 first 方法不起作用。我正在使用 Design Lab 的 Type Theme。

这是我的 index.php:

    <?php
/**
 * The main template file.
 *
 * This is the most generic template file in a WordPress theme
 * and one of the two required files for a theme (the other being style.css).
 * It is used to display a page when nothing more specific matches a query.
 * E.g., it puts together the home page when no home.php file exists.
 *
 * @link https://codex.wordpress.org/Template_Hierarchy
 *
 * @package Type
 * @since Type 1.0
 */

get_header(); ?>

    <?php
    /* Blog Options */
    $blog_layout = get_theme_mod('blog_layout', 'list');
    $blog_sidebar_position = get_theme_mod('blog_sidebar_position', 'content-sidebar');
    $post_template = type_blog_template();
    $post_column = type_blog_column();
    ?>

    <?php if ( have_posts() ) : ?>

        <div id="primary" class="content-area">
            <main id="main" class="site-main" role="main">

                <section class="row posts-loop <?php if ('grid' == $blog_layout) { echo esc_attr('flex-row'); } ?>">
                    <?php /* Start the Loop */
                    while ( have_posts() ) : the_post(); ?>
                        <div class="post-wrapper <?php echo $post_column; ?>">
                            <?php get_template_part( 'template-parts/post/content', $post_template ); ?>
                        </div>
                    <?php endwhile; ?>
                </section>
                <?php the_posts_navigation(); ?>

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

    <?php else : ?>

            <?php get_template_part( 'template-parts/post/content', 'none' ); ?>

    <?php endif; ?>

<?php 
    // Sidebar
    if ( 'content-sidebar' == $blog_sidebar_position || 'sidebar-content' == $blog_sidebar_position ) {
        get_sidebar();  
    }
?>
<?php get_footer(); ?>

这是我的 content-list.php(我的主题是从中提取的):

" >

<?php if ( has_post_thumbnail() ) { ?>
    <figure class="entry-thumbnail">
        <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">                
            <?php the_post_thumbnail('type-medium'); ?>
        </a>
    </figure>
<?php } ?>

<div class="entry-header">
    <?php if ( 'post' === get_post_type() ) { ?>
        <div class="entry-meta">
            <span class="cat-links"><?php the_category( ', ' ); ?></span>
            <span class="sep">/</span>
            <?php echo '<span class="posted-on">' . type_time_link() . '</span>'; ?>
        </div>
    <?php } ?>
    <h2 class="entry-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
</div><!-- .entry-header -->

<div class="entry-summary">
     <?php the_excerpt(); ?>
</div><!-- .entry-content -->

我知道我需要更改 the_excerpt(); to the_content();,但我不能让它只在最近的帖子上工作,而且任何时候我试图把计数器的代码放在我认为是循环开始的地方......我得到一个语法错误。

我更喜欢 html 和 css……但我掌握了 php 的基础知识……我只是……是的,我在这方面失败了。

谢谢!

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    好的...所以...这是我一直在编辑 index.php 的地方...因为 content-list.php 工作正常...

    <?php
    /* Blog Options */
    $blog_layout = get_theme_mod('blog_layout', 'list');
    $blog_sidebar_position = get_theme_mod('blog_sidebar_position', 'content-sidebar');
    $post_template = type_blog_template();
    $post_column = type_blog_column();
    ?>
    
    <?php if ( have_posts() ) : ?>
    
        <div id="primary" class="content-area">
            <main id="main" class="site-main" role="main">
    
                <section class="row posts-loop <?php if ('grid' == $blog_layout) { echo esc_attr('flex-row'); } ?>"
    
                    <?php /* Start the Loop */
                    while ( have_posts() ) : the_post(); ?>
    
                         <?php $postcount++; //add 1 to the post counter ?>
    
                        <div class="post-wrapper <?php echo $post_column; ?>">
                            <?php get_template_part( 'template-parts/post/content', $post_template ); ?>
                        </div>
    
                            <?php $i++; ?>
    
                    <?php endwhile; ?>
                </section>
                <?php the_posts_navigation(); ?>
    
            </main><!-- #main -->
        </div><!-- #primary -->
    
    <?php else : ?>
    
            <?php get_template_part( 'template-parts/post/content', 'none' ); ?>
    
    <?php endif; ?>
    

    【讨论】:

      【解决方案2】:

      好的,是的,您似乎走在了正确的轨道上,但还没有完全到达那里。首先,您需要创建初始 postcount 并将其设置为 0。

      所以在你的 index.php 中,这样做:

      <?php
      /* Blog Options */
      $blog_layout = get_theme_mod('blog_layout', 'list');
      $blog_sidebar_position = get_theme_mod('blog_sidebar_position', 'content-sidebar');
      $post_template = type_blog_template();
      $post_column = type_blog_column();
      $postcount = 0; //This will be the counter for which post it is. If it's 0, show the full post, if it's higher, show the excerpt
      ?>
      

      然后,在 index.php 的下方,执行以下操作:

      <div id="primary" class="content-area">
              <main id="main" class="site-main" role="main">
      
                  <section class="row posts-loop <?php if ('grid' == $blog_layout) { echo esc_attr('flex-row'); } ?>"
      
                      <?php /* Start the Loop */
                      while ( have_posts() ) : the_post(); ?>
      
                          <div class="post-wrapper <?php echo $post_column; ?>">
                              <?php get_template_part( 'template-parts/post/content', $post_template ); ?>
                          </div>
      
                       <?php $postcount++; //add 1 to the post counter ?>
      
                      <?php endwhile; ?>
                  </section>
                  <?php the_posts_navigation(); ?>
      
              </main><!-- #main -->
          </div><!-- #primary -->
      

      由于您的主题没有像上面链接的教程中的示例那样定义是使用完整帖子还是仅使用 index.php 中的摘录,所以您还需要更改其他内容。

      在你的 content_list.php 中,找到这部分:

      <div class="entry-summary">
           <?php the_excerpt(); ?>
      </div><!-- .entry-content -->
      

      并将其更改为:

      <div class="entry-summary">
           <?php 
                if ($postcount == 0) {
                   the_content();                  
                }
                else {
                   the_excerpt(); 
                }
           ?>
      </div><!-- .entry-content -->
      

      我认为这应该可以解决问题。

      我希望这会有所帮助。如果没有,请告诉我您遇到了什么错误。

      【讨论】:

      • 完全复制了它,它只是让所有的帖子都是完整的帖子,没有摘录。 (我将暂时留在 adorkableme.com)
      【解决方案3】:

      阅读这篇文章 -> https://www.nosegraze.com/featured-post-homepage/

      您可以执行此操作的方法是查找最新帖子并使用 the_content() 显示该帖子;如果帖子不是最新帖子,则应显示 the_excerpt。

      希望这是有道理的。

      我想我也会更新答案。看起来您实际上并没有在寻找最新的帖子,所以我很快就提出了要点。它可能不起作用,因为我没有测试它,但你明白了它的要点(哈)

      <?php 
        if ( have_posts() ) : 
        $i = 0; 
      ?>
      <div id="primary" class="content-area">
          <main id="main" class="site-main" role="main">
      
              <section class="row posts-loop <?php if ('grid' == $blog_layout) { echo esc_attr('flex-row'); } ?>"
      
                  <?php /* Start the Loop */
                  while ( have_posts() ) : the_post(); 
      
                    if ( $i == 0 && ! is_paged() ) {
                    ?>
                        <div class="post-wrapper <?php echo $post_column; ?>">
                          <?php get_template_part( 'template-parts/post/content', $post_template ); ?>
                      </div>
                    <?php
                      } else {                
                            get_template_part( 'template-parts/post/content', 'none' ); 
                       }
                     $i++; 
                     endwhile; 
                  ?>
              </section>
              <?php the_posts_navigation(); ?>
      
          </main><!-- #main -->
      </div><!-- #primary -->
      

      【讨论】:

      • 所以我尝试了那个作者所说的......但是......它仍然无法识别计数器。我不确定我是否没有将&lt;?php $i = 0; ?&gt;&lt;?php $i++; ?&gt; 放在错误的位置或什么地方。
      • @RiniCS 你能用你更新的代码开始一个codepen或一个要点或其他东西,这样我就可以看到你在做什么?
      • @RiniCS 无法回复您的上述代码导致声誉/耸肩,但据我所知,您仍然没有检查第一篇文章。我 100% 怀疑这会起作用,因为没有测试它,但它会给你一个想法gist.github.com/JordanNZ/52c83902dfbec62c7826dfffe52201d0
      • 我尝试了你的要点......我一直在尝试那篇文章所说的如何处理计数器代码......但它仍然无法正常工作。我想我现在只能放弃了。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-05
      相关资源
      最近更新 更多