【问题标题】:Wordpress featured image codeWordPress 特色图片代码
【发布时间】:2015-03-14 14:46:02
【问题描述】:

您好,我正在研究如何在我的自定义页面上将特色图片放入 wordpress 中。

这是我的代码:

            <?php

                    $args = array( 'numberposts' => '1' );
                    $recent_posts = wp_get_recent_posts( $args );
                    if (has_post_thumbnail() ) {
                    foreach( $recent_posts as $recent ){
                        the_post_thumbnail();
                        echo '<li><a href="' . get_permalink($recent["ID"]) . '">' .   $recent["post_title"].'</a> </li> ';
                    }
                }
                else {
                    echo "no featured post";
                }
            ?>

【问题讨论】:

    标签: php wordpress


    【解决方案1】:
    <?php
    
        $args = array( 'numberposts' => '1' );
        $recent_posts = wp_get_recent_posts( $args );
    
        if( $recent_posts ) {
            foreach( $recent_posts as $recent ){
                if (has_post_thumbnail($recent['ID']) ) {
                   echo get_the_post_thumbnail($recent['ID']);
                }
                echo '<li><a href="' . get_permalink($recent["ID"]) . '">' .   $recent["post_title"].'</a> </li> ';
            }
        } else {
            echo "no featured post";
        }
    ?>
    

    the_post_thumbnail 要求您处于循环状态。如果要使用自己的 ID 检索,请使用 get_the_post_thumbnail

    【讨论】:

      猜你喜欢
      • 2019-07-31
      • 1970-01-01
      • 2015-04-28
      • 2015-07-17
      • 2014-05-13
      • 2014-11-22
      • 2015-04-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多