WordPress.org

Forums

"Latest Post" on external PHP page hack? (22 posts)

  1. Heartless_King
    Member
    Posted 9 years ago #

    Was wondering if there is a hack which allows me to display the latest post title and author and an external php page outside of the script?

    Thanks,
    -King :)

  2. Heartless_King
    Member
    Posted 9 years ago #

    Anyone? :(

  3. sylla2
    Member
    Posted 9 years ago #

    "and an external php page" or "as an external php page" ?

    And is it an entry you wanna display or just meta data?

  4. Thord Daniel Hedengren
    Member
    Posted 9 years ago #

    I guess you mean to display latest post on an external page, written in php... The easiest way in my book is using some RSS-reading script, there's a bunch of those out there.

  5. cam_oai
    Member
    Posted 9 years ago #

    i use this one in in my website http://www.vietzon.com

    -----------------

    <?php $posts = get_posts( "numberposts=5" ); ?>
    <?php if( $posts ) : ?>
    <?php foreach( $posts as $post ) : setup_postdata( $post ); ?>

    <span class="date"><?php the_time('M d'); ?>:</span> " title="click here to more..."><?php the_title(); ?>
    <?php endforeach; ?>
    <?php endif; ?>

    -----------------

  6. Heartless_King
    Member
    Posted 9 years ago #

    Thanks guys, I'm gonna try "cam_oai's" code. Ill let you know how it goes :)

    -King

  7. cam_oai
    Member
    Posted 9 years ago #

    here is what i did:
    1. create a new php and add this two before <body>
    <?php
    require('blog/wp-blog-header.php');
    require_once("blog/wp-config.php");
    ?>

    change the dir to the correct path.

    2. paste the code:

    <?php $posts = get_posts( "numberposts=5" ); ?>
    <?php if( $posts ) : ?>
    <?php foreach( $posts as $post ) : setup_postdata( $post ); ?>

    <span class="date"><?php the_time('M d'); ?>:</span> " title="click here to more..."><?php the_title(); ?>
    <?php endforeach; ?>
    <?php endif; ?>

    that's all. if you only paste the code and not the 2lines before <body> then it will not know "where to get the posts" :)

    check out my http://www.vietzon.com . the code gets 5latest post :)

  8. Heartless_King
    Member
    Posted 9 years ago #

    Thanks cam_oai, finally found spare time to give code a go and it works! :)

    To extend latest posts, what do I have to call to get the "author" and "postlink (permalink maybe?)"

  9. sailor73
    Member
    Posted 9 years ago #

    Is it possible to export ‚ÄúLatest posts‚Ä? to another site? And in HTML?

  10. Heartless_King
    Member
    Posted 9 years ago #

    Unfourtunately you can't. Its breaches security to insert code from foreign sites. Also HTML is a know as the output needs to be built before the page is loaded.

    Thanks,
    -King

  11. cam_oai
    Member
    Posted 9 years ago #

    Heartless_King: if you have time, take a look at the themes. download one and play with it. i just rip off the index.php in the theme folder and then remove bit by bit.

    postlink: " title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?>

    author: <?php the_author() ?>

    somehow in can't get the author-thing to work, not in my own index.php, but in the themes index is OK.

  12. the-faun
    Member
    Posted 9 years ago #

    Including wp-blog-header.php and wp-config.php in order to use regular WP functions is a bit gratuitous in this case.

    I penned together a basic PHP/mySQL query to handle it for my own site; you can see it here

  13. noslee3
    Member
    Posted 8 years ago #

    Hi all:

    Thanks...cam's code works great for me. I'm in the first stages of implementing it(see http://diseducation.com/newsletter/lastpost.php) but I want to see if I can take it a step further.

    Can I get the most recent comment as well as the most recent post, and put it in an external page?

    Thanks for the help anyone...

  14. davidchait
    Member
    Posted 8 years ago #

    If you use an RSS aggregator (like my CG-FeedRead) to do the work, you could get the most recent post, or comment, or comment on a particular post, or anything else you can pick up via RSS. ;)

    RSS has another advantage: so long as you are using a caching aggregator (CGFR does, and I think most of the decent ones do), then it is only an html include on pageload while it is cached. When the timer expires and it needs to refresh, it polls the WP site for the RSS, and gets what it needs. In any case, you never directly include WP in your page, and you certainly never directly hit a SQL database to look something up.

    Just IMHO, a much better way to approach the problem. ;)

    -d

  15. noslee3
    Member
    Posted 8 years ago #

    thanks...I'll give it a shot.

  16. Skettalee
    Member
    Posted 8 years ago #

    Hey i cant get the actually permalink code from you guys on here because it turns the code into an actual code and makes a link on your text. any help?

  17. keeva
    Member
    Posted 8 years ago #

    I'm new to PHP. So apologies upfront... This hack works great, except it kills the ability for me to read a variable from the URL I use in another small script.

    For example, I need to pass "homefolio" variable in the URL of this page http://goldmandesign.com/index-test.php?_homefolio=25 to "25" If I don't use this hack I can easily echo this variable into the page. But once I use the hack and include <?php include('blog/wp-blog-header.php'); ?> specifically I can no longer echo the variable from the URL string.

    And suggestions?

    thanks in advance,

    - Kevin

  18. introvert
    Member
    Posted 8 years ago #

    I cannot seem to get this code working. It's not displaying properly. With this code, all I'm seeing in the area where the latest posts should display is the following:

    Jun 25: " title="click here to more...">Test Jun 25: " title="click here to more...">Hello world!

    Obviously it's pulling the data, as I have those two posts in my WP blog, but it's definitely not displaying them correctly.

    I copied and pasted cam_oai's code directly and did not alter it. Any suggestions?

  19. moshu
    Member
    Posted 8 years ago #

    If the code has ONLY the date and title tags - it will display exactly that.

    Use whatever template tag you want to be displayed:
    http://codex.wordpress.org/Template_Tags
    (e.g. the_content > for content etc.)

  20. introvert
    Member
    Posted 8 years ago #

    Well, you definitely got me moving in the right direction.

    Now, is there an easy way to put comment links on those entries so people can continue on to the actual blog and make a comment?

  21. moshu
    Member
    Posted 8 years ago #

    As I said above: just use any template tag and/or code snippet that exists in the original Loop and it will work.
    See the Loop:
    http://codex.wordpress.org/The_Loop

    Alternatively, you take a lok at this tutorial, too:
    http://www.transycan.net/blogtest/2005/07/05/integrate/

  22. keeva
    Member
    Posted 8 years ago #

    I mentioned this hack was messing up URL variables for another script I have running along side WP. I found the easy fix, which is to put the snippet of HTML and code for this hack in a separate file as an include. It works great. Thanks for thread and the notes on how to create this.

Topic Closed

This topic has been closed to new replies.

About this Topic