0

I have created Custom post type 'references' and two taxonomies under this

e.g

"Custom Taxonomy A" have Categories "Cat-a1" , "Cat-a2"....

"Custom Taxonomy B" have Categories "Cat-b1" , "Cat-b2"....

Each post or reference have assigned one category from Taxonomy A and one from Taxonomy B. I have a template for displaying posts from above Custom post type with respect to Custom Taxonomy A in a loop e.g

Title "Cat-a1"

ALL posts assigned to this category

Title "Cat-a2"

ALL posts assigned to this category

What i am trying is assign a related Category name from Custom Taxonomy B as id of each title

e.g

<div id="Catb1" ><h1>"Cata1"</h1>
ALL posts assigned to this category </div>

<div id="Catb2" ><h1>"Cata2"</h1>
ALL posts assigned to this category </div>

<div id="Catb3" ><h1>"Cata3"</h1>
ALL posts assigned to this category </div>

Here is my Template

<?php
    $args=array(
    'post_type'                => 'references',
    'child_of'                 => 0,
    'parent'                   => '',
    'orderby'                  => 'name',
    'order'                    => 'ASC',
    'hide_empty'               => 1,
    'hierarchical'             => 1,
    'exclude'                  => '',
    'include'                  => '',
    'number'                   => '',
    'taxonomy'                 => 'references-company',
    'pad_counts'               => false
    );

    $categories=get_categories($args);

    foreach ( $categories as $category ) {

        if ( $category->parent > 0 ) {
            continue;
        }

        $querystr = "SELECT $wpdb->posts.*
            FROM $wpdb->posts, $wpdb->term_relationships, $wpdb->terms
            WHERE term_id = (" . $category->cat_ID . ")
            AND term_taxonomy_id = (" . $category->term_taxonomy_id . ")
            AND ID = object_id
            AND post_type = 'references'
            AND post_status = 'publish'
            ORDER BY post_date ASC";
        $posts = $wpdb->get_results($querystr, OBJECT);

        echo '<div class="content_item" id="';
                $term_list = wp_get_post_terms($post->ID, 'references-industry', array("fields" => "names"));
                echo $term_list[0];
                echo '"><div class="border-r"><div class="quotesimg"><h1 class="termtitle headingopen" style="cursor: pointer;">' . $category->name . '(' . $category->category_count . ')</h1></div></div>';

        echo '<div class="references-inner textContent expander">';
        foreach ( $posts as $post ) {
        setup_postdata($post);
            echo '<div class="floatleft twocol-one">';
                the_content();
                the_excerpt();
                echo '<div class="social-linkedin">';
                    $values = get_post_custom_values("linkedin");
                    if ( is_array($values) ) {
                        echo '<a target="_blank" href="';
                        echo the_field( "linkedin" );
                        echo '"><img src="';
                        echo get_template_directory_uri();
                        echo '/images/ico_social_4.jpg" /></a>';
                    } else {
                        echo '&nbsp;';
                    }
                echo '</div>';
            echo '</div>';
            echo '<div class="thumbnail-r fourcol-one last">';
                the_post_thumbnail('full');
            echo '</div>';
        }
        echo '</div>';
        echo '<div class="moreopen"><p class="more">Mere</p></div>';
        echo '</div>';
    }
?>

how i can get term/cat names from custom taxonomy B specific to post to use them as ID in div. I have tried in above template but its seems not getting them correctly.

I used this for getting term name assigned to post from Taxonomy B

echo '<div class="content_item" id="'; $term_list = wp_get_post_terms($post->ID,     'references-industry', array("fields" => "names")); echo $term_list[0]; echo '">

Thanks

1 Answer 1

1

You kind of not telling what the problem is, like debug, error message you get. There is most likely something wrong with how you wrote that, I'm not willing to go trough this right now. But generally you should make this cleaner and separate parts for an better structure, regarding the taxonomy name as id for the div see below simplified example on how I'd do this:

$term_obj = wp_get_post_terms(get_the_ID(), 'example-taxonomy', array("fields" => "names"));
echo '<div class"example" id="'.$term_obj[0].'">EXAMPLE</div>';
1
  • Sorry for not clearly describing a question but you are in right direction and i have tried this already if you can see my code you can see it echo '<div class="content_item" id="'; $term_list = wp_get_post_terms($post->ID, 'references-industry', array("fields" => "names")); echo $term_list[0]; echo '"> But this is not working and not getting term name assigned to post. Thanks Dec 3, 2013 at 17:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.