• Resolved correliatt

    (@correliatt)


    Hi,
    I’m trying to add a function to my functions.php where random categories from my cpt taxonomy ‘client’ should be listed using a shortcode. But I can’t get it to work.

    The code I’m using is:
    https://onlinephp.io/c/c3b04

    This doesn’t output anything. Any clues why?

    • This topic was modified 12 months ago by correliatt.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Hardik Sharma

    (@hardiksharma)

    Hi @correliatt,
    As i check the official docs of WordPress Read Here . It does not support random in orderby. To get normal list you can use below code

    function get_all_taxonomies(){
    
    		$terms     = get_terms(
    			[
    				'taxonomy'   => 'directors',
    				'hide_empty' => false,
    			]
    		);
    		if ( count( $terms ) ) {
    			foreach ( $terms as $term ) {
    				?>
    					<li>
    						<a href="<?php echo esc_url( get_term_link( $term ) ); ?>">
    							<?php echo esc_html( $term->name ); ?>
    						</a>
    					</li>		
    				<?php
    			}
    		}
    
    	
    }
    
    add_shortcode( 'show_term_list', 'get_all_taxonomies' );
    Moderator bcworkz

    (@bcworkz)

    If you want the terms in random order, you could apply shuffle() to the array of terms before they are output in a loop.

    Also, you must not generate output from a shortcode callback. Instead, all output needs to be collected into a single variable which is then returned to WP. WP will then echo out the returned string at the appropriate place. Failure to do this will cause your shortcode output to be in the wrong place on the page.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Random categories list’ is closed to new replies.