get_categories(): how to get output
-
Hi there,
This is driving me mad!
I am trying to pass the value choosen from a dropdown into a second set of code. I have two ways of creating the dropdown , both of which work (I prefer the look of the first).
First way:<div class="gallery-row"> <li id="categories"> <h2><?php _e( 'Categories:' ); ?></h2> <form id="category-select" class="category-select" action="<?php echo esc_url( home_url( '/' ) ); ?>" method="get"> <?php $args = array( 'taxonomy' => 'download_category', // EDD custom post type Taxonomy 'order' => 'ASC', // Order of the list 'value_field' => 'slug', // <strong>Should be name?</strong> ); wp_dropdown_categories($args); ?> <input type="submit" name="submit" value="view" /> </form> </li></div>
Second way:
<?php //--This is the drop down START-- echo '<select name="categories">'; // Add custom option as default echo '<option>' . __('No Category', 'text-domain') . '</option>'; ?> // Get categories as array <?php $args = array( 'taxonomy' => 'download_category', // EDD custom post type Taxonomy 'order' => 'ASC' // Order of the list ); $categories = get_categories( $args ); ?> <?php foreach ( $categories as $category ) : // Check if current term ID is equal to term ID stored in database $selected = ( $stored_category_id == $category->term_id ) ? 'selected' : ''; echo '<option value="' . $category->term_id . '" ' . $selected . '>' . $category->name . '</option>'; endforeach; echo '</select>'; //--This is the drop down END-- ?> </div>
This is the code that displays the results – I can’t output the result of the dropdown as $cat
<div id="main-content" class="row store-template"> <div class="content clearfix"><div class="gallery-row2"> <?php $current_page = get_query_var('paged'); // Retrieving the data $per_page = get_option('posts_per_page'); $offset = $current_page > 0 ? $per_page * ($current_page -1) : 0; $cat = esc_attr( $_POST[ 'cat' ]); <strong>// Is this needed?</strong> $product_args = array( 'post_type' => 'download', 'posts_per_page' => $per_page, 'offset' => $offset, 'download_category' => $cat[cat_name], <strong>// Change this please - it should be the output from the dropdown);</strong> $products = new WP_Query($product_args); ?> <?php if ($products->have_posts()) : $i = 1; ?>
And into the loop…..
This should be so simple but it’s driving me mad!
Help please
- This topic was modified 6 years, 11 months ago by .
- This topic was modified 6 years, 11 months ago by .
- This topic was modified 6 years, 11 months ago by .
The page I need help with: [log in to see the link]
Viewing 7 replies - 1 through 7 (of 7 total)
Viewing 7 replies - 1 through 7 (of 7 total)
- The topic ‘get_categories(): how to get output’ is closed to new replies.