• Hello,
    is there a way to show all taxonomies I have and then mark the ones that are selected in the Post?

    Like:

    My Sports:
    Football – YES
    Tennis – NO
    Icehockey – NO
    Basketball – YES
    Sailing – YES

    Right now I list all the YES taxonomies with:

    function check_rock_details() {
    	global $post;
    	$rock_details = wp_get_object_terms( $post->ID,  'rock_details' );
    if ( ! empty( $rock_details ) ) {
    	if ( ! is_wp_error( $rock_detailss ) ) {
    		echo '<table border="1"><tr><td colspan="2">Details</td></tr>';
    			foreach( $rock_details as $rock_detail ) {
    				echo '<tr><td>' . esc_html( $rock_detail->name ) . '</td><td>YES</td></tr>'; 
    			}
    		echo '</table>';
    	}
    }
    }
    add_shortcode( 'rock_details', 'check_rock_details' );

    How do I create a List ith both, YES and NO?

    Thanks for some suggestions,

    Denis

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    Use get_terms() or the WP_Term_Query class to get all terms in a taxonomy. Check the terms in the returned list against the terms returned by wp_get_object_terms() Mark any matches as YES, the rest would be NO.

    Repeat for every taxonomy you want to do this for. You can get all registered taxonomies with get_taxonomies().

    Thread Starter DenisCGN

    (@deniscgn)

    @bcworkz

    Thanks a lot, now I saw that I can use ARGS in 3rd wp_get_object_terms()
    If I can use the ARGS that only returns an ARRAY if the taxonomy IDs.

    So ich I have the ARRY of the taxonomies that are used in the post.

    I could easy use IN_ARRAY() and change if YES the color to green.

    This seems to be easier, IF I know how to use the 3rd argument ARGS.

    Cheers,
    Denis

    Moderator bcworkz

    (@bcworkz)

    Yes you can use $args to have wp_get_object_terms() return an array of taxonomy term IDs.
    $term_ids = wp_get_object_terms( $post->ID, 'rock_details', ['fields'=>'ids',]);

    Then use something like in_array( $current_id, $term_ids ) to decide what color to set the current term.

    Thread Starter DenisCGN

    (@deniscgn)

    Perfect, this is my solution, for 10 Terms its fine ??

    function check_rock_details_1() {
    	global $post;
    	$rock_details_1 = wp_get_object_terms( $post->ID,  'rock_details', ['fields'=>'ids'] );
    if ( ! empty( $rock_details_1 ) ) {
    	if ( ! is_wp_error( $rock_details_1 ) ) {
    		echo '<table border="1"><tr><td colspan="2">Details</td></tr>';
    		echo '<tr ' . ( in_array( '457', $rock_details_1 ) ? 'bgcolor="green"' : 'bgcolor="red"' ) . '><td>Biergarten / Terrasse (457):</td><td>' . ( in_array( '457', $rock_details_1 ) ? 'JA' : 'NEIN' ) . '</td></tr>';
    		echo '<tr ' . ( in_array( '455', $rock_details_1 ) ? 'bgcolor="green"' : 'bgcolor="red"' ) . '><td>Billard (455):</td><td>' . ( in_array( '455', $rock_details_1 ) ? 'JA' : 'NEIN' ) . '</td></tr>';
    		echo '<tr ' . ( in_array( '456', $rock_details_1 ) ? 'bgcolor="green"' : 'bgcolor="red"' ) . '><td>Dart / (e)Dart (456):</td><td>' . ( in_array( '456', $rock_details_1 ) ? 'JA' : 'NEIN' ) . '</td></tr>';
    		echo '</table>';
    	}
    }
    }
    add_shortcode( 'rock_details_1', 'check_rock_details_1' );

    Is global $post; nessesary? When I delete it it does not work.

    For me it is working fine, maybe if there are some parts to do easier, please let me know…I am happy for any suggestion.

    Thanks so much,
    Denis

    • This reply was modified 4 years, 9 months ago by DenisCGN.
    • This reply was modified 4 years, 9 months ago by DenisCGN. Reason: global post question
    Moderator bcworkz

    (@bcworkz)

    Do not echo out shortcode output. It’s unpredictable where it ends up on the page. To ensure the output is where the shortcode was placed, collect all output into a single variable, then return that variable. Roughly like this:

    $output = '<div>Arbitrary HTML.';
    $output .= ' More content for';
    $output .= ' shortcode output.</div>';
    return $output;

    Or buffer the echoed output, then return the buffer content.

    You need global $post; so that $post->ID has a proper value. An alternate is to use get_the_ID(), which if you check its source code uses global $post; itself.

    If the terms you’re checking are limited to 3 or so, what you have other than the echoing aspect is fine. Many more and you should devise some sort of loop to deal with the individual terms to list (IDs 455, 456, 457). For example, place these all in their own array and do a foreach loop on that array to generate output.

    Any time you find yourself repeating almost the exact same code but for one small aspect, you should ask yourself if there’s a better way ?? Otherwise, well done!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Show all Terms with wp_get_object_terms’ is closed to new replies.