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 - 16 through 30 (of 30 total)
  • Author
    Posts
  • #2552690
    David
    Staff
    Customer Support

    Let us know!

    #2561333
    nudnavda

    Hi David,
    sorry for the delay.
    Had to arrange a few things first…

    I found a way to make local testing environment (via Wamp).
    And tested the code you provided for the button with linked titles to pages.
    Alas, the shortcode does not work, one way or another.

    I made a few test-pages (category: test) so you can see what’s going on:
    https://dharmoebe.nl/test-overview/
    Please let me know if I can do anything to assist you further.

    Thanks!

    #2561365
    David
    Staff
    Customer Support

    So that category has a slug of test so the shortcode should be:

    [category_posts category_slug="test"]

    Does that work?

    #2561442
    nudnavda

    That’s the slug i used.
    But does not work.

    #2561750
    David
    Staff
    Customer Support

    Does it work if you add it to the page content ?

    #2561778
    nudnavda

    No, if you click test-1 you’ll see the result:
    https://dharmoebe.nl/test-1/

    #2561811
    David
    Staff
    Customer Support

    Hmm… i tested it on 3 dev sites and all 3 worked without any issues.

    Do any shortcodes work ? Try this snippet:

    add_shortcode( 'hello', function() {
        ob_start();
        // Start your PHP below
      
        echo 'hello';
      
        // End your PHP above
        return ob_get_clean();
    } );

    the shortcode is [hello] and it simply prints out hello

    #2562601
    nudnavda

    Yes – your “hello” shortcode work.
    See test-1 link.

    #2563810
    nudnavda

    Maybe some relevant info, David: I use the plugins ‘Post Type Switcher’ (to transform older single posts in pages where needed) and ‘Add Categories to Pages’ (to organize pages according to categories).

    #2563985
    David
    Staff
    Customer Support

    Hmmm…. it could be either of those plugins that are messing with the way terms are output.
    Unfortunately i can’t replicate the issue locally, i tested the code on 3 dev sites and each of them it works as expected.

    You may need to look for a plugin.

    #2564640
    nudnavda

    Yes, I understand.
    Appreciate the support you’ve given.

    #2564698
    nudnavda

    I have one question that might help me: how have you “tested the code on 3 dev sites”?
    You probably did not use any plugin to add a category to the pages?

    #2564870
    David
    Staff
    Customer Support

    One last attempt.
    Try this:

    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' => 'page',
            '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' );
    #2565614
    nudnavda

    I’m sorry this is creating a bit of a hassle now, David.
    The latest code didn’t change the output.
    So maybe working with tags instead of categories will offer a solution.
    We’ll see.

    Thanks a lot, anyway! 🍺

    #2565827
    David
    Staff
    Customer Support

    Sorry i couldn’t be of much more help 🙂

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