【问题标题】:Wordpress query posts with thumbnail images and recent post titles带有缩略图和最近帖子标题的 Wordpress 查询帖子
【发布时间】:2014-02-24 10:10:16
【问题描述】:

我是 wordpress 主题开发的初学者。我正在开发一个主题。我遇到了一个问题。

在我的 index.php 页面中,我希望将最新帖子显示为缩略图。第二、第三和第四个最新帖子仅显示标题。我还有五个类别。我希望帖子显示类别明智。

现在该怎么做。谁能帮帮我???

【问题讨论】:

    标签: wordpress


    【解决方案1】:

    在创建主题之前,请先查看 wordpress 查询:http://codex.wordpress.org/Class_Reference/WP_Query。关于问题。试试这个:

    // The Query
    $args = array('post_type' => 'post', 'posts_per_page' => 5);
    $the_query = new WP_Query( $args );
    $count = 0;
    // The Loop
    if ( $the_query->have_posts() ) {
            echo '<ul>';
        while ( $the_query->have_posts() ) {
            $the_query->the_post();
            if($count == 0){
                the_post_thumbnail();
            }else{
                echo get_the_title();
            }
    
            $count++;
        }
            echo '</ul>';
    } else {
        // no posts found
    }
    /* Restore original Post Data */
    wp_reset_postdata();
    

    干杯!

    【讨论】:

    • 谢谢。但是如果我有不同的类别假设,首页,第一个博客,第二个博客,第三个博客等,我想根据类别发布帖子,那么我该如何编辑查询。
    【解决方案2】:

    请使用此代码进行测试:

    <?php $the_query = new WP_Query('posts_per_page=5&cat=18');
    $count = 0;
    // The Loop
    if ( $the_query->have_posts() ) {
            echo '<ul>';
        while ( $the_query->have_posts() ) : $the_query->the_post();
            if($count == 0){?>
    
                <?php the_post_thumbnail('medium'); ?>
    
    
    
                                    <a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( '%s', 'twentyeleven' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark">
                                        <?php the_title();  ?>
                                    </a>
    
        <?php
            }else{ ?>
               <li class="list">
    
                                    <a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( '%s', 'twentyeleven' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark">
                                        <?php the_title();  ?>
                                    </a>
                            </li>
    
                <?php
            }
    
            $count++;
             echo "</li>";
        endwhile;
    
    } else {
        echo "No Post Found!";
    }
    /* Restore original Post Data */
    wp_reset_postdata();
    
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-04
      • 2015-07-19
      • 1970-01-01
      • 2011-03-15
      • 1970-01-01
      • 2012-09-27
      • 1970-01-01
      • 2013-07-17
      相关资源
      最近更新 更多