猿问

每个函数的 if 语句

$args = array( 'numberposts' => '3' ); 

$recent_posts = wp_get_recent_posts( $args );


foreach( $recent_posts as $recent ){


    echo '<a style="color:white;text-decoration:none;" href="'. get_permalink($recent["ID"]) .'">

    <article class="post post_home" style="background-image: url('. 

    function(){ 

        if( has_post_thumbnail() ) {

            echo get_the_post_thumbnail_url( $recent["ID"], 'cover' );

        } elseif ( has_category( 'positive-morning' ) ) {

            echo get_bloginfo('template_directory') . '/img/BG/2-Morning.jpg'; 

        } elseif ( has_category( 'positive-talks' ) ) {

            echo get_bloginfo('template_directory') . '/img/BG/2-Talks.jpg'; }

        } 

    .'); 

    background-position : center; background-size :cover;">

    <h2>'. $recent["post_title"] .'</h2></article>';

    ....


}

大家好,


我无法放置 if 语句,因为当我添加应该声明帖子是否没有缩略图的 if 语句时,它应该获得显示为类别定义的缩略图的相对路径。


我尝试了不同的方法来使其工作,但我找不到问题所在。我得到的唯一错误是


闭包类的对象无法转换为字符串


谢谢你的帮助


拉风的咖菲猫
浏览 86回答 2
2回答

手掌心

function(){是一个函数定义,你不能连接它。此外,has_post_thumbnail实际上是您要使用的 WP 函数,它的第一个参数是帖子 ID。所以你应该重写你的代码如下。$args = array( 'numberposts' => '3' );&nbsp;$recent_posts = wp_get_recent_posts( $args );foreach( $recent_posts as $recent ){&nbsp; &nbsp; $output = '<a style="color:white;text-decoration:none;" href="'. get_permalink($recent["ID"]) . '">&nbsp; &nbsp; <article class="post post_home" style="background-image: url(';&nbsp;&nbsp;&nbsp; &nbsp; if( has_post_thumbnail($recent["ID"]) ) {&nbsp; &nbsp; &nbsp; &nbsp; $output .= get_the_post_thumbnail_url( $recent["ID"], 'cover' );&nbsp; &nbsp; } elseif ( has_category( 'positive-morning' ) ) {&nbsp; &nbsp; &nbsp; &nbsp; $output .= get_bloginfo('template_directory') . '/img/BG/2-Morning.jpg';&nbsp;&nbsp; &nbsp; } elseif ( has_category( 'positive-talks' ) ) {&nbsp; &nbsp; &nbsp; &nbsp; $output .= get_bloginfo('template_directory') . '/img/BG/2-Talks.jpg';&nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; $output .= ' background-position : center; background-size :cover;">&nbsp; &nbsp; <h2>'. $recent["post_title"] .'</h2></article>';&nbsp; &nbsp; echo $output;}

慕容708150

这里缺少一些东西。您没有在循环中设置 postdata 并且您也不需要其中的函数,而且我个人也不喜欢在 PHP 标记内将字符串连接在一起来制作 HTML,所以我使用ob_start()并ob_get_clean()构建 HTML。尝试这样的事情......<?php$args = array( 'numberposts' => '3' );$recent_posts = wp_get_recent_posts( $args );ob_start();foreach( $recent_posts as $recent ){&nbsp;&nbsp; &nbsp; setup_postdata($recent); ?>&nbsp; &nbsp; <a style="color:white;text-decoration:none;" href="<?php get_permalink($recent["ID"])?>">&nbsp; &nbsp; <article class="post post_home" style="background-image: url(<?php&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; if( has_post_thumbnail() ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo get_the_post_thumbnail_url( $recent["ID"], 'cover' );&nbsp; &nbsp; &nbsp; &nbsp; } elseif ( has_category( 'positive-morning' ) ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo get_bloginfo('template_directory') . '/img/BG/2-Morning.jpg';&nbsp; &nbsp; &nbsp; &nbsp; } elseif ( has_category( 'positive-talks' ) ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo get_bloginfo('template_directory') . '/img/BG/2-Talks.jpg';&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }; ?>&nbsp; &nbsp; ); background-position : center; background-size :cover;">&nbsp; &nbsp; <h2><?php echo $recent["post_title"] ?></h2></article>&nbsp; &nbsp; <?php&nbsp; &nbsp; wp_reset_postdata();}echo ob_get_clean(); ?>公平警告我没有测试任何代码
随时随地看视频慕课网APP
我要回答