• Resolved OsakaWebbie

    (@osakawebbie)


    It has come to my attention that a little plugin I’ve been using called “Display Terms Shortcode” has a security vulnerability and hasn’t been updated in years. I’d be happy to stop using it, but it does one important job for me, so I’ve come here to ask how to do the same thing without the plugin.

    One custom taxonomy on the site is called “Issue”, denoting the publication issues of a quarterly magazine. Here’s an Imgur post showing the widget definition using the shortcode, the styled result on the frontend, and the custom taxonomy (which is nothing special – it works just like tags): https://imgur.com/a/recent-issues-widget-V4ldtGA

    I know that lots of functionality has been added to WordPress since the time I set this up (especially all kinds of ways of using blocks – I’m way behind on understanding how blocks work outside of the main content of posts), so I suspect there is now a way to do this without a plugin. I hope you guys can point me in the right direction. Note that the list of terms is sorted by slug descending, and the description (which is the theme of each issue) is also displayed – those things are important. TIA!

    The page I need help with: [log in to see the link]

Viewing 9 replies - 1 through 9 (of 9 total)
  • Hey @osakawebbie,
    Please check this url https://wordpress.com/support/display-posts-shortcode/
    Also check url https://wordpress.org/plugins/wp-anywhere-widgets/
    It’s may helps you.

    Best Regards
    Ravindra Singh Meyda

    • This reply was modified 4 months, 4 weeks ago by ravindra107.
    Thread Starter OsakaWebbie

    (@osakawebbie)

    Neither of those would help me. The first one only displays posts, not taxonomy terms (which come from a completely different database table). The second one is for putting a widget somewhere other than a widget area, but a widget area is exactly where I do want my code. The problem is that there isn’t a widget to do what I need. Anyone else have ideas?

    Hey @osakawebbie,
    Check below code

    $test_cat = !empty( $_POST['test-cat'] ) ? $_POST['test-cat'] : '';
    $categories = get_categories( array( 'taxonomy' => 'category', 'parent' => 0 ) );
    if( !empty( $categories ) ) :
    $output .= '<select id="test-cat" name="test-cat">';
    $output .= '<option value="0">' . __( 'Select Category' ) . '</option>';
    foreach( $categories as $category ) :
    $output .= '<option value="' . esc_attr( $category->term_id ) . '" ' . selected( $category->term_id, $test_cat, false ) . '>' . esc_html( $category->name ) . ' (' . esc_attr( $category->count ) . ')</option>';
    endforeach;
    $output .= '</select>';
    endif;

    Best Regards
    Ravindra Singh Meyda

    • This reply was modified 4 months, 4 weeks ago by ravindra107.
    Thread Starter OsakaWebbie

    (@osakawebbie)

    Thanks, but you don’t seem to understand what I need. I do not need a select field for a form; I need a list of the names, descriptions, and slugs of a limited number of terms. Yes, I can change the HTML in your example from select to ul, but your code doesn’t get the descriptions or slugs, doesn’t sort, and doesn’t limit the number. And you used get_category(), but my custom taxonomy is more like a tag than a category (which I said in my explanation), and looking through the developer docs (I was hoping I wouldn’t have to get that deep – I’m a user, not a plugin developer), it seems that get_terms() is more applicable even than get_tags() – is that right? Finally, you wrote PHP code with no context, but if I must write my own PHP, it would need a place to be – if it can be in a hook in functions.php, I don’t know what hook that would be. If it needs to be in a plugin construct, I don’t know how to make that into a shortcode I can put in a widget.

    I was hoping there might be a native block that could do this. If not, I thought there was probably a relevant plugin that had not been abandoned. If neither exist and therefore I need to write PHP, I’ll need more help with context.

    Hey @osakawebbie,
    Please check below shortcode you can put or pass taxonomy in shortcode.
    [custom-terms-list taxonomy=’catgeory’ orderby=’name’ order=’ASC’ number=5]
    Put below code in your theme functions.php use above shortcode on posts or pages.

    function display_custom_taxonomy_terms( $atts ) {
    // Extract shortcode attributes
    $atts = shortcode_atts(
    array(
    'taxonomy' => 'your_custom_taxonomy',
    'orderby' => 'name',
    'order' => 'ASC',
    'number' => 5,
    ),
    $atts
    );

    // Fetch terms
    $terms = get_terms( array(
    'taxonomy' => $atts['taxonomy'],
    'orderby' => $atts['orderby'],
    'order' => $atts['order'],
    'number' => $atts['number'],
    'hide_empty' => true, // Hide terms with no associated posts
    ) );

    // Check if terms are available
    if ( is_wp_error( $terms ) || empty( $terms ) ) {
    return '<p>No terms found.</p>';
    }

    // Generate the output
    $output = '<ul class="custom-taxonomy-terms">';
    foreach ( $terms as $term ) {
    $output .= '<li>';
    $output .= '<strong>' . esc_html( $term->name ) . '</strong>'; // Term name
    $output .= '<br><small>' . esc_html( $term->description ) . '</small>'; // Term description
    $output .= '<br><em>' . esc_html( $term->slug ) . '</em>'; // Term slug
    $output .= '</li>';
    }
    $output .= '</ul>';

    return $output;
    }
    add_shortcode( 'custom-terms-list', 'display_custom_taxonomy_terms' );

    Best Regards
    Ravindra Singh Meyda

    Thread Starter OsakaWebbie

    (@osakawebbie)

    Your code doesn’t make sense. You say the shortcode would be called “custom-terms-list”, but that name doesn’t appear anywhere in the code. And there is no loop to fetch anything from the database – it just declares variables.

    Hey @osakawebbie,
    Could you please specify the parameters included in the shortcode? Before sending this shortcode, I tested it, and it works fine.
    [custom-terms-list taxonomy=”category”]
    It works on the existing database and does not fetch data from a remote or other database.

    Best Regards
    Ravindra Singh Meyda

    • This reply was modified 4 months, 3 weeks ago by ravindra107.
    Thread Starter OsakaWebbie

    (@osakawebbie)

    Oh, I’m so sorry! I somehow didn’t notice that there was a lot more code hidden below the visible area in your comment – I thought what I could see what all there was. The rest of the code makes much more sense. I adjusted the output HTML to match what I had already styled with CSS (including a link rather than just outputting the slug), and it works great. Thank you!

    Hey @osakawebbie,
    No problem at all! I’m glad you were able to find the rest of the code and adjust it to fit your needs. It’s great to hear that everything is working as expected with your CSS styling and the link addition. If you have any more questions or need further assistance, feel free to ask. Happy coding! 😊

    Best Regards
    Ravindra Singh Meyda

    • This reply was modified 4 months, 3 weeks ago by ravindra107.
Viewing 9 replies - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.