1

For a website I am developing I have come across the following challenge.

I have made a custom taxonomy "blog" (associated with both the Post-post_type and the User-object_type). With the taxonomy meta plugin I have added a meta field image. On the taxonomy archive page I output the taxonomy term name, description and this image.

On the front_page of this site I want to list all Blogs and I would like to do that in such a way that it again shows that taxonomy meta image.

I already have managed to show the blogs with title (link) and description, using the last example on the Codex entry:

/* List all blogs, source example 2: http://codex.wordpress.org/Function_Reference/get_terms#Examples */
$args = array( 'taxonomy' => 'blog', 'orderby' => 'name', 'order' => 'ASC', 'fields' => 'all' );

$terms = get_terms('blog', $args);

$count = count($terms); $i=0;
if ($count > 0) {
    foreach ($terms as $term) {

        $i++;

        $term_list .= '<article class="blog-list"><p><a href="' . get_home_url() . '/blog/' . $term->slug . '/" title="' . sprintf(__('View all articles in %s Blog', 'wys'), $term->name) . '">' . $term->name . '</a></p><p>' . $term->description . '</p>';
        if ($count != $i) $term_list .= '</article>'; else $term_list .= '';
    }
    echo '<div class="my_term-archive">' . $term_list . '</div>';
}

The code I used to show the image on the blog taxonomy archive comes straight from Rilwis' website (all the way at the bottom):

$queried_object = get_queried_object();
$term_id = $queried_object->term_id;

$meta = get_option('blog_image'); //meta_id
    if (empty($meta)) $meta = array();
    if (!is_array($meta)) $meta = (array) $meta;
        $meta = isset($meta[$term_id]) ? $meta[$term_id] : array(); //term_id
        $images = $meta['blogimg']; //field_id
            foreach ($images as $att) {
                // get image's source based on size, can be 'thumbnail', 'medium', 'large', 'full' or registed post thumbnails sizes
                $src = wp_get_attachment_image_src( $att, 'thumbnail' );
                $src = $src[0];
                // show image
                echo '<figure class="thumb alignleft"><img src="' . $src . '" alt="image" /></figure>';
            }

echo '<div class="archive-meta">';

    $description = term_description();
    if ( $description ) { // Show Blog taxonomy term description
        echo $description;
    } //endif

    $meta = get_option('blog_image'); //meta_id
        if (empty($meta)) $meta = array();
        if (!is_array($meta)) $meta = (array) $meta;
            $meta = isset($meta[$term_id]) ? $meta[$term_id] : array(); //term_id
            $value = $meta['blog_authors']; //field_id
            echo '<p class="blog-authors">' . __( 'Author(s): ', 'wys' ) . $value . '</p>'; // if you want to show                  

echo '</div>';

The difficulty to do this on the homepage too is that I seem to have to combine two foreach calls and that is a little bit too complex territory for me.

For whomever want to have a visualization, I have made available on my staging server the homepage and one of the blog archives.

Any tips and/or nudges in the right direction are appreciated!

Thanks

1

Over at Google+ I posted the same question and credits go to Alexander Conroy for coding up the correct solution:

/* List all blogs, source example 2: http://codex.wordpress.org/Function_Reference/get_terms#Examples
 *
 * credits Alexander Conroy - https://plus.google.com/u/0/113053085239726972447
*/
$terms = get_terms('blog', $args);
$blog_image_meta = get_option('blog_image');

if (!empty($terms)) {
    foreach ($terms as $term) {
        $meta = isset($blog_image_meta[$term->term_id]) ? $blog_image_meta[$term->term_id] : array(); //term_id
        $image = array_shift(wp_get_attachment_image_src( array_shift($meta['blogimg']), 'thumbnail' ));

        $term_list .= '<article class="blog-list"><figure class="thumb alignleft"><img src="' . $image . '" alt="image" /></figure><p><a href="' . get_home_url() . '/blog/' . $term->slug . '/" title="' . sprintf(__('View all articles in %s Blog', 'wys'), $term->name) . '">' . $term->name . '</a></p><p>' . $term->description . '</p></article>';
        }
    echo '<div class="my_term-archive">' . $term_list . '</div>';
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy