unordered list loop not showing up in sidebar

admin2025-06-05  3

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();
?>

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();
?>

Share Improve this question asked Dec 21, 2018 at 21:48 marilynnmarilynn 51 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 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',
);
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749077019a316160.html

最新回复(0)