Wordpress显示成千上万的帖子


Wordpress display thousands of posts

我正在做一个项目,我需要列出所有的类别,子类别(孩子,孙子等约5级)和他们里面的帖子。目前我已经设法使网站工作,但问题是网站需要几分钟才能加载。

为了明确我想要的网站看起来像什么,这里有一个绘图:

类别子范畴Subsubcategory内容Subsubcategory内容子范畴你得到的图片

代码如下:

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

您可能缺少post ' s_per_page'

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

"number"表示类别

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

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

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

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

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