0

I have made a widget in my functions.php, like this:

class Categories_Widget extends WP_Widget
{
  function __construct() {
        parent::__construct(
            'categories_widget', // Base ID
          ('Categoriën tonen'), // Name
            array( 'description' => ( 'Toon categoriën' ), ) // Args
        );
    }
  function widget($args, $instance)
  {
    extract($args, EXTR_SKIP);

    echo $before_widget;
    //echo "<h1>This is my new widget!</h1>";
    include 'inc/categories.php';
    echo $after_widget;
  }
}
add_action( 'widgets_init', create_function('', 'return register_widget("Categories_Widget");') );

Which fully works. In this widget, I include the categories.php file, which looks like this:

<?php

    $categories = get_categories();

    foreach ( $categories as $category ) {
      ?>
      <div class="categories-block category-<?php echo $category->name ?>">
        <h4><?php echo $category->name ?></h4>
        <ul>
        <?php
        $catId = $category->term_id;
        global $post;
        $args = array( 'category' => $catId );
        $posts = get_posts( $args );
        foreach ( $posts as $post ) {
          setup_postdata($post);
          ?>
          <li><a href="<?php the_permalink() ?>"><?php the_title() ?></a></li>
          <?php
        }
        wp_reset_postdata();
        ?>
        </ul>
      </div>
      <?php
    }

For some reason, it shows the category name, but not the query I want to create.

1

For the type of your $catId, you must use the cat parameter.

You can check here the differents parameters allowed.

cat (int) - use category id.

category_name (string) - use category slug.

category__and (array) - use category id.

category__in (array) - use category id.

category__not_in (array) - use category id.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.