1

I am trying to figure out how to add a custom field to display an image instead of a title on the stats_get_csv from Wordpress stats.

  <?php if ( function_exists('stats_get_csv') && $top_posts = stats_get_csv('postviews', 'days=2&limit=6') ) : ?>
    <ol>
<?php foreach ( $top_posts as $p ) : ?>
<li><a href="<?php echo $p['post_permalink']; ?>"><?php echo $p['post_title']; ?></a></li>
<?php endforeach; ?>
    </ol>
<?php endif; ?>

Update

<?php if ( function_exists('stats_get_csv') && $top_posts = stats_get_csv('postviews', 'days=2&limit=6') ) : ?>
    <?php if ( get_post_meta($post->ID, 'Image', true) ) : ?>
        <ol>
            <?php foreach ( $top_posts as $p ) : ?>
                <li>
                    <img class="thumb" src="<?php echo get_post_meta($post->ID, 'Image', true) ?>" alt="<?php the_title(); ?>" height='100' width='100' />
                </li>
            <?php endforeach; ?>
        </ol>
    <?php endif; ?>
<?php endif; ?>
  • what does stats_get_csv look like? – Brooke. Apr 7 '11 at 5:53
  • What image do you want to display? There is another question about displaying the post thumbnail. – Jan Fabry Apr 7 '11 at 9:31
  • I saw the post thumbnail question but I want to use it with a custom field instead of a post thumbnail. Every post on the site has a custom field of 'Image' and I would like to use that. – Spewf Apr 7 '11 at 10:50
1

Return of that function should have post ID, right? Then it's straight get_post_meta() using that ID and name of your field.

Update

In your second code snippet $post is global variable, $post->ID is not tied in any way with return of stats_get_csv() function. You need something like from first example ($p['post_permalink']), just figure out if there is a field with ID.

Also you don't need wrapping if ( get_post_meta($post->ID, 'Image', true) ) that would make sense only for single current post, not for loop of posts.

  • That doesn't work. I tried it via that page and it only shows the same image repeating 6 times. I have also tried codex.wordpress.org/Function_Reference/get_post_custom_values – Spewf Apr 7 '11 at 19:19
  • @Spewf could you please add your code to the question? I am not sure I would be able to help since I don't use WP stats, but maybe someone else will be. – Rarst Apr 7 '11 at 19:41
  • Do you not see the code at the top? I added it correctly. You can also see the code at wordpress.org/extend/plugins/stats/faq – Spewf Apr 7 '11 at 22:53
  • @Spewf that code has nothing to do with my suggestion to try and access post meta via ID that should be (I think) returned with rest of stuff and what might fail when you try that – Rarst Apr 7 '11 at 23:01
  • @Rarst. Sorry I didn't understand what you meant the first time. – Spewf Apr 8 '11 at 0:20

Your Answer

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

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