WordPress.org

Ready to get started?Download WordPress

Forums

Display Custom Fields posts on static page (6 posts)

  1. Digitalminds
    Member
    Posted 5 years ago #

    I'm using the code below to display the titles of the last 8 posts on a static page:

    <?php
    # Here starts Mini Loop trick: You can use this code, but _at your own risk_
    # If you want to improve this code, you're welcome ;)
    $how_many=8; //How many posts do you want to show
    ?>
    <ul id="news">
    <?php
    $news=$wpdb->get_results("SELECT ID,post_title FROM $wpdb->posts
    WHERE post_type=\"post\" AND post_status= \"publish\" ORDER BY post_date DESC LIMIT $how_many");
    foreach($news as $np){
    printf ("

  2. %s
  3. ", $np->ID,$np->post_title);
    }
    ?>

    Now I want also to show the value of a custom field after the title, does anyone know how to realize this?

  • MichaelH
    Member
    Posted 5 years ago #

    You'll want to use something like get_post_custom but review Using_Custom_Fields for a discussion of the topic.

    Also might direct you to other possible choices on presenting posts:
    http://www.daobydesign.com/blog/2007/07/modifying-wordpress-front-page/
    http://weblogtoolscollection.com/archives/2008/04/13/define-your-own-wordpress-loop-using-wp_query/

  • Digitalminds
    Member
    Posted 5 years ago #

    Sorry Michael but I don't see the connection between my problem and your links. I've already the titles of the posts now I also want to display the value of a custom field but it doesn't work with the examples mentioned in the codex using_custom_fields

  • MichaelH
    Member
    Posted 5 years ago #

    Please place the code you did use in a pastebin and report the link back here. Thanks.

  • Digitalminds
    Member
    Posted 5 years ago #

    <?php
      # Here starts Mini Loop trick: You can use this code, but _at your own risk_
      # If you want to improve this code, you're welcome ;)
      $how_many=8; //How many posts do you want to show
    ?>
    <ul id="news">
    <?php
      $news=$wpdb->get_results("SELECT ID,post_title FROM $wpdb->posts
        WHERE <code>post_type</code>=\"post\" AND <code>post_status</code>= \"publish\" ORDER BY <code>post_date</code> DESC LIMIT $how_many");
      foreach($news as $np){
        printf ("<li><a href=\"index.php?p=%s\">%s</a></li>", $np->ID,$np->post_title);
      }
    ?>
  • MichaelH
    Member
    Posted 5 years ago #

    but it doesn't work with the examples mentioned in the codex using_custom_fields

    An example of the code you used, that didn't work, please.

  • Topic Closed

    This topic has been closed to new replies.

    About this Topic

    Tags