PHP计数与相关文章

时间:2016-02-29 07:56:55

标签: php wordpress advanced-custom-fields

我的目标是在帖子下方显示相关文章(检查是否可用,然后循环浏览那些文章)。如果我有3或6个设置但是如果我只有5个,它会自动显示原始帖子作为第六个相关文章。

echo "<div class='related-posts'>";
    $related_articles = get_field('related_articles', false, false);
    $id = $related_articles[0];
    if($id) {
        echo "<h5 class='related-posts_h'>Ähnliche Artikel, die du lesen musst</h5>";

        echo "<ul class='related-posts_list clearfix'>";

                for ($i=1; $i <= 6; $i++) { 

                $id = $related_articles[$i-1];
                        if ( has_post_thumbnail($id) ) {
                        echo "<li class='related-posts_item'>";
                        echo "<figure class='featured-thumbnail thumbnail large'>";
                        echo "<div class='hider-page'></div>";
                            echo "<a href='".get_permalink($id)." title='".get_the_title($id)."'>";
                            echo get_the_post_thumbnail( $id, 'medium-thumb' );
                            echo "<span class='zoom-icon'></span>";
                            echo "</a>";
                        echo "</figure>";

                    } else { 
                        "<figure class='thumbnail featured-thumbnail'>";
                        echo "<div class='hider-page'></div>";
                            echo "<a href='".get_permalink($id)."' title='".get_the_title($id)."'>";
                            echo "<img src='".get_template_directory_uri()."/images/empty_thumb.gif' alt='".get_the_title($id)."' />";
                            echo "</a>";
                        echo "</figure>";
                }
                    echo "<h6><a href='".get_permalink($id)."'>";
                    echo get_the_title($id);
                    echo "</a>";
                    echo "</h6>";
                echo "</li>";
        }
        echo "</ul>";
echo "</div><!-- .related-posts -->";   

1 个答案:

答案 0 :(得分:0)

试试这个..在for循环后检查相关的帖子ID是否等于原始帖子ID然后只显示相关的帖子

$id = $related_articles[$i-1];
if(get_the_ID() != $id){
   //then do the stuff
}

我希望您在详细信息或single.php页面中,这样您就可以通过get_the_ID()

获取原始帖子的ID
echo "<div class='related-posts'>";
    $related_articles = get_field('related_articles', false, false);
    $id = $related_articles[0];
    if($id) {
        echo "<h5 class='related-posts_h'>Ähnliche Artikel, die du lesen musst</h5>";
        echo "<ul class='related-posts_list clearfix'>";

        for ($i=1; $i <= 6; $i++) { 
            $id = $related_articles[$i-1];
            if(get_the_ID() != $id){

                if ( has_post_thumbnail($id) ) {
                    echo "<li class='related-posts_item'>";
                    echo "<figure class='featured-thumbnail thumbnail large'>";
                    echo "<div class='hider-page'></div>";
                    echo "<a href='".get_permalink($id)." title='".get_the_title($id)."'>";
                    echo get_the_post_thumbnail( $id, 'medium-thumb' );
                    echo "<span class='zoom-icon'></span>";
                    echo "</a>";
                    echo "</figure>";
                } else { 
                    "<figure class='thumbnail featured-thumbnail'>";
                    echo "<div class='hider-page'></div>";
                        echo "<a href='".get_permalink($id)."' title='".get_the_title($id)."'>";
                        echo "<img src='".get_template_directory_uri()."/images/empty_thumb.gif' alt='".get_the_title($id)."' />";
                        echo "</a>";
                    echo "</figure>";
                }
                echo "<h6><a href='".get_permalink($id)."'>";
                echo get_the_title($id);
                echo "</a>";
                echo "</h6>";
                echo "</li>";
            }
        }
    echo "</ul>";
echo "</div><!-- .related-posts -->";