Forums

Using Custom Fields in Sidebar Featured Post (2 posts)

  1. heffernan.courtney
    Member
    Posted 1 year ago #

    I'm calling this portion of my template into my sidebar, to display some featured posts. It is not displaying any of the post meta data. Can I use custom fields in my sidebar? I tried putting the same code into single.php and everything worked fine...

  2. heffernan.courtney
    Member
    Posted 1 year ago #

    The code:

    <?php
    
    //The Query
    query_posts('category_name=featured_book');
    
    //The Loop
    if ( have_posts() ) : while ( have_posts() ) : the_post(); 
    
    $book="book_image";
    $author="author_name";
    $buythis="buy_this";
    
    $bookimage= get_post_meta($post->ID, $book, true);
    $writebookimage='<img src="' . $bookimage . '"/>';
    
    $author= get_post_meta($post->ID, $author, true);
    $writeauthor = '<h3>'.$author.'</h3>';
    $buylink= get_post_meta($post->ID, $buythis, true);
    $writebuylink='<a href="' . $buy_this . '"/>';
    
    ?>
    
    <a href="<?php echo $buylink; ?>" rel="bookmark" title="Buy the book <?php the_title_attribute(); ?>">
    <?php the_title(); ?>
    </a>
    <h2> <?php echo $writeauthor; ?></h2>
    <?php if ( get_post_meta($post->ID, 'book_image', true) ) : ?>
            <img class="thumb" src="<?php echo get_post_meta($post->ID, 'book_image', true) ?>" alt="<?php the_title(); ?>" />
    
    <?php endif; ?>
    <?php endwhile; else: ?>
    <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
    <?php endif; ?>
    
    <?php
    //Reset Query
    wp_reset_query();
    
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic