Site logo

[Support request] A button with selection function

Home Forums Support [Support request] A button with selection function

Home Forums Support A button with selection function

Viewing 15 posts - 1 through 15 (of 30 total)
  • Author
    Posts
  • #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!

    #2545292
    David
    Staff
    Customer Support

    Hi 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 dynamic

    #2545315
    nudnavda

    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.

    #2545335
    nudnavda

    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).

    #2545379
    David
    Staff
    Customer Support

    That 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 ?

    #2545418
    nudnavda

    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/

    #2545476
    David
    Staff
    Customer Support

    Try 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"]

    #2545494
    nudnavda

    Great!
    I’ll let you know… 😉

    #2545798
    David
    Staff
    Customer Support

    Please do 🙂

    #2548249
    nudnavda

    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.

    #2548592
    David
    Staff
    Customer Support

    Are the posts a normal Post ? Or are they custom posts ?

    #2548752
    nudnavda

    Normal posts (pages).

    #2548795
    nudnavda

    I’m sorry, I’ve given you the wrong slug-name for the category.
    Must be [praktijkleraren] and not [praktijk-leraren].

    #2549688
    David
    Staff
    Customer Support

    I 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: praktijkleraren then your shortcode would be:

    [category_posts category_slug="praktijkleraren"]

    #2552534
    nudnavda

    Hi David, somehow I missed the notification of your last reaction.
    Will check it out tomorrow!

Viewing 15 posts - 1 through 15 (of 30 total)
  • You must be logged in to reply to this topic.