简体   繁体   English

如何在Wordpress中获取和显示主页上标签的特定帖子

[英]How to fetch and show particular posts for a tag on home page in wordpress

I am trying to fetch and show all posts in a featured listing style for tag my-guide 我正在尝试以标记为my-guide的精选列表样式获取并显示所有帖子

<?php if(have_posts()) : ?><?php while(have_posts()) : the_post(); ?>
    <div class="post" id="post-<?php the_ID(); ?>">
    <div class="thumbnail">
    <a href="<?php the_permalink(); ?>">
    <?php the_post_thumbnail(); ?>
    </a>
    </div>
    <?php endwhile; ?>
    <?php endif; ?>

This code fetch all featured image from posts, however I am trying to fetch it via particular tag, I tried this also but its not working for tag query_posts('tag=my-guide'); 这段代码从帖子中获取所有特色图片,但是我试图通过特定的标签获取它,我也尝试过,但是不适用于标签query_posts('tag=my-guide'); - --

    <?php query_posts('tag=my-guide'); 
  while (have_posts()) : the_post();  
?>  
<div class="breaking">  
    <img src="<?php echo get_post_meta($post->ID, 'thumbnail',true) ?>" alt="Post Image" class="postimg" />  
    <h2><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>  
    <p class="datetime"><?php the_time('l, F j, Y G:i'); ?></p>  
    <?php the_content('Continue...'); ?>  
    <div class="postmeta">  
        <p><?php the_category(', '); ?> - <a href="<?php the_permalink() ?>#commenting" title="View Comments">  
    <span class="comm"><?php comments_number('0 Comments','1 Comment','% Comments'); ?></span></a></p>  
    </div><!--/postmeta-->  
</div><!--/breaking-->  
<?php endwhile; ?>  

Source of this -> http://net.tutsplus.com/tutorials/wordpress/build-a-featured-posts-section-for-wordpress/ 来源-> http://net.tutsplus.com/tutorials/wordpress/build-a-featured-posts-section-for-wordpress/

I fixed the code on my own - 我自己修改了代码-

The problem with the second code block is that, no value is pouring in for this - $post->ID 第二个代码块的问题在于,没有任何值可输入- $post->ID

So this method call is not doing any good in the code - 因此,此方法调用在代码中没有任何好处-

<?php echo get_post_meta($post->ID, 'thumbnail',true) ?>

To fix this, simply call, <?php the_post_thumbnail(); ?> 要解决此问题,只需调用<?php the_post_thumbnail(); ?> <?php the_post_thumbnail(); ?> instead of <img src="<?php echo get_post_meta($post->ID, 'thumbnail',true) ?>" alt="Post Image" class="postimg" /> <?php the_post_thumbnail(); ?>代替<img src="<?php echo get_post_meta($post->ID, 'thumbnail',true) ?>" alt="Post Image" class="postimg" />

So this is the working code in general for a particular tag post display - 因此,这general是特定标记发布显示的工作代码-

<?php query_posts('tag=your-tag'); ?>
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="thumbnail">  
        <?php the_post_thumbnail(); ?>
        <h2><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>  
        <p class="datetime"><?php the_time('l, F j, Y G:i'); ?></p>  
        <?php the_content('Continue...'); ?>  
        <div class="postmeta">  
            <p><?php the_category(', '); ?> - <a href="<?php the_permalink() ?>#commenting" title="View Comments">  
        <span class="comm"><?php comments_number('0 Comments','1 Comment','% Comments'); ?></span></a></p>  
        </div>
    </div>  
<?php endwhile; endif; ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM