WordPress.org

Ready to get started?Download WordPress

Forums

Recent posts list in a custom field? (8 posts)

  1. Morten Rand-Hendriksen
    Member
    Posted 5 years ago #

    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?

  2. MichaelH
    Member
    Posted 5 years ago #

    How about using the the Custom Field Field parameters of the template tag, query_posts()?

    Related:
    Stepping Into Template Tags
    Stepping Into Templates
    Template Hierarchy

  3. Morten Rand-Hendriksen
    Member
    Posted 5 years ago #

    I'm obviously doing something wrong here.

    In my template I have:

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

    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'); ?>
    
    <ul>
      <?php while (have_posts()) : the_post(); ?>
      <li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
      <?php endwhile;?>
    </ul>

    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?

  4. MichaelH
    Member
    Posted 5 years ago #

    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.

    Example:

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

    or

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

  5. Morten Rand-Hendriksen
    Member
    Posted 5 years ago #

    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.

  6. MichaelH
    Member
    Posted 5 years ago #

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

    http://us3.php.net/eval

    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); ?>
  7. Morten Rand-Hendriksen
    Member
    Posted 5 years ago #

    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.

  8. MichaelH
    Member
    Posted 5 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic