【问题标题】:wordpress image isn't displaying where it supposed towordpress 图像没有显示在它应该显示的位置
【发布时间】:2013-01-28 21:17:03
【问题描述】:

我正在尝试为我的博客创建一个小型 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>'; 

【问题讨论】:

    标签: php html wordpress plugins


    【解决方案1】:

    使用这个代码,你已经使用了额外的&lt;li&gt;&lt;/li&gt;

    $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>'; 
    }
    

    【讨论】:

    • @user2013488 种子编辑答案,关闭h2 后,您已关闭li
    • 这里的结果相同。 img 在列表之内,但在 div 之外
    【解决方案2】:

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

            $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>'
             ; 
    

    【讨论】:

    • 谢谢。现在它在列表之内,但在 div 之外。我在 html 中查找了其他语法错误,但没有找到。
    • 抱歉,如果没有您的其余代码,我不太确定您希望任何人提供帮助。我对你最好的建议是简单地清理你的代码。我敢打赌问题会得到解决,因为它太乱了。
    • 愚蠢的我,我发现了我的错误。是的,我的代码一团糟,因为我有这个习惯,可以在代码完成后清理我的代码。这是我会尝试解决的问题。再次感谢您。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-28
    • 1970-01-01
    • 2013-09-12
    • 1970-01-01
    相关资源
    最近更新 更多