Display custom field value in sidebar
-
0 down vote favorite
share [g+] share [fb] share [tw]I’ve been struggling with this for a few hours and, being the php newbie that I am, and can’t seem to work it out, so here I am.
I’m trying to display the last 5 custom field values (thumbnails) associated with a custom post (post type ‘portfolio’) in a page sidebar, excluding the value for the custom field of the presently displayed custom post.
For now, all I’ve managed to do is : display one custom field value, that of the displayed custom post. And I haven’t been able to make it work from sidebar.php so far, so it’s directly injected in the single-portfolio.php
Here is what I have:
<?php if (have_posts()) : while (have_posts()) : the_post(); ?> <?php $image = get_post_meta($post->ID, 'tz_portfolio_thumb', true); if($image) : ?> <a href="<?php the_permalink() ?>" title="Project for <?php the_title(); ?>"><img src="<?php echo $image; ?>" alt="Project for<?php the_title(); ?>" /></a> <?php endif; ?> <?php endwhile; endif; ?>And here is what’s in my sidebar.php at the moment:
<?php if(!is_page() && get_post_type() != 'portfolio') { dynamic_sidebar('main'); } elseif(get_post_type() == 'portfolio') { dynamic_sidebar('portfolio'); } else { dynamic_sidebar('page'); } ?>If my explanations don’t make sense, you can take a look at the current state of things here. Any help or pointers will be greatly appreciated!
-
A bump with the next step I’ve been able to take so far. Problem is, it only displays the alt text instead of the actual thumbnails, and I don’t get why!
<!--BEGIN #sidebar .aside--> <div id="sidebar" class="aside"> <?php $my_query = new WP_Query('showposts=5&orderby=post_date&order=desc&post_type=portfolio'); if( $my_query->have_posts() ) { while ($my_query->have_posts()) : $my_query->the_post(); ?> <?php $thumbnail = get_posts( array( 'post_type' => 'portfolio', 'numberposts' => 5, 'key' => 'tz_portfolio_thumb' ) ); if($thumbnail) : ?> <a href="<?php the_permalink() ?>" title="Project for <?php the_title(); ?>"><img src="<?php echo $thumbnail; ?>" alt="Project for <?php the_title(); ?>" /></a> <?php endif; ?> <?php endwhile; } wp_reset_query(); ?> <!--END #sidebar .aside--> </div>Managed to exclude displaying the current post custom field value by changing the first part to this:
global $wp_query; $current_id = $wp_query->get_queried_object_id(); $my_query = new WP_Query( array( 'post_type'=>'portfolio', 'posts_per_page'=>'5', 'post__not_in' => array($current_id), )); if( $my_query->have_posts() ) { while ($my_query->have_posts()) : $my_query->the_post(); $thumbnail = get_posts( array('post_type' => 'portfolio','numberposts' => 5,'meta_key' => 'tz_portfolio_thumb', 'post__not_in' => array($current_id)) );Still not displaying the actual thumbnails though, just the alt text. Irritating!
Gah, I was being so stupid…
This finally worked, like so:
<?php global $wp_query; $current_id = $wp_query->get_queried_object_id(); $my_query = new WP_Query( array( 'post_type'=>'portfolio', 'posts_per_page'=>'5', 'post__not_in' => array($current_id), )); if( $my_query->have_posts() ) { while ($my_query->have_posts()) : $my_query->the_post(); $image = get_post_meta($post->ID, 'tz_portfolio_thumb', true); ?> <a href="<?php the_permalink() ?>" title="Project for <?php the_title(); ?>"> <img src="<?php echo $image; ?>" alt="Project for <?php the_title(); ?>" /> </a> <?php endwhile; } wp_reset_query(); ?>Now, how do I turn this into a condition for sidebar.php ?
The topic ‘Display custom field value in sidebar’ is closed to new replies.