【问题标题】:PHP Counting With Related ArticlesPHP 计数与相关文章
【发布时间】:2016-06-12 04:33:06
【问题描述】:

我的目标是在帖子下方显示相关文章(检查是否可用,然后遍历那里的文章)。如果我有 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 -->";   

【问题讨论】:

  • 你看过这个question? (click here)if ($post)....foreach($post)...我觉得这样更有意义
  • 请提供$related_articles的输出

标签: php wordpress advanced-custom-fields


【解决方案1】:

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

那是

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

我希望你在 details 或 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 -->"; 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-08
    • 2016-08-23
    • 1970-01-01
    相关资源
    最近更新 更多