loop - Creating select dropdown with parent-level custom post types

admin2025-06-05  3

I'm trying to create a dropdown with an option for each top-level parent. I've gotten the query down because when I print the results, I see the posts I'm looking for. My code is also creating the right number of options, but it's not entering the info as I'd expect. Here is the code I have:

<select class="filters-select">
    <option value="*">Show All</option>
    <?php
        $args = array(
            'post_type' => 'locations',
            'post_status' => 'publish',
            'order_by' => 'title',
            'order' => 'asc',
            'post_parent' => 0,
            'posts_per_page' => -1
        );
        $posts = get_posts( $args );
        foreach ( $posts as $post ) {
            echo "<option value='." . $post->slug . "' class='" . $post->slug . "'>" . $post->name . "</option>\n";
        } ?>                
</select>

I'm trying to create a dropdown with an option for each top-level parent. I've gotten the query down because when I print the results, I see the posts I'm looking for. My code is also creating the right number of options, but it's not entering the info as I'd expect. Here is the code I have:

<select class="filters-select">
    <option value="*">Show All</option>
    <?php
        $args = array(
            'post_type' => 'locations',
            'post_status' => 'publish',
            'order_by' => 'title',
            'order' => 'asc',
            'post_parent' => 0,
            'posts_per_page' => -1
        );
        $posts = get_posts( $args );
        foreach ( $posts as $post ) {
            echo "<option value='." . $post->slug . "' class='" . $post->slug . "'>" . $post->name . "</option>\n";
        } ?>                
</select>
Share Improve this question asked Dec 18, 2018 at 3:39 lushiris02lushiris02 357 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Found the answer. I was using the terms nomenclature to call my info. Had to update to:

echo "<option value='." . $post->post_name . "'>" . $post->post_title . "</option>\n";

And now it's working just fine. (I also eliminated class cause I didn't need it here.)

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749088881a316268.html

最新回复(0)