Skip to main content
added 10 characters in body
Source Link
tfrommen
  • 9.2k
  • 7
  • 39
  • 59

The number of posts is set to 5 by default, so you have to set it to your liking. If you want to show all posts, then it's -1, and the number you want otherwise.

You should also put functions out of loops (as in your second foreach), so here is your code, optimized:

<?php  
$args = array(
    'child_of' => 1,
);
$categories = get_categories($args);
if (count($categories)) {
    ?>
    <ul>
    <?php
    foreach ($categories as $category) {
        ?>
        <li><a><?php echo $category->name; ?></a>
        <ul>
        <?php
        $args = array(
            'posts_per_page' => -1, // query ALL posts
            'post_status' => 'publish',
            'cat' => $category->term_id,
        );
        $query = new WP_Query($args);
        while ($query->have_posts()) {
            $query->the_post();
            ?>
            <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
            <?php
        }
        wp_reset_postdata();
        ?>
        </ul></li>
        <?php
    }
    ?>
    </ul>
    <?php
}
?>

The number of posts is set to 5 by default, so you have to set it to your liking. If you want to show all posts, then it's -1, and the number you want otherwise.

You should also put functions out of loops (as in your second foreach), so here is your code, optimized:

<?php  
$args = array(
    'child_of' => 1,
);
$categories = get_categories($args);
if (count($categories)) {
    ?>
    <ul>
    <?php
    foreach ($categories as $category) {
        ?>
        <li><a><?php echo $category->name; ?></a>
        <ul>
        <?php
        $args = array(
            'posts_per_page' => -1, // query ALL posts
            'post_status' => 'publish',
            'cat' => $category->term_id,
        );
        $query = new WP_Query($args);
        while ($query->have_posts()) {
            $query->the_post();
            ?>
            <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
            <?php
        }
        wp_reset_postdata();
        ?>
        </ul></li>
    }
    ?>
    </ul>
    <?php
}
?>

The number of posts is set to 5 by default, so you have to set it to your liking. If you want to show all posts, then it's -1, and the number you want otherwise.

You should also put functions out of loops (as in your second foreach), so here is your code, optimized:

<?php  
$args = array(
    'child_of' => 1,
);
$categories = get_categories($args);
if (count($categories)) {
    ?>
    <ul>
    <?php
    foreach ($categories as $category) {
        ?>
        <li><a><?php echo $category->name; ?></a>
        <ul>
        <?php
        $args = array(
            'posts_per_page' => -1, // query ALL posts
            'post_status' => 'publish',
            'cat' => $category->term_id,
        );
        $query = new WP_Query($args);
        while ($query->have_posts()) {
            $query->the_post();
            ?>
            <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
            <?php
        }
        wp_reset_postdata();
        ?>
        </ul></li>
        <?php
    }
    ?>
    </ul>
    <?php
}
?>
added 7 characters in body
Source Link
tfrommen
  • 9.2k
  • 7
  • 39
  • 59

The number of posts is set to 5 by default, so you have to set it to your liking. If you want to show all posts, then it's -1, and the number you want otherwise.

You should also put functions out of loops (as in your second foreach), so here is your code, optimized:

<?php  
$args = array(
    'child_of' => 1,
);
$categories = get_categories($args);
if (count($categories)) {
    ?>
    <ul>
    <?php
    foreach ($categories as $category) {
        ?>
        <li><a><?php echo $category->name; ?></a>
        <ul>
        <?php
        $args = array(
            'posts_per_page' => -1, // query ALL posts
            'post_status' => 'publish',
            'cat' => $category->term_id,
        );
        $query = new WP_Query($args);
        while ($query->have_posts()) {
            $query->the_post();
            ?>
            <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
            <?php
        }
        wp_reset_postdata();
        ?>
        </ul></li>
    }
    ?>
    </ul>
    <?php
}
?>

The number of posts is set to 5 by default, so you have to set it to your liking. If you want to show all posts, then it's -1, and the number you want otherwise.

You should also put functions out of loops (as in your second foreach), so here is your code, optimized:

<?php  
$args = array(
    'child_of' => 1,
);
$categories = get_categories($args);
if (count($categories)) {
    ?>
    <ul>
    <?php
    foreach ($categories as $category) {
        <li><a><?php echo $category->name; ?></a>
        <ul>
        <?php
        $args = array(
            'posts_per_page' => -1, // query ALL posts
            'post_status' => 'publish',
            'cat' => $category->term_id,
        );
        $query = new WP_Query($args);
        while ($query->have_posts()) {
            $query->the_post();
            ?>
            <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
            <?php
        }
        wp_reset_postdata();
        ?>
        </ul></li>
    }
    ?>
    </ul>
    <?php
}
?>

The number of posts is set to 5 by default, so you have to set it to your liking. If you want to show all posts, then it's -1, and the number you want otherwise.

You should also put functions out of loops (as in your second foreach), so here is your code, optimized:

<?php  
$args = array(
    'child_of' => 1,
);
$categories = get_categories($args);
if (count($categories)) {
    ?>
    <ul>
    <?php
    foreach ($categories as $category) {
        ?>
        <li><a><?php echo $category->name; ?></a>
        <ul>
        <?php
        $args = array(
            'posts_per_page' => -1, // query ALL posts
            'post_status' => 'publish',
            'cat' => $category->term_id,
        );
        $query = new WP_Query($args);
        while ($query->have_posts()) {
            $query->the_post();
            ?>
            <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
            <?php
        }
        wp_reset_postdata();
        ?>
        </ul></li>
    }
    ?>
    </ul>
    <?php
}
?>
Source Link
tfrommen
  • 9.2k
  • 7
  • 39
  • 59

The number of posts is set to 5 by default, so you have to set it to your liking. If you want to show all posts, then it's -1, and the number you want otherwise.

You should also put functions out of loops (as in your second foreach), so here is your code, optimized:

<?php  
$args = array(
    'child_of' => 1,
);
$categories = get_categories($args);
if (count($categories)) {
    ?>
    <ul>
    <?php
    foreach ($categories as $category) {
        <li><a><?php echo $category->name; ?></a>
        <ul>
        <?php
        $args = array(
            'posts_per_page' => -1, // query ALL posts
            'post_status' => 'publish',
            'cat' => $category->term_id,
        );
        $query = new WP_Query($args);
        while ($query->have_posts()) {
            $query->the_post();
            ?>
            <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
            <?php
        }
        wp_reset_postdata();
        ?>
        </ul></li>
    }
    ?>
    </ul>
    <?php
}
?>