【问题标题】:How to show thumbnails of posts in a category at the end of a single post?如何在单个帖子的末尾显示类别中帖子的缩略图?
【发布时间】:2021-06-25 04:05:00
【问题描述】:

我昨天发布了这个,但是当我注册后我再也看不到问题了,所以如果它现在在这里出现两次,请道歉!

我试图在单个页面的末尾显示一个缩略图网格。我的“工作”页面上的网格工作正常,这是一个类别页面。它们使用下面的代码显示为缩略图网格。你可以在网站上看到Here

<?php
get_header(); ?>

<div class="grid work thumb-wrap clearfix">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();?>
                    

        <a class="thumb" href="<?php the_permalink(); ?>">
            <img src="http://www.nathanspence.com/wp-content/uploads/<?php echo get_post_meta($post->ID, 'thumb', true); ?>"/>
            <div class="post-excerpt">
                <h2><?php echo get_the_title($ID); ?></h2>
                <div class="sub-title"><?php echo get_post_meta($post->ID, 'project-name', true); ?></div>
            </div>      
        </a>

                    
        <?php endwhile; ?>
        <?php endif; ?>

</div><!--end of "thumb-wrap"-->

<div class="push"></div>
    
</div><!--end of "page-wrap"--> 

<?php get_footer(); ?>

我想在每篇文章的末尾复制这个。 Here.

<?php get_header(); ?>

<div class="page-content">

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

<article class="work post-content ready-to-column clearfix">
                
    <header class="post-header column-left">
        <h1><?php echo get_the_title($ID); ?></h1>
        <h2><?php echo get_post_meta($post->ID, 'project-name', true); ?></h2>
        <div class="project-info"><?php echo get_post_meta($post->ID, 'project-info', true); ?></div>                   
    </header>
    
    <div class="column-right">
    <?php the_content(); ?>
    
        

    
    
    
    </div><!---end of "right-column flex-column"--->

            
</article>
    
<?php endwhile; else: ?>    
<p>Sorry, this post does not exist</p>
<?php endif; ?>
    
    <div class="grid work thumb-wrap clearfix">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();?>
                    

        <a class="thumb" href="<?php the_permalink(); ?>">
            <img src="http://www.nathanspence.com/wp-content/uploads/<?php echo get_post_meta($post->ID, 'thumb', true); ?>"/>
            <div class="post-excerpt">
                <h2><?php echo get_the_title($ID); ?></h2>
                <div class="sub-title"><?php echo get_post_meta($post->ID, 'project-name', true); ?></div>
            </div>      
        </a>

                    
        <?php endwhile; ?>
        <?php endif; ?>

</div><!--end of "thumb-wrap"-->

<div class="push"></div>
    
</div><!--end of "page-wrap"--> 

<?php get_footer(); ?>

我想在单个帖子的末尾显示此网格。我试过只是粘贴相同的循环,但正如你所看到的,简单地粘贴它只会显示当前帖子的缩略图。知道我该怎么做吗?我是一名刚刚学习能够维护自己的网站的设计师,所以我在阅读循环等方面有点迷茫。谢谢!

编辑:为了更清楚。如何获取下面的代码以在单个帖子末尾输出该类别中的所有帖子?

<a class="thumb" href="<?php the_permalink(); ?>">
            <img src="http://www.nathanspence.com/wp-content/uploads/<?php echo get_post_meta($post->ID, 'thumb', true); ?>"/>
            <div class="post-excerpt">
                <h2><?php echo get_the_title($ID); ?></h2>
                <div class="sub-title"><?php echo get_post_meta($post->ID, 'project-name', true); ?></div>
            </div>      
        </a>

【问题讨论】:

    标签: php wordpress loops categories


    【解决方案1】:

    尝试将其添加到您想要所有帖子的位置

    <?php 
    $wpb_all_query = new WP_Query(array('post_type'=>'post', 'post_status'=>'publish', 'posts_per_page'=>-1)); ?>
     
    <?php if ( $wpb_all_query->have_posts() ) : ?>
     
    <ul>
     
        <!-- the loop -->
        <?php while ( $wpb_all_query->have_posts() ) : $wpb_all_query->the_post(); ?>
            <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
        <?php endwhile; ?>
        <!-- end of the loop -->
     
    </ul>
     
        <?php wp_reset_postdata(); ?>
     
    <?php else : ?>
        <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
    <?php endif; ?>
    

    【讨论】:

    • 感谢您的回复。在我当前代码的上下文中,这会去哪里?我现在有点迷路了。如果你能告诉我把它放在哪里以及从我当前的代码中取出什么,那将是一个巨大的帮助! :)
    • @NathanSpence 上面的代码将出现在&lt;?php get_footer(); ?&gt; 行之前。你只需要修复一点设计
    • 好的。网格缩略图的代码在哪里适合? &lt;a class="thumb" href="&lt;?php the_permalink(); ?&gt;"&gt; &lt;img src="http://www.nathanspence.com/wp-content/uploads/&lt;?php echo get_post_meta($post-&gt;ID, 'thumb', true); ?&gt;"/&gt; &lt;div class="post-excerpt"&gt; &lt;h2&gt;&lt;?php echo get_the_title($ID); ?&gt;&lt;/h2&gt; &lt;div class="sub-title"&gt;&lt;?php echo get_post_meta($post-&gt;ID, 'project-name', true); ?&gt;&lt;/div&gt; &lt;/div&gt; &lt;/a&gt;
    • @NathanSpence 你可以用你的替换&lt;li&gt;&lt;a href="&lt;?php the_permalink(); ?&gt;"&gt;&lt;?php the_title(); ?&gt;&lt;/a&gt;&lt;/li&gt; 代码。
    • 是的,我这样做并删除了 UL 标签,只是因为我没有将这些项目作为列表。也许他们应该是,但它有效。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-29
    • 2013-07-02
    • 2021-10-14
    • 1970-01-01
    • 1970-01-01
    • 2017-08-02
    • 1970-01-01
    相关资源
    最近更新 更多