0

我正在尝试为我的博客创建一个小型 wp 插件,但我遇到了以下问题。

帖子图像未显示在正确的位置。

这是正确的 HTML

<li>

    <div class="projects">

        <ul class="projects sticker">
        <li><h2><?php the_title(); ?></h2></li>
        <li><p><a href="">details</a></p></li>
        </ul>
        <img src="" />

    </div>

    </li>

这就是它现在的显示方式

      <li>

    <div class="projects">

        <ul class="projects sticker">
        <li><h2><?php the_title(); ?></h2></li>
        <li><p><a href="">details</a></p></li>
        </ul>


    </div>

    </li> 
    <img src="" /> 

基本上我必须将 img 标签放在列表和 div 中

到目前为止,这是我的代码

        $args = array( 'numberposts' => '3','category' => $cat_id );
        $recent_posts = wp_get_recent_posts( $args );
        foreach( $recent_posts as $recent ){
         echo '<li>'
     . '<div class="projects">'
     . '<ul class="projects sticker">'
     . '<li>'
     . '<h2>'
     . '<a href="' . get_permalink($recent["ID"]) . '" title="Look   '.esc_attr($recent["post_title"]).'" >'
     . $recent["post_title"]
     . '</a>'
     . '</h2>'
     . '</li>'
     . '<li><p><a href="">details</a></p></li>'
     . '</ul>'
     . '<img src="'.the_post_thumbnail('thumbnail').'" />'
     . '</div>'
     . '</a>'; 
4

2 回答 2

2

使用此代码,您使用了额外的<li></li>

$args = array( 'numberposts' => '3','category' => $cat_id );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
     echo '<a href="' . get_permalink($recent["ID"]) . 
          '" title="Look   '.esc_attr($recent["post_title"]).'" >'  
          .'<div class="projects">' .'<ul class="projects sticker">'       
          .'<li>' .'<h2>' .   $recent["post_title"] .'</h2>' .'</li>' 
          .'<li><p><a href="">details</a></p></li></ul>' 
          .'<img src="'.the_post_thumbnail('thumbnail').'" />'  
          .'</div>' .'</a>'; 
}
于 2013-02-13T10:46:25.850 回答
1

最后有一个额外<li>的结束,第一个结束标记的位置<li>嵌套不正确,并且<a href>开始和结束标记也放错了位置。此外,如果您格式化代码以便人类更容易阅读,您可能更容易解决这个问题 - 可能是您自己。像这样在一行上堆积一堆指令只会引起混乱:

        $args = array( 'numberposts' => '3','category' => $cat_id );
        $recent_posts = wp_get_recent_posts( $args );
        foreach( $recent_posts as $recent ){
         echo '<li>'
         . '<div class="projects">'
         . '<ul class="projects sticker">'
         . '<li>'
         . '<h2>'
         . '<a href="' . get_permalink($recent["ID"]) . '" title="Look   '.esc_attr($recent["post_title"]).'" >'
         . $recent["post_title"]
         . '</a>'
         . '</h2>'
         . '</li>'
         . '<li><p><a href="">details</a></p></li>'
         . '</ul>'
         . '<img src="'.the_post_thumbnail('thumbnail').'" />'
         . '</div>'
         . '</a>'
         ; 
于 2013-02-13T10:50:22.250 回答