WordPress.org

Ready to get started?Download WordPress

Forums

Show WordPress on External site with thumbnails (10 posts)

  1. Dave
    Member
    Posted 1 year ago #

    I am able to show my latest WP post titles on a .html site with

    <?php
    /* Short and sweet */
    define('WP_USE_THEMES', false);
    require('./wp-blog-header.php');
    ?>

    and then

    <?php
    global $post;
    $args = array( 'posts_per_page' => 3 );
    $myposts = get_posts( $args );
    foreach( $myposts as $post ) :	setup_postdata($post); ?>
    <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a><br />
    <?php endforeach; ?>

    This only shows the titles though.

    How can I get the thumbnail (300px) or an excerpt with Thumbnail showing through?

  2. Dave
    Member
    Posted 1 year ago #

    I've tried `<?php
    global $post;
    $args = array( 'posts_per_page' => 3 );
    $myposts = get_posts( $args );
    foreach( $myposts as $post ) : setup_postdata($post); ?>
    <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?> <?php the_post_thumbnail(); ?></a><br />
    <?php endforeach; ?>`

    But I'm getting a full blown image rather than thumbnail.

  3. Dave
    Member
    Posted 1 year ago #

    So the issue is that it's pulling featured image which is not displayed in the post ... Hence there's no thumbnail.

    Anyone know the best resolution for this? eg. how to make featured images have thumbnails?

  4. catacaustic
    Member
    Posted 1 year ago #

    Featured imaged do have thumbnails, but not all pages/posts have featured images. If the_post_thumbnail() isn't showing anything, that means that there's no featured image set for that post.

    To get around that you need to do a check before you try to output it. As an example...

    <?php if (has_post_thumbnail ()): ?>
        <?php the_post_thumbnail ("thumbnail"); ?>
    <?php else: ?>
        <img src="fallback_image_file.jpg" />
    <?php endif; ?>
  5. Dave
    Member
    Posted 1 year ago #

    Yes featured images are displaying on the blog along with thumbnails. The issue comes about when having them shown on html with the above code.

    Rather than pulling a thumbnail the above code seems to be pulling the full sized image.

    Are you suggesting to replace

    ?php the_post_thumbnail(); ?>

    With `<?php if (has_post_thumbnail ()): ?>
    <?php the_post_thumbnail ("thumbnail"); ?>`?

  6. catacaustic
    Member
    Posted 1 year ago #

    When you read the codex page for the_post_thumbnail() you can see that the first argument for it is the size that you want the image to be, and the default is to supply the image at the thumbnail size, but I'd suggest that you specify that you want 'thumbnail'. It should work without that, but... I prefer to specify it so I know that it's going to work as expected.

    If you are not worried about showing a default image if there's no thumbnail then you don't have to use the if() statement. I added that there because in your posts before you kept saying that there was no thumbnail for the post, so that was probably me getting confused about what you were really after.

  7. Dave
    Member
    Posted 1 year ago #

    I tried specifying a size

    <?php the_post_thumbnail( '100' ); ?>

    But it's still putting the full sized image up.

    So if I upload a 500px image it's displaying 500 px. Ditto if I upload a 100px. Seems like there's not thumbnails being made? Which is strange as featured posts do show as thumbnails in excerpts etc.

  8. catacaustic
    Member
    Posted 1 year ago #

    Did you try using the word 'thumbnail' instead of a number? What I can see wrong is that your exanple uses quotes around the number - which makes it a string and not a numerical value so its treated as the name of a size and not a dimension.

  9. Dave
    Member
    Posted 1 year ago #

    Yes! that worked!

    <?php the_post_thumbnail(thumbnail); ?>

    Thank you very much for your help. I don't think I'd ever have tried just using the word "thumbnail" instead of size :)

  10. catacaustic
    Member
    Posted 1 year ago #

    That's why it's always good to read the codes pages for the functions that you're trying to use. On there is says about the 'size' attribute:

    Either a string keyword (thumbnail, medium, large, full), or any custom size keyword defined by add_image_size(), or a 2-item array representing width and height in pixels, e.g. array(32,32).

    So you were doing it wrong, and the default for invalid parameters is to return the image as full size. I've found that 99% of the times that I've had a function not work correctly it's been me trying to pass invalid or incorrect values to it.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.