PHP if statement isn't working? - php

I'm working on a Wordpress index page that only shows posts if they have a featured image. This is my content.php code:
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<div class="col-sm-4">
<?php
if (has_post_thumbnail()) {
echo '<div class="small-index-thumbnail clear">';
echo '<a href="' . get_permalink() . '" title="' . get_the_title() . '" rel="bookmark">';
echo the_post_thumbnail('index-thumb');
echo '</a>';
echo '</div>';
}
?>
</div>
</article><!-- #post-## -->
I'm just wondering if this is enough code for someone to tell me why it's still showing the posts, despite the posts not having a featured image. Thank you!

Actually, it looks right but there is a note about this on Codex, which is something like this:
// Must be inside a loop.
if ( has_post_thumbnail() ) {
// ...
}
Note The above code apparently fails in some instances and the below code is "recommended"
if ( '' != get_the_post_thumbnail() ) {
// some code
} else {
// some code
}
Check get_the_post_thumbnail if needed.

Related

Ignore thumbnail space if there is no thumbnail

So I have the below code that loops through my posts and displays posts in a list format. Here is the code that I'm using:
<?php while (have_posts()) : the_post(); ?>
<div class="one-sixth first"><?php the_post_thumbnail(); ?></div>
<div class="five-sixths"><?php the_title(); ?></div>
<?php endwhile; ?>
Here is the image with the thumbnail:
Here is when no post thumbnail is available, it just leaves an empty space:
If check thumbnail if have displayed otherwise display placeholder image. Hope this help you.
<?php
// Must be inside a loop.
if ( has_post_thumbnail() ) {
the_post_thumbnail();
}
else {
echo '<img src="' . get_bloginfo( 'stylesheet_directory' )
. '/images/placeholder.jpg" />';
}
?>

Wordpress PHP - how do you move post thumbnail image to below post title and excerpt

I'm trying to rearrange the layout of my 'recent posts' in Wordpress. I would like the post's thumbnail image to appear below the 'post header' and 'excerpt'. I don't know PHP and wherever I move the code it doesn't seem to work.
If anyone could please advise me on what code to move and where would be a great help!
<?php }
}
else {
if ( has_post_thumbnail() ) { echo '' . get_the_post_thumbnail($post->ID, 'portfolio-thumb', array('title' => '')) . ''; }
}
?>
<div class="post-header">
<h1 class="title"><?php the_title(); ?></h1>
<span class="meta-author"><?php the_author_posts_link(); ?> </span> <span class="meta-category"> | <?php the_category(', '); ?> </span> <span class="meta-comment-count"> | <a href="<?php comments_link(); ?>">
<?php comments_number( __('No Comments',NECTAR_THEME_NAME), __('One Comment',NECTAR_THEME_NAME), '% '. __('Comments',NECTAR_THEME_NAME) ); ?></a> </span>
</div><!--/post-header-->
<?php
$excerpt_length = (!empty($options['blog_excerpt_length'])) ? intval($options['blog_excerpt_length']) : 30;
echo '<div class="excerpt">' . nectar_excerpt($excerpt_length) . '</div>';
} // default style
You can save the string in a variable. Then echo the variable where you want the image to appear:
First define the variable outside of your if/else scope (in the top of the file).
$thumbnail = '';
Then in the else scope, fill the variable with the HTML.
else {
if ( has_post_thumbnail() ) {
$thumbnail =
'<a href="' . get_permalink() . '">' .
get_the_post_thumbnail(
$post->ID,
'portfolio-thumb',
array('title' => '')
) .
'</a>';
}
}
Now echo it where you want it to appear (below excerpt).
echo $thumbnail;

WordPress custom fields outside html labels

