WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] taxonomies (6 posts)

  1. another-webmaster
    Member
    Posted 4 months ago #

    Trying to make a function which put taxonomy and terms on posting.

    It should not have a hyperlink, looking in the codex made it first easy..copy/paste/edit and done but how to do it with multiple taxonomies and terms? Seldom is shown (in codex) how to do for multiple issues/options so I am stuck. Any help would be nice.

    function my_function($content) {
    
    if (is_single()) {
    
    global $post, $site, $article;
    
    $terms = get_the_terms($post->ID, 'site');
    $terms = get_the_terms($post->ID, 'article') ;
    
    if ( $terms && ! is_wp_error( $terms ) ) :
       $site = array();
       $article = array();  
    
       foreach ( $terms as $term ) {
    	$site_links[] = $term->name ;
    	$article_links[] = $term->name ;
       }
    
       $site = join( ", ", $site_links );
       $article = join( ", ", $article_links ); 
    
       ?>
       <p>
       Site: <strong><?php echo $site; ?></strong><br />
       Article: <strong><?php echo $article; ?></strong>
       </p>
       <?php
       endif;
       return $content;
       } else {
       return $content;
       }
    }
    
    add_filter('the_content', 'my_function');

    It shows only outcome for Article (on both), it skips Site, what am I missing or doing wrong?

  2. Steven Jones
    Member
    Posted 4 months ago #

    I think you maybe should be using wp_get_post_terms() for this.

    http://codex.wordpress.org/Function_Reference/wp_get_post_terms

    Also, you're overwriting your site information with the article information as you're using $terms in both circumstances therefore it's overwriting it.

  3. another-webmaster
    Member
    Posted 4 months ago #

    So how to prevent (because that is the knowledge I am missing!!!) that overwriting. Please could you enlighten me?

    I used partly the code from codex A Basic Example.

    Edit: using wp_get_post_terms did not change any, result is the same.

  4. bcworkz
    Member
    Posted 4 months ago #

    You can't use $terms to contain term data from both taxonomies at the same time. You need to assign a different variable to contain the terms from one of the taxonomies, then run a separate loop to build a term name array from each set of results from wp_get_post_terms() (or get_the_terms(), either will work).

    The reason for using wp_get_post_terms() is it is specifically tailored for situations like yours. get_the_terms() is a more generic function. The decision to use one over the other is more style than necessity. Using more targeted functions makes your code easier to follow a year later when you decide to enhance your code (and is easier for us) and is less prone to mistakes and ambiguity.

  5. another-webmaster
    Member
    Posted 4 months ago #

    $site_list = wp_get_post_terms ($post->ID, 'site', array( "fields" => "names") );
    print_r($site_list[0]);

    Nice output on the post (used echo's etc. so it would look good on the post) but again, I am stuck because of the same reason, how to do it with multiple taxonomies/terms?
    Yes maybe I forgot to mention but I am not a coder/programmer just a webdesigner/user.

    So please could you add a sample for at least 2 taxonomies and terms, because reading the codex is not helping me for that, they have no samples for multiple taxonomies/term use.(yes they say it is possible, but they don't tell how to do :( )

    Or do I overlook some now?

  6. another-webmaster
    Member
    Posted 4 months ago #

    ok found it out myself.

Reply

You must log in to post.

About this Topic