- This topic has 29 replies, 2 voices, and was last updated 3 years, 3 months ago by
David.
-
AuthorPosts
-
February 24, 2023 at 3:12 am #2545217
nudnavda
Hello helpdesk,
it seems deceivingly simple, but I don’t find a way to create it: a button with text “select” that produces (when clicked) a rolldown menu of single pages, filtered or queried from a specified category.
So this would not use a standard menu (manually produced) or a general pagelist widget.
It would be automatically generated from the database, and used as an extra option on specific category archives.Until now I haven’t found a method in GP or GB, nor a plugin that does this.
Maybe it’s due to my limited knowledge of WordPress.Would you know if this is possible, and if so, how?
Thanks very much!February 24, 2023 at 4:12 am #2545292David
StaffCustomer SupportHi there,
yeah, thats not WP does by default and we don’t have anything in the Theme or GB.
The closet thing in WP is the Category List block, which does has a Display as Dropdown option in its settings.Where would this select box be displayed?
And what would the list contain ? I assume it would be dynamicFebruary 24, 2023 at 4:33 am #2545315nudnavda
It would be displayed on an archive page of specific category.
It would contain all url’s from the category.
And yes, it would keep its contents automatically updated.February 24, 2023 at 4:48 am #2545335nudnavda
For instance:
https://dharmoebe.nl/visie/yulu/The button under the header is an element that uses a manually edited menu.
I’m looking for something similar, but with automated contents (url’s).
February 24, 2023 at 5:28 am #2545379David
StaffCustomer SupportThat URL is for a static page which is display a fixed parameter query loop.
Are you manually creating each of those category pages?If so would a shortcode with a category argument, where you set the terms to list, work ?
February 24, 2023 at 5:58 am #2545418nudnavda
Thanks so much for thinking along!
I am not a coder and creating shortcodes is not instantly clear for me.Am I in the right direction with investigating the information on the following page:
https://diveinwp.com/shortcode-display-post-from-categories/February 24, 2023 at 6:48 am #2545476David
StaffCustomer SupportTry this PHP Snippet:
function display_post_links_select_box( $atts ) { $output = ''; // Set up the shortcode attributes $atts = shortcode_atts( array( 'category_slug' => '', ), $atts ); // Get the current category ID, or the ID of the specified category slug if ( $atts['category_slug'] ) { $category = get_category_by_slug( $atts['category_slug'] ); $category_id = $category->term_id; } else { $category = get_queried_object(); $category_id = $category->term_id; } // Set up the query arguments to get posts in the current or specified category $args = array( 'posts_per_page' => -1, 'post_type' => 'post', 'post_status' => 'publish', 'orderby' => 'title', 'order' => 'ASC', 'category__in' => array( $category_id ), ); $query = new WP_Query( $args ); if ( $query->have_posts() ) { $output .= '<select name="post_links" onchange="location = this.value;">'; while ( $query->have_posts() ) { $query->the_post(); $output .= '<option value="' . get_permalink() . '">' . get_the_title() . '</option>'; } $output .= '</select>'; wp_reset_postdata(); } return $output; } add_shortcode( 'category_posts', 'display_post_links_select_box' );Then on your page add the shortcode:
[category_posts category_slug="your-category-slug"]February 24, 2023 at 7:13 am #2545494nudnavda
Great!
I’ll let you know… 😉February 24, 2023 at 10:00 am #2545798David
StaffCustomer SupportPlease do 🙂
February 27, 2023 at 1:52 am #2548249nudnavda
I put the PHP (without any alterations) in Snippets, David.
And then the shortcode [category_posts slug=”praktijk-leraren”] on the page which is:https://dharmoebe.nl/praktijk/leraren-overzicht/
Alas, there’s no result – not via inserting shortcode, nor via element (hook, after-entry) plus shortcode.
I am not supposed to change any of your php-code, am I?
Just the slug in the shortcode?What I want to achieve is placing underneath the title ‘Leraren overzicht’ a sort of TOC-button with index of the page-titles within the category.
Must confess, I’m a bit troubled by the fact that this competes with the archive-page itself which after all is kind of an index of the category-pages.But it would be nice for visitors to have a compact overview and be able to select a preferred teacher.
Also, I might change the lay-out of the template (more text, featured image).
And this button would be applied to several of these kind of archives on this site.I do hope my explanations are of any help.
February 27, 2023 at 7:31 am #2548592David
StaffCustomer SupportAre the posts a normal Post ? Or are they custom posts ?
February 27, 2023 at 7:56 am #2548752nudnavda
Normal posts (pages).
February 27, 2023 at 8:15 am #2548795nudnavda
I’m sorry, I’ve given you the wrong slug-name for the category.
Must be [praktijkleraren] and not [praktijk-leraren].February 28, 2023 at 3:26 am #2549688David
StaffCustomer SupportI edited my code above.
Can you use that instead.And the Shortcode you must enter looks like:
[category_posts category_slug="your-category-slug"]So if your category term is:
praktijklerarenthen your shortcode would be:[category_posts category_slug="praktijkleraren"]March 2, 2023 at 4:35 am #2552534nudnavda
Hi David, somehow I missed the notification of your last reaction.
Will check it out tomorrow! -
AuthorPosts
- You must be logged in to reply to this topic.