【问题标题】:Wordpress display thousands of postsWordpress 显示数千个帖子
【发布时间】:2016-01-05 16:45:07
【问题描述】:

我正在做一个项目,我需要列出所有类别、子类别(儿童、孙子等到大约 5 级)和其中的帖子。目前我已经设法让该网站正常运行,但问题是该网站需要几分钟才能加载。

为了明确我希望网站的外观,这里有一张图:

类别 子类别 子类别 内容 子类别 内容 子类别 你得到了图片

这是代码:

<?php
    function listing($parentcat, $list_id='', $list_name='', $path=false) {
        // parentcat is the desired category parent category, which is defined because the function is called a few times with different sets on the page.

        // list_id, list_name and path are used for purposes not related to the question

            echo "<ul><li name='$list_name'><h2><a href='#$list_name'>$list_name</a></h2>";
        }

        $args = array(
            'parent' => $parentcat,
            'include' => $cat_ids,
            'hide_empty' => 1,
            'orderby' => 'id'
        );

        $categories = get_categories( $args );
        echo "<ul>";
            foreach ($categories as $cat) {
                if ($cat->cat_name != 'Uncategorized') {

                    $flat_path = substr(get_category_parents($cat->cat_ID, false, ' &raquo;' ), 14);
                    $catnam = $cat->cat_name;

                    $listtitle = ($path ? $flat_path : $catnam);
                    echo ('<li name="' . $cat->cat_ID . '"><h2><a href="#' . $cat->cat_ID . '">' . $listtitle . '</a></h2>' );

                    if (get_posts( "category_name=$catnam" ) ) {
                        if (have_posts()) : while (have_posts()) : the_post();
                        if (in_category($cat->cat_ID)) {
                                echo '<ul><li><div>';
                                    the_content();
                                echo '</div></li></ul>';
                        } endwhile;
                        else :
                        _e('Empty list');
                        endif;
                    }

                    // Here's a recursive call for the function
                    listing($cat->cat_ID);
                    echo '</li>';
                }
            }
        echo '</ul>';
    }
    ?>

【问题讨论】:

标签: php wordpress loops


【解决方案1】:

您可能缺少帖子的“posts_per_page”

看看这里http://codex.wordpress.org/Function_Reference/get_posts

&

类别的“编号”

看看这里http://codex.wordpress.org/Function_Reference/get_categories

【讨论】:

  • 在正常情况下会这样做。问题是,我需要一次加载所有内容。这绝对不是什么好的做法等,但网站的逻辑要求它们全部一次加载。
  • 您的问题是页面加载速度问题?如果是,那么分页就是解决方案。
【解决方案2】:

找到解决方案!我会回答自己,所以如果其他人需要类似的东西,答案就在这里。

基本上,重点是首先创建一个包含所有类别的数组。之后,可以简单地循环遍历数组。这样我们就可以避免递归使用循环,这可能是之前解决方案中最大的问题。

即使这个解决方案确实很慢,但比上一个解决方案有了令人难以置信的改进。第一个想法在大约 30 秒后超时前加载了几百个帖子。这个在 5-10 秒内获得了整个网站(大约 1500 个帖子)。速度很糟糕,但我们网站的使用方式以及从划分所有内容到单独帖子的附加功能超过了速度问题。

$categories = get_categories('orderby=id');

    $cats_by_parent = array();

    foreach ($categories as $cat) {
        $parent_id = $cat->category_parent;
        if (!array_key_exists($parent_id, $cats_by_parent)) {
            $cats_by_parent[$parent_id] = array();
        }
        $cats_by_parent[$parent_id][] = $cat;
    }

    $posts = get_posts(['posts_per_page' => 10000000]);
    $posts_by_cats = array();

    foreach ($posts as $post) {
        $cat_id = wp_get_post_categories($post->ID)[0];
        $posts_by_cats[$cat_id] = $post->post_content;
    }

// Loop through the array and print with:
    echo $posts_by_cats[$cat->cat_ID];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-10
    • 2012-10-02
    • 2019-07-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多