Posts with images
-
Hello, how can I display recent posts with a featured image?
-
What theme are you using? Where did you download it from?
I made a custom theme. Using 4.0.1 WordPress
In general, you’d do something like this:
<?php if ( has_post_thumbnail() ) : the_post_thumbnail(); endif; ?>You can use CSS to style the image as necessary.
Thanks, I’ll try it and respond in an hour.
How can I target images to be those thumbnails?First make sure you’ve added support for post thumbnails in your theme’s
functions.php:add_theme_support( 'post-thumbnails' );Then, you should be able to assign a post thumbnail to a blog post or static page in the editing screen. There should be a metabox in the right-hand column labeled “Featured Image” and a link to “Set featured image”. Clicking that link allows you to choose an image from your Media Library or to upload one. Check out Post Thumbnails in the Codex for more information: http://codex.wordpress.org/Post_Thumbnails
Thank you both for contributing. I’ll make sure to try when I’m at my computer. After 2-3h.
My functions.php does not recognize the code:
if ( function_exists( ‘add_theme_support’ ) ) {
add_theme_support( ‘post-thumbnails’ );
set_post_thumbnail_size( 150, 150, true ); // default Post Thumbnail dimensions (cropped)// additional image sizes
// delete the next line if you do not need additional image sizes
add_image_size( ‘category-thumb’, 300, 9999 ); //300 pixels wide (and unlimited height)
}or simply
add_theme_support( ‘post-thumbnails’ );
The code just appears as text in the admin panel. :/
Is that your entire
functions.php? Do you have the opening<?phptag in the file? Is the file namedfunctions.php(note the plural)?OK, one problem solved. The featured image metabox appeared in the admin panel. But can the featured image be displayed on the recent posts tab?
Here is my index.php if I did not make a lot of sense. I reckon there should be an additional php snippet to display the featured image?
To clarify, when you say the “recent posts tab”, are you referring to WordPress’ built-in widget, or are you referring to a section in the
index.phpthat you posted a link to?I’m referring to this part of the code: http://pastebin.com/F1wN5k7x
Which displays recent posts.
In general, you would do something like this:
<div class="col-md-10"> <?php while(have_posts()) : the_post(); ?> <?php if ( has_post_thumbnail() ) : ?> <?php the_post_thumbnail(); ?> <?php endif; ?> <h3><a href="<?php the_permalink('http://viso.lv/wordpress/?p=76'); ?>"><?php the_title(); ?></a></h3> <p><?php the_excerpt(); ?></p> <p class="text-muted">Опубликовано <?php the_author(); ?> на <?php the_time('F jS, Y'); ?></p> <?php endwhile; wp_reset_query(); ?> </div>You can then use CSS to style the image as needed.
Woohoo thanks a lot! Now it’s time to proceed. 🙂
Can I ask one more question?
How would you suggest to code my navbar if it’s hardcoded with simple a tags? Should I implement php snippets or just put a link to the pages?Here is the code for the menu: http://pastebin.com/GMZ1uFsh
You could call wp_nav_menu() to display the menu instead. An advantage of doing it this way is that
wp_nav_menu()automatically adds a bunch of HTML classes that can be used for styling via CSS. You would have to call register_nav_menus() in yourfunctions.phpfor it to work:register_nav_menus( array( 'primary' => 'Primary Menu' ) );And then call it in your
header.phplike this:<?php wp_nav_menu( array( 'theme_location' => 'primary' ) ); ?>Thank you a lot once again! Now I feel more confident in WordPress and the website is building well. Have a nice day/evening! 🙂
The topic ‘Posts with images’ is closed to new replies.