wordpress - Php loop through categories (and generated urls for all post in)

817

I am rather new to php (dont get much further than tweaking some code) So what i want is for every category (except category id 1, which is uncategorized) I want the category url + category name And for each post within the category, i want the url + title (loop max 3 times)

what i have so far is:

<div >
  <div >
    <div >
      <h3 >
        <a href="<?php echo get_category_link( "5" );?>">
          <?php echo get_cat_name(5);?>
        </a>
      </h3>
    </div>

    <div >
      <ol>
        <?php
          $args = array('category' => 5, 'post_type' => 'post'); 
          $postslist = get_posts($args);  
          $i = 0; 

          foreach ($postslist as $post) : setup_postdata($post);{if(++$i > 3) break;} 
        ?> 

            <li> 
              <a href="<?php the_permalink(); ?>">
                <?php the_title(); ?>
              </a>
            </li> 

           <?php endforeach; ?> 
    </div>
  </div>
</div>
</ol>

This does what i want perfectly, except it does it only for category id 5 ofcourse. Is there an easy way to make this loop for all categories?

Edit: Basicly i want the code to look through all categories instead of just do it for category 5 (we add/remove categories, so hardcoding the numbers isn't usefull)

699

Answer

Solution:

As you don't want to hardcode the category IDs, you need to fetch them throughget_all_category_ids and loop over the result. This could work:

<div >
  <div >
    <?php
    $categoryIds = get_all_category_ids();
    foreach($categoryIds as $categoryId): ?>
    <div >
      <h3 >
        <a href="<?php echo get_category_link($categoryId); ?>">
          <?php echo get_cat_name($categoryId); ?>
        </a>
      </h3>
    </div>

    <div >
      <ol>
        <?php
        $args = array('category' => $categoryId, 'post_type' => 'post');
        $postslist = get_posts($args);
        $i = 0;

        foreach ($postslist as $post) : setup_postdata($post);
          {
            if (++$i > 3) {
              break;
            }
          }
          ?>

          <li>
            <a href="<?php the_permalink(); ?>">
              <?php the_title(); ?>
            </a>
          </li>

        <?php endforeach; ?>
      </ol>
    </div>
    <?php endforeach; ?>
  </div>
</div>
37

Answer

Solution:

This should work

<?php
$args = array( 'category__and' => array(2, 3, 4, 5), 'post_type' =>  'post' );
$i = 0; 
$postslist = get_posts( $args );   
foreach ($postslist as $post) {
    setup_postdata($post); 
    if (++$i > 4) break;
    ?>
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> 
<?php } ?>

People are also looking for solutions to the problem: php - How to return Content-Length header for TWIG rendered page?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.