首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP Counting与相关文章

PHP Counting与相关文章
EN

Stack Overflow用户
提问于 2016-02-29 15:56:55
回答 1查看 110关注 0票数 2

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

代码语言:javascript
复制
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 -->";   
EN

回答 1

Stack Overflow用户

发布于 2016-02-29 19:28:16

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

那是

代码语言:javascript
复制
$id = $related_articles[$i-1];
if(get_the_ID() != $id){
   //then do the stuff
}

我希望你在详细信息或single.php页面上,这样你就可以通过get_the_ID()获得原始帖子的ID

代码语言:javascript
复制
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 -->"; 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35694427

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档