0

I have a custom sidebar named "sidebar-events" that should only display posts in the "Events" category. I placed the following code in the sidebar. The header appears, but not the unordered list. In the source code there's nothing - no ul, no li. What am I doing wrong?

<div id="sidebar" class="widgets-area">
<h2>What's Going On</h2>

<?php
    //what I will be looking for
    $args = array(
        "type" => post,
        "posts_per_page" => 5,
        "category_name" => "Events"
    );
    //create new query and pass the arguments
    $recentEvents = new WP_Query($args);

    if( $recentEvents->have_posts() ): ?>
        <ul>

        <?php while( $recentEvents->have_posts() ): $recentEvents->the_post(); ?>

            <li><?php the_title(); ?></li>

        <?php endwhile; ?>
        </ul>
    <?php endif; wp_reset_postdata();
?>

1 Answer 1

0

You want to use post_type instead of just type and put apostrophes around your post type itself.

Instead of this:

$args = array(
    "type" => post,
    "posts_per_page" => 5,
    "category_name" => "Events"
);

I would do this:

$args = array(
    'post_type' => 'post',
    'posts_per_page' => 5,
    'category_name' => 'Events',
);
2
  • This absolutely did the trick! I will now worship you like a God.
    – marilynn
    Dec 22, 2018 at 0:45
  • haha. No need for that. Just mark as the correct answer!
    – rudtek
    Dec 22, 2018 at 6:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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