I'm not super familiar with wordpress, but I've been working on getting my layout written as a theme so I can use the wordpress platform. For some reason I can't get the_post_thumbnail function working. I already added
add_theme_support( 'post-thumbnails' );
to my functions.php file and yes I did set a featured image and include the_post_thumbnail(); in the loop.
What am I doing wrong? I went as far as to go through the actual function and found that it is having problems with the $image variable in the wp_get_attachment_image() function while referencing the wp_get_attachment_image_src() function. $image is empty, when I'm guessing it shouldn't be. It get's the post_thumbnail id just fine though so I know it knows there's an image set. It just won't display the darn thing.
I'm writing myself a custom theme from scratch so functions.php only has add_theme_support( 'post-thumbnails' ); in it right now if you're curious.
Edit:
Here's my loop:
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div class="home-entry clearfix" id="post-<?php the_ID(); ?>">
<a href="<?php the_permalink() ?>" rel="bookmark" >
<?php
if (has_post_thumbnail()) {
the_post_thumbnail();
} else {
echo '<img class="home-thumb trans-border" src="' . catch_first_image() . '" width="200px" height="150px" title="' . the_title() . '" />';
}
?>
</a>
<div class="home-post">
<div class="home-meta">Posted <?php the_time('M j, Y'); ?> - <?php the_category(' , ') ?> - <?php comments_popup_link('No Comments', '1 Comment', '% Comments'); ?></div>
<h2 class="post-title">
<?php the_title(); ?>
<a class="read" href="<?php the_permalink(); ?>" title="Read More">Read More</a>
</h2>
<div class="home-excerpt">
<?php the_excerpt(); ?>
</div>
</div>
</div>
<?php endwhile; ?>
<?php echo paginate_links() ?>
<?php else : ?>
<h2>Nothing Found</h2>
<?php endif; ?>
Continued:
So I went and found a theme with featured image support and copied that part of the loop exactly and still nothing:
<?php if(has_post_thumbnail()) {
echo '<span class="thumbnail">';the_post_thumbnail(array(100,100)); echo '</span>';
} else {
$image = evlget_first_image();
if ($image):
echo '<span class="thumbnail"><img src="'.$image.'" alt="';the_title();echo'" /></span>';
endif;
} ?>
So what the heck could it be? I'm so confused...
This function will echo itself.
How to solve: Remove the echo in front of the function.
check this
Are your images locally stored on your server/local machine?
If not, the_post_thumbnail() won't work as it can't retrieve an image based on a URL.
Related
I have an ACF repeater, which repeats post objects. I am changing the postdata to the post object rather than the page so that i can get the title and thumbnail image. This works perfectly for the first one, however the subsequent objects pull the correct thumbnail but the title is pulled from the page title.
Heres the code:
<?php if( have_rows('service_repeater') ): ?>
<?php while ( have_rows('service_repeater') ) : the_row(); ?>
<?php $post_object = get_sub_field('service'); ?>
<?php if( $post_object ): ?>
<?php $post = $post_object; ?>
<?php setup_postdata( $post ); ?>
<?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); ?>
<a href="<?php the_permalink(); ?>" class="service">
<div class="background" style="background-image: url('<?php echo $url; ?>');"></div>
<div class="content">
<h3><?php the_title(); ?></h3>
<p><?php the_field('read_more_text'); ?></p>
</div>
</a>
<?php unset($post_object, $post); ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
<?php endwhile; ?>
Any help would be greatly appreciated!
Ok, so i removed the <?php unset($post_object, $post); ?> and it works now. Im concerned that this will give me trouble later down the line with variables however.
I would like to display a few post titles from a specific category on homepage. First one have to be with a small thumbnail and excerpt and rest of them just title. Below this section I would like to have a link clicking on which will show all the posts under this category.
like this http://i.stack.imgur.com/N5jUA.jpg
As arslaan ejaz said, you can use wp_query. But I think that answer is not enough for your question. You want to show first post with thumbnail and others with titles only right?. It can be done by php count. Here is what I am using on my site. check below code, it will show first post with thumbnail, title and excerpt, other three posts with title only from category ID 1.
<div class="main-div">
<div class="thumbnaildiv">
<?php $count=1; $query = new WP_Query('showposts=4&cat=1&offset=0'); if ($query->have_posts()) : ?>
<?php while ($query->have_posts()) : $query->the_post(); ?>
<?php if($count==1){ ?>
<h2>
<a href="<?php the_permalink() ?>" rel="bookmark">
<?php the_title(); ?></a>
</h2>
<div class="thumb">
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail('thumbnail'); ?>
</a>
<p><?php the_excerpt(); ?> ...</p>
</div>
</div><!--div with thumbnail, title, and excerpt end-->
<div style="clear:both"></div>
<div class="without-thumb">
<ul>
<?php } if($count>1){ ?>
<li>
<a href="<?php the_permalink() ?>" rel="bookmark">
<?php the_title(); ?></a>
</li>
<?php } if($count==4){ ?>
</ul>
<?php } ?>
<?php $count++; endwhile; else: endif; wp_reset_postdata(); ?>
</div><!--div without thumbnail end-->
</div><!--main div end-->
The div's I have used is for information purpose only. You can change and style it as desired.
Use WP-Query:
<?php
$args = array('cat'=>1);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
echo '<li>' . get_the_title() . '</li>';
echo '<li>' . the_permalink() . '</li>';
echo '<li>' . the_excerpt() . '</li>';
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
It will list all posts of category 1, with title, permalink, and excerpt
More info: wp_query
I can recommend you to add 'Elementor' plugin. With this plugin, you can add 'read more' and split your text. If you add 'read more' in the beginning of your text then there will be shown only title and under the title a 'read more' link.
Here is the Screenshot
How do I link a post's featured image to its full size version on a category page?
Here is my loop on my category.php:
<h1>Professional Portfolio Gallery - <?php single_cat_title(); ?></h1>
<p><?php echo category_description(); ?></p>
<?php if (have_posts()) : ?>
<ul class="portfolio">
<?php while (have_posts()) : the_post(); ?>
<li>
<?php
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail();
}
?>
<?php the_excerpt(); ?></li>
<?php endwhile; ?>
</ul>
<?php wp_paging(); ?>
<?php else : ?>
<h1>Not Found</h1>
<p><?php _e("Sorry, but you are looking for something that isn't here."); ?></p>
<?php endif; ?>
Any help would be appreciated!
To link Post Thumbnails to the full size version you should be able to use the following:
<?php
if ( has_post_thumbnail()) {
$full_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'full');
echo '<a href="' . $full_image_url[0] . '" title="' . the_title_attribute('echo=0') . '" >';
the_post_thumbnail('thumbnail');
echo '</a>';
}
?>
You can find out more about this redirect here: http://codex.wordpress.org/Function_Reference/the_post_thumbnail
Hope that helps!
I am using an if elseif statement to check what page it is, and it requires that there are posts on the page so when a search is returned with a result of 0, my code stops working. Any ideas how to code this better?
this code is in my sidebar and is showing the recent articles, you can see an example of a search working here(the sidebar is setup as a sub-footer):
http://ivry.sweetyams.ca/?s=new
and a search not working here:
http://ivry.sweetyams.ca/?s=asjdfkl%3B
Code I am using:
(I have tried putting stuff into the else{ code and it doesn't work either because there are no posts on the 'nothing found' search page
<?php if (have_posts()) : ?>
<?php /* IF SEARCH PAGE */ if (is_search() ) { ?>
<?php query_posts('category_name=0&showposts=5'); ?>
<?php while (have_posts()) : the_post(); ?>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
<p> <?php the_title(); ?></p>
</a>
<?php endwhile;?>
<?php /* IF ESCALADE PAGE */ }elseif (is_category_or_sub(6)) { ?>
<?php query_posts('category_name=escalade&showposts=5'); ?>
<?php while (have_posts()) : the_post(); ?>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
<p> <?php the_title(); ?></p>
</a>
<?php endwhile;?>
<?php /* IF MONTAGNE PAGE */ } elseif (is_category_or_sub(14)) { ?>
<?php query_posts('category_name=montagne&showposts=5'); ?>
<?php while (have_posts()) : the_post(); ?>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
<p> <?php the_title(); ?></p>
</a>
<?php endwhile;?>
<?php } else { ?>
DO SOMETHING ELSE
<?php }?>
<?php endif; ?>`
I am trying to get all recent articles for my sidebar and when there is no result in the search, it also stops my sidebar from working.
The code I added is not in my search page but in my SIDEBAR, it separates the comments into 2 categories, (6 and 14) I am essentially splitting my site using categories for navigation, anything as a child below 6 will be styled one way and 14 the other, you can see the difference on the escalade and montagne links:
This is getting every post below escalade, OR every post below the montagne category
My search pulls results from ALL categories, but if there is no search result, for some reason my sidebar code, the code I included, doesn't work.
first, showposts is deprecited since v2.1, used posts_per_page instead.
second, after each query_post, you need to reset the query like this:
<?php
// The Query
query_posts( $args );
// The Loop
while ( have_posts() ) : the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;
// Reset Query
wp_reset_query();
?>
this is how I understand it. see more detail here:query_posts(), hope this helps.
I found a way to get it to work,
I found that it is encouraged to use WP_Query for secondary posts within a page, http://new2wp.com/noob/query_posts-wp_query-differences/
also, for anyone that is wondering the is_category_or_sub function is from here: http://valendesigns.com/wordpress/is-category-or-subcategory-wp-function/
This code checks whether you are on a category or subcategory,
<?php
$my_posts = new WP_Query(); ?>
<?php /* IF ESCALADE PAGE */ if (is_category_or_sub(6)) { ?>
<?php $my_posts->query('posts_per_page=5&cat=6');?>
<?php /* ELSEIF MONTAGNE PAGE */ } elseif (is_category_or_sub(14)) { ?>
<?php $my_posts->query('posts_per_page=5&cat=14'); ?>
<?php } /* AND OTHER PAGE */ else { ?>
<?php $my_posts->query('posts_per_page=5'); ?>
<?php }?>
<?php while ($my_posts->have_posts()) : $my_posts->the_post(); // loop for posts ?>
<a href="<?php echo esc_url( get_permalink( $post->the_permalink ) ) ?>" title="Permanent Link to <?php echo substr($post->post_title,0,200);?>">
<p><?php echo substr($post->post_title,0,200); ?></p>
</a>
<?php endwhile; ?>
<?php wp_reset_query() // RESET QUERY ?>
I want to use the plugin WP Smart Sort to sort my posts alphabetically, except I need the most recent posted first on the home page. The relevant existing code for the home page is below. Is there something I can add to it to force it to include the recent posts and go by date rather than by the alphabetical order I want everywhere else?
(In case it helps to see the context, the site is at http://mormonscholarstestify.org/)
<div id="homebox">
<?php if ($aOptions['homebox-id'] != '') { query_posts('showposts=5&cat=' . $aOptions['homebox-id']); } ?>
<?php if(have_posts()) : the_post() ?>
<div id="boxmain">
<img src="<?php $key="thumbnail"; echo get_post_meta($post->ID, $key, true); ?>" alt="<?php the_title() ?>" />
<h3><?php the_title() ?></h3>
<?php $key="affiliation"; echo get_post_meta($post->ID, $key, true); ?><p>
<?php the_excerpt(); ?>
</div>
<?php endif; ?>
<?php while(have_posts()) : the_post() ?>
<div class="boxitem">
<img src="<?php $key="thumbnail"; echo get_post_meta($post->ID, $key, true); ?>" alt="<?php the_title() ?>" />
<h3><?php the_title() ?></h3>
<?php $key="affiliation"; echo get_post_meta($post->ID, $key, true); ?>
</div>
<?php endwhile; ?>
</div>
According to the Wordpress query_posts documentation you should be able to supply an orderby parameter to the querystring to change how the posts are ordered, like so:
query_posts('showposts=5&orderby=date&order=ASC&cat=' . $aOptions['homebox-id']);