Issues using the_post_thumbnail in wordpress - php

I'm teaching myself Wordpress, and my current goal is to display 4 thumbnails of images posted in a particular category.
I've already got the posts being pulled in by category, however I cannot pull ONLY the images. It's either the images and post text, or none at all.
My code is posted here: http://pastebin.com/NZ7fyyPA
I've tried simply replacing
<?php the_content(); ?>
with:
<?php the_post_thumbnail(); ?>
but that didn't work at all. Nothing came back at all!
I've even tried simply removing:
<?php the_content(); ?>
and same as above! I was thinking that because I have
<div class="entry-thumbnail">
<?php the_post_thumbnail(); ?>
</div>
That would pull the thumbnail in only, so I thought removing the content would help but nope!
I've read through the Codex here: http://codex.wordpress.org/Function_Reference/the_post_thumbnail
and the only question I have from that, is that immediately the Codex says:
"Use get_the_post_thumbnail($id, $size, $attr ) instead to get the featured image for any post."
Which I don't think I'm doing, because I haven't setup anything related to a 'featured image' in my theme...or is what I'm trying to do considered using a featured image? I'm only uploading images and posting them inside of the blog posts.
Sorry for the long write up, I'm sure it's something simple!
Thanks!

One more thing you can set a featured image from admin and call that image with this code the_post_thumbnail( $size, $attr ); and when you want to show anything from content part then you can call with this code simply. the_content();
you can also use excerpt for this as the_excerpt();
Thanks

This is a bit of a hack; however, it works.
While viewing your Wordpress website, use the browsers developer tools to target the 'post text'. You'll be able to see the id that is associated with that data-set.
Then, in your CSS. Target that id and use display:none;
Example:
#posttext {
display:none;
}

Related

How Set Image URL As Featured Image?

I have article with title "POST-TITLE" and I create image title same with post title, "POST-TITLE.jpg".
All image I put in my hosting, like this:
http://www.example.com/img/CATEGORY/POST-TITLE.jpg
In mytheme post-single.php, I have this code:
<img src="http://www.example.com/img/<?php foreach((get_the_category()) as $category) { echo $category->cat_name . ''; } ?>/<?php the_title(); ?>.jpg" onError="this.src='http://cerpenkoran.net/img/dot.jpg';" title="Thumb for <?php the_title(); ?>" >
My answer:
What code I add the image url code above, so it as Featured Image?
OR how to combine this fix URL:
http://www.example.com/img/<?php foreach((get_the_category()) as $category) { echo $category->cat_name . ''; } ?>/<?php the_title(); ?>.jpg
with this Featuread Image code:
<?php if (has_post_thumbnail( $post->ID )) : ?>
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
<img src="<?php echo $image[0]; ?>" />
<?php endif; ?>
Are you asking how to add a Featured Image to your WordPress website? If so, then follow the below instructions:
Firstly, you are going to need to add the relevant Theme Support inside your functions.php file. To achieve this, go into your functions.php file and enter the following code:
<?php
function my_theme_name_support(){
add_theme_support('post-thumbnails');
}
add_action('after_setup_theme', 'my_theme_name_support');
?>
Make sure that the above code is placed after the opening <?php tag (at the top of the functions.php file) but before the closing ?> tag (at the bottom of the functions.php file.)
You will see that there are two my_theme_name_support entries. You can change the wording of these to something more appropriate. Just make sure they match.
Now, head into your WordPress Dashboard and go to create a New Page or New Post. You should now be able to see an entry, entitled 'Featured Image'. As you scroll the page, you should see it appear on the right hand side. Simply select the link and add the required image.
This, alone, does not output the Featured Image. To output your Featured Image, you now need to enter the following code into the relevant file(s) on your server:
<?php the_post_thumbnail('thumbnail'); ?>
By following the above steps, you should now be able to dynamically call your Featured Images from your WordPress Dashboard and output them to whichever pages you want.
For more information, on Featured Images, check out this WordPress Codex Article >>>
Answer Extension
If you want the same image to appear for every Blog Post, so that you do not have to manually add a Featured Image to every Blog Post, you could:
Upload the image to WordPress, via their Media Uploader.
Then head to the single.php Blog Post Template File and enter the following code:
<img src="POST-TITLE.jpg" alt="post-title" height="42" width="42">
Be sure to make the following modifications, to the above code:
src: This is the link to the actual image. Therefore, change 'POST-TITLE.jpg' to the name of your desired image. Be sure to include the correct image format (.jpg, .png etc).
alt: This is a brief description of the image. No coding required here. Plain text will do.
height and width: Insert the correct dimensions. The dimensions are in pixels.
Please Note ...
single.php is the default Blog Post Template File. There is a chance that there are additional Blog Post Template files too. If this is the case, you will need to add the code to these other Template Files too.
You may find this WordPress Article, helpful, when working with Template Files.

