Site logo

Archived topic

A button with selection function

29 replies · Started by nudnavda on February 24, 2023

Viewing posts 16–30 of 30

Let us know!

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!

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

[category_posts category_slug="test"]

Does that work?

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

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

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

Yes - your "hello" shortcode work.
See test-1 link.

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

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.

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

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?

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' );

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! 🍺

Sorry i couldn't be of much more help :)

This archived topic is closed to new replies.