Support » Themes and Templates » Recent posts list in a custom field?

  • Has anyone managed to put a recent posts or recent comments list in a custom field? It’s not as simple as putting the recent post template tag in so so some PHP magic is obviously needed. Any ideas?

Viewing 7 replies - 1 through 7 (of 7 total)
  • How about using the the Custom Field Field parameters of the template tag, query_posts()?

    Stepping Into Template Tags
    Stepping Into Templates
    Template Hierarchy

    I’m obviously doing something wrong here.

    In my template I have:

    <div class="bottomBox">
    	<?php echo get_post_meta($post->ID, 'leftBox', false); ?>

    and in the actual custom field I have “leftBox” as the key and the following content which works if I put it in the actual template:

    <?php query_posts('showposts=5'); ?>
      <?php while (have_posts()) : the_post(); ?>
      <li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
      <?php endwhile;?>

    obviously the issue is that the php isn’t parsed when it is put in the custom field. so, how do I make it parse properly?

    Do just need to use the postbypost parameter of the template tag, [[Template_Tags/wp_get_archives|wp_get_archives()].

    If not please redescribe or give a better example cause I’m not getting what you want.


    I want the lastest 5 posts and also want to display the custom fields for thost posts.


    I want the 5 posts that have a custom field of a specific key and value.

    Yes, we are indeed talking past each other. Here is what I’m trying to do:

    I have a custom page template that has three boxes on the bottom that are populated using custom fields. They are called leftBox, centerBox and rightBox. I want to be able to populate them with whatever I want depending on the page in question, so for some pages just text strings, for some images etc.

    The problem I’m stuck on is that I want to be able to insert some code in one of one of the custom fields so that it spits out a list of the 5 last posts.

    The whole idea is that the contents of each box is defined from within the page and not in the template so that it changes depending on the page. Did that make more sense? it’s a bit hard to describe.

    I appreciate your assistance btw. I’ve noticed there are several other posts about the same thing in the forums and none have answers.

    Ah, I missed something before–you want to put PHP code in a Custom Field, then look at the PHP eval code:

    You would need to assign your custom field to a variable then use that variable in the eval statement:

    <?php $mycode=get_post_meta($post->ID, 'leftBox', true); ?>
    <?php eval('?>'.$mycode); ?>

    Great! that works exactly the way I wanted it to.

    one more question: this code seems to terminate any PHP code coming directly after. In my editor the last ?> is highlighted as an error. I’m assuming I can fix this with some form of conditional statement?

    Much appreciated.

    Wonder if that’s because of the ?> in the eval('?>'.$mycode) statement?

    Thinking about this, I believe you should put the code in the template and test for the custom key or value of the custom field, then do whatever code you need based on the particular custom field value.

    Then if you ever decide to do things differently (e.g. display only 3 posts instead of 5), you only have to change one template file, rather than edit code in each custom field on each post.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Recent posts list in a custom field?’ is closed to new replies.