I've created a custom field with Advanced Custom Fields plugin called 'colaboradores' with fields logo (image), direccion (string) and mapa (image);
In archive-colaboradores.php, I call the WP_Quer. In short, code is:
<?php
$args = array(
'post_type' => 'colaboradores',
'pagination' => false
); ?>
<?php $the_query = new WP_Query( $args ); ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', 'colaboradores' ); ?>
<?php endwhile; // end of the loop. ?>
So in content-colaboradores.php I show my custom fields with the_field():
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<h1 class="entry-title"><?php the_title(); ?></h1>
<?php $image = get_field( 'logo' ); ?>
<?php echo '<img src="' . $image['url'] . '" >'; ?>
<?php echo '<p>' . the_field('direccion') . '</p>'; ?>
<?php $image = get_field( 'mapa' ); ?>
<?php echo '<img src="' . $image['url'] . '" >'; ?>
</article><!-- #post-## -->
Everything is fine except direccion, which is always outside the tag checking with Google Chrome:
I've removed all the code previous to ' . the_field('direccion') . ''; ?> but the problem persists.
Do you know why is it hapening? Thanks.
I cant be certain as dev tools doesnt show anything out of the ordinary but maybe try wrapping the_field in some tags to make sure nothing out of the ordinary is coming through.
<?php echo '<p>' . strip_tags(trim(the_field('direccion'))) . '</p>'; ?>
I put the strip tags in there because thats normally why chrome and other browsers would put content outside of an inline tag like a p tag
Other than that, maybe try letting wordpress format the paragraph tags, and see if it runs then
<?php echo apply_filters("the_content",get_field('direccion')); ?>
See if either of them work?

WordPress Adding tags to loop content

I am wrapping my head on wordpress loop at the moment, im trying to give tags to the respective content, so an H tag to the title, an p tag to the excerpt and so on...
The code i got so far is
<div id="<?php echo $page_id; ?>" class="container"><!-- begin container -->
<div id="postovi" style="display:none;">
<?php $custom_loop = new
WP_Query('showposts=5&category_name=Zanimljivosti&orderby=rand');
if ( $custom_loop->have_posts() ) : echo '<ul>'; while ( $custom_loop->have_posts() ) : $custom_loop->the_post(); echo '<li>' . get_the_title() . get_the_post_thumbnail($loop->post->ID, 'shop_catalog') . get_the_excerpt();'</li>'; endwhile; wp_reset_query(); echo '</ul>';endif;?> </div>
any suggestions apreciated :)
So your solution is :
<?php
$custom_loop = new WP_Query('showposts=5&category_name=Zanimljivosti&orderby=rand');
if ( $custom_loop->have_posts() ) :
echo '<ul>';
while ( $custom_loop->have_posts() ) :
$custom_loop->the_post(); echo '<li><h2><a href="' . get_permalink() . '">' . get_the_title();
echo '</h2>' . get_the_post_thumbnail($loop->post->ID, 'shop_catalog');
echo'<p>' . get_the_excerpt();'</p></a></li>';
endwhile; wp_reset_query();
echo '</ul>';endif;
?>

Wordpress next_posts_link() only reloads the same page

I'm having a problem with my Wordpress theme. My next_posts_link() only reloads the same page. Here's my code.
<div id="main">
<?php query_posts('category_name='.get_the_title().'&post_status=publish,future');?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="utdrag">
<h2><?php the_title(); ?></h2>
<div class="crop"><a href="<?php the_permalink(); ?>">
<?php
if ( get_the_post_thumbnail($post_id) != '' ) {
echo '<a href="'; the_permalink(); echo '" class="thumbnail-wrapper">';
the_post_thumbnail();
echo '</a>';
} else {
echo '<img src="';
echo catch_that_image();
echo '" alt="Image unavailable" class="crop" />';
echo '</a>';
}
?></a>
</div>
<?php the_excerpt(); ?>
</div>
<?php endwhile; else: endif; ?>
<?php next_posts_link();?><?php previous_posts_link();?>
</div>
I'm using a static page as my front page. As you can see it's only displaying posts that have the same category as the page title: query_posts('category_name='.get_the_title().'&post_status=publish,future');
This is something I want to keep. So does anyone know why it just reloads? Why it isn't changing to the next page?
I figured it out! I'm posting it here in case someone else is having the same problem!
I added this code at the top:
<?php if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
}
elseif ( get_query_var('page') ) {
$paged = get_query_var('page');
}
else { $paged = 1; }
?>
And replaced this:
<?php query_posts('category_name='.get_the_title().'&post_status=publish,future');?>
with this:
<?php query_posts('catery_name='.get_the_title().'&showposts=2'.'&paged='.$paged); ?>
For more information, check out this link:
http://wordpress.org/support/topic/front-page-wp-query-pagination-issue-repeating-posts-next_posts_link-help
These two functions does not work on static pages.
From the documentation for wp_next_post_link():
This function does not work with static pages.
However, take a look at this article. It talks about how to create a static front page with dynamic content.

Categories