My single.php page does not show the related data to the post which is clicked

I have a post for my site's logo and I have inserted its thumbnail in my header.php using the code below and it works fine:
<div id="logo">
<?php
$my_query = new WP_Query('showposts=1&cat=12');
while ($my_query->have_posts()):
$my_query->the_post();
$do_not_duplicate = $post->ID;?>
<?php the_post_thumbnail( 'site-logo'); ?>
<?php endwhile; ?>
</div>
Beside this I have a single.php file where I show the details of a post using Advanced Custom Fields plugin's codes and my problem is when I click on a post and I get redirected to single.php instead of seeing its thumbnail and details I see the site's logo and the details are empty.
This happened right after I added my logo as a post and used that post's thumbnail. Before that my single.php page was functioning very well.
How can I fix this without removing the logo? (Or if this problem is because of inserting logo this way recommend me a better way.)
Thanks.
I got my answer here. Thanks to WebElaine. All I need to do
is:
This probably has to do with the custom query - it's preventing other queries from working properly.
Setting a logo as a post seems an odd approach. Media attachments are already posts in and of themselves. Additionally, WP has a nice logo feature - custom-logo. I would add the logo that way and then just call the theme mod for the logo. That way you're not adding an extra query that throws off other queries.
In theme's functions.php:
<?php add_theme_support('custom-logo'); ?>
In theme's header.php:
<?php the_custom_logo(); ?>

How to show page_title instead of post_title

So I had the idea to create a div with the class 90. where the <?php the_title( '<h3>', '</h3>' ); ?> of the page is shown in a 90° angle.
Works great. Just some tweaking to get the layout working correctly.
I added in the reading settings the option that A static page is selected for my posts.
Now I want that the page titel is shown in the DIV 90 and not the post title. But apparently the <?php the_title ?> is for both of them.
Is there a way to determine what title you want selected? So in this case load/echo the page title and dismiss the post title.
Hope I explained it right.
You should do a search in the WordPress codex, has a lot of useful information there..
https://codex.wordpress.org/
This is what I believe you are after:
<h3><?php wp_title(); ?></h3>
https://developer.wordpress.org/reference/functions/wp_title/

Wordpress Featured Image only on overview

I'm using a template called 'expression' for Wordpress which works fine.
I'd just like to tweak it a little.
I have a page that shows all my blog posts, and ofcourse each blog post has an own overview if you click on it.
on each blog post you make, you can set a featured image. This image is shown on the overview page, and on each individual detailed view of 1 blog post.
I'm trying to find a way to only show this featured image on the overview page of all blogs, and not on the detailed view of a blog post.
Is there a way to do this?
I tried to edit the code in post-template.php by adding:
ispage('blog')
full code:
if(has_post_thumbnail() && !$hide_thumbnail && !is_page('blog')){ ?>
<div class="blog-post-img">
<?php if(!is_single()){?>
<a href="<?php the_permalink(); ?>">
<?php }
$img_id=$pex_page->layout=='full'?'post_box_img_full':'post_box_img';
the_post_thumbnail($img_id);
if(!is_single()){
?>
</a>
<?php } ?>
</div>
<?php
}
this works, but both featured images will be gone. which is not what I wanted.
Thanks for your help!
Got it eventually,
you have to use:
is_single();
instead of :
is_page();
Thanks anyway.

Calling a WordPress Post Based on Popularity

If you look at my site, I have an area named Top Winter Projects in the right-side navigation. I would like to automate this part of the site as I currently have to manually paste HTML into that particular section. What I want to be able to to do is have those images (which should link to the corresponding post) change based on date.
I've tried using the featured image plugin, but that doesn't seem to work with my theme for some reason. Is there an alternate route?
I've tried piecing some code together, but i'm a bit clueless when it comes to php. I'm also not sure how i'd get a picture to show up. Here is what I have, where 40, 50, & 60 are three posts. Can echo calls show pictures?
<? php
$args = array('include' => 40, 50, 60 )
echo get_posts($args);
?>
Here is my website: http://www.merrimentdesign.com
Thanks, any help is appreciated.
One way to do it is to create a loop using WP_Query and then you can put whatever content you want in the loop. I typically do this and use categories to control what posts are shown in each area. So for example if you created a category called featured, you could tag every post you want to show in that section with that category. Then you could create a simple loop like :
<?php $the_query = new WP_Query('category_name=featured&showposts=10'); ?>
<?php while ($the_query->have_posts()) : $the_query->the_post();?>
<a href="<?php the_permalink() ?>" rel="bookmark" class="postTitleLink">
<?php the_post_thumbnail(); ?>
</a>
<?php endwhile; ?>
This is assuming you have the thumbnail function enabled in your themes functions.php.
add_theme_support( 'post-thumbnails' );

Categories