wordpress has_post_thumbnail if/else statement not working - php

I have the following code, it seems to ignore the if/else for the displaying the thumbnail. It will display all thumbnails should a post have a thumbnail set however it will not show the placeholder if there is no thumbnail. Am I blind or is there something wrong with this?
<?php $x = 0;
$displayed = [];
function runQuery($args) {
global $displayed;
global $x;
$query = new WP_Query( $args );
if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post();
$cat_terms = get_the_terms($post->id, 'product_cat');
$datagroups = '';
if ( in_array( get_the_ID(), $displayed ) ){
continue;
}
// update array with currently displayed post ID
$displayed[] = get_the_ID();
foreach ($cat_terms as $key => $cat) {
if (count($cat_terms) == ($key + 1)) {
$datagroups .= '"' . $cat->name . '"';
} else {
$datagroups .= '"' . $cat->name . '", ';
}
}
?>
<div class="flex-item product-single flex-cols-4" data-groups='[<?php echo $datagroups; ?>]' data-date-created="<?php the_modified_date('Y-m-d') ?>" data-title="<?php the_title() ?>">
<figure class="picture-item__inner">
<figcaption class="picture-item__title">
<a data-open="product-<?php echo $x; ?>">
<div class="product-wrap">
<div class="product-image">
<?php if( has_post_thumbnail() ) { ?>
<?php the_post_thumbnail('medium');?>
<?php } else { ?>
<img src="<?php echo get_template_directory_uri(); ?>/armco-old/assets/img/products/placeholder.jpg" alt="Coming Soon" />
<?php } ?>
</div>
<div class="product-title">
<p><?php the_title(); ?></p>
</div>
</div>
</a>
</figcaption>
</figure>
</div>
<?php $x ++;
endwhile;
endif;
wp_reset_postdata();
}?>
<?php if ( $terms && !is_wp_error( $terms ) ) {
foreach ( $terms as $term ) {
$args = array(
'post_type' => 'products',
'posts_per_page' => -1,
'orderby' => 'menu_order',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $term->slug,
),
),
'order' => 'asc',
);
runQuery($args);
}
} ?>
</div>
Any help would be appreciated as I cannot see what the issue is, when I remove the first if statement and just tell it to echo out the image it works with brining in the image however as soon as it's inside the else statement it doesn't work.

Due to whatever reasoning my original code did not work, so I swapped it to check if the get_the_post_thumbnail() function was empty or not:
<?php if( !empty(get_the_post_thumbnail()) ) { ?>
<?php the_post_thumbnail('medium');?>
<?php } else { ?>
<img src="<?php echo get_template_directory_uri(); ?>/armco-old/assets/img/products/placeholder.jpg" alt="Coming Soon" />
<?php } ?>

Related

change the category from article when the date is expire with wordpress

i have a problem the category does not change
I want to change the category from open to interviewing of an article when the date is expired.
I have added a custom field on my posts called closing_date, when the current date passes the closing date the category changes
this is my code
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<?php
global $post;
$args = array(
'post_type' => 'openings',
'posts_per_page' => 40,
'meta_key' => 'closing_date',
'orderby' => 'closing_date',
'order' => "DESC",
'tax_query' => array(
'taxonomy' => 'openings_status',
'field' => 'slug',
'terms' => array('open, interviewing, filled'),
),
);
$groupedPosts = get_posts( $args );
foreach($groupedPosts as $post) : setup_postdata($post);
$closing_date = get_field('closing_date');
$Status = get_the_terms($post->ID, 'openings_status' );
//get current date
//convert expire into a date obj
$closing_date = new DateTime($closing_date);
//get current date
$today = new DateTime();
//convert dates to seconds for easier comparison
$closing_date_secs = $closing_date->format('U');
$today_secs = $today->format('U');
if ($closing_date_secs < $today_secs ) :
$Status = 'Interviewing';
//featured set to false
//update_post_meta($post->ID, 'Interviewing', $Status );
// set back to empty
update_post_meta($post->ID, 'closing_date', '' );
?>
<li data-filter="<?php $term = get_the_terms( $post->ID, 'openings_status' ); if ( !empty( $term ) ) { foreach ($term as $t) { echo $t->slug; } } ?>" class="grid-item">
<figure>
<?php if ( has_post_thumbnail() ) : ?><a class="img" href="<?php the_permalink(); ?>"><?php the_post_thumbnail('cp_thumb', array()); ?></a><?php endif; ?>
<figcaption>
<h5 title="<?php the_title(); ?>"><?php the_title(); ?></h5>
<?php if( get_post_meta($post->ID, 'company_name', true) ) { ?><h6><?php if( get_post_meta($post->ID, 'openings_url', true) ) { ?><a target="_blank" href="<?php the_field('openings_url'); ?>"><?php } ?><?php the_field('company_name'); ?><?php if( get_post_meta($post->ID, 'openings_url', true) ) { ?></a><?php } ?></h6><?php } ?>
<?php the_excerpt(); ?>
<p class="more">Learn More</p>
</figcaption>
<ul>
<?php $term = get_the_terms( $post->ID, 'openings_status' );
if ( !empty( $term ) ) { ?>
<li class="status">
<?php foreach ($term as $t) { ?>
<span class="<?php echo $t->slug; ?>"><?php echo $t->name; ?></span>
<?php } ?>
</li>
<?php } ?>
<?php if( get_post_meta($post->ID, 'location', true) ) { ?><li class="location"><?php the_field('location'); ?></li><?php } ?>
<li class="share pdf">
<?php echo do_shortcode('[addtoany]') ?>
</li>
<!-- <?php // if( get_post_meta($post->ID, 'openings_pdf', true) ) { ?> -->
<li class="download-2">
<?php echo do_shortcode('[WORDPRESS_PDF]'); ?>
<p>Download as a PDF
<?php if( function_exists( 'mpdf_pdfbutton' ) ) {
mpdf_pdfbutton( false, 'my link', 'my login text' );
} ?>
</p>
</li>
</ul>
</figure>
</li>
<?php endif;
endforeach;?>

WP_Query posts get term list outside of loop?

Maybe I'm going about this incorrectly but I'm having an issue with getting info outside of the while loop:
<?php
$title = get_field('car_list_title');
$field = get_field('tax_field_selector');
$query = new WP_Query( array(
'post_type' => 'cars',
'taxonomy' =>'make',
'term' => $field->name,
'posts_per_page' => -1,
'orderby' =>'title',
'order' =>'ASC'
) );
$taxonomy = get_terms( array(
'taxonomy' => 'location',
'hide_empty' => true
) );
if ( $field || $query->have_posts() ) :
?>
<div class="c-cars">
<h2 class="c-cars_title u-t--underline--lightblue">
<?= $title; ?>
</h2>
<?php foreach( $taxonomy as $tax ) :
$tax_name = $tax->name;
?>
<div class="c-cars_row">
<h4 class="c-cars_location-title">
<?= $tax_name; ?>
</h4>
<div class="c-cars_cars">
<?php while ( $query->have_posts() ) : $query->the_post();
$title = get_the_title();
$link = get_permalink();
$image = get_field('car-picture');
$image_alt = get_field('car_alt');
$image_title = get_field('car_title');
$post_id = get_the_ID();
$terms = get_the_terms( $post_id, 'location', array( 'order' => 'DESC', 'hide_empty' => true));
$location = $terms[0]->name;
?>
<?php if( $location === $tax_name ) : ?>
<div class="c-cars_car">
<a href="<?= $link; ?>">
<img class="c-cars_car-image" src="<?= $image; ?>" alt="<?= $image_alt; ?>" title="<?= $image_title; ?>">
</a>
<h4 class="text-center">
<a href="<?= $link; ?>">
<?= $title; ?>
</a>
</h4>
</div>
<?php endif; ?>
<?php endwhile; wp_reset_postdata(); ?>
</div>
</div>
<?php endforeach; ?>
</div>
<?php endif; ?>
So what happens here is I get a list of the locations and all the cars in those locations:
Location 1:
Car
Car
Car
Location 2:
Car
Car
Car
Location 3:
Location 4:
Car
Car
Car
The problem here is, as an example, Location 3 shows up even though there's no "posts" in that term.
The while loop is only cars of a specific model, sorted into what location they are at.
I'm not really sure how to filter out the empty locations.
I do:
<?php if( $location === $tax_name ) : ?>
Inside of the loop and that filters them out of the locations but still leaves the location title because it's outside of the while loop. If I were able to do this earlier up in the code it may work but I can't get the list of active terms outside of the while loop.
I'm kind of lost right now. Any ideas or suggestions? Thanks!
you can check using condition if has a term so show title either it will be blank try it below condition and mentioned in a comment if it's work or not.
has_terms
https://developer.wordpress.org/reference/functions/has_term/
function check if the post has terms.
if( has_term( $location, $tax_name ) ) {
// do something
}
Okay, I have updated your answer please try it below code. I have just get post count of terms and applied condition if terms has post count so show terms title name or if there is no post count of terms so the title will show blank.
<?php
$title = get_field('car_list_title');
$field = get_field('tax_field_selector');
$query = new WP_Query( array(
'post_type' => 'cars',
'taxonomy' =>'make',
'term' => $field->name,
'posts_per_page' => -1,
'orderby' =>'title',
'order' =>'ASC'
) );
$taxonomy = get_terms( array(
'taxonomy' => 'location',
'hide_empty' => true
) );
if ( $field || $query->have_posts() ) :
?>
<div class="c-cars">
<h2 class="c-cars_title u-t--underline--lightblue">
<?= $title; ?>
</h2>
<?php foreach( $taxonomy as $tax ) :
$tax_name = $tax->name;
$tax_post_count = $tax->count;
?>
<div class="c-cars_row">
if ( $tax_post_count > 0 ) : ?>
<h4 class="c-cars_location-title">
<?= $tax_name; ?>
</h4> <?php
else : ?>
<h4 class="c-cars_location-title">
<?= $tax_name = ''; ?>
</h4> <?php
endif; ?>
<div class="c-cars_cars">
<?php while ( $query->have_posts() ) : $query->the_post();
$title = get_the_title();
$link = get_permalink();
$image = get_field('car-picture');
$image_alt = get_field('car_alt');
$image_title = get_field('car_title');
$post_id = get_the_ID();
$terms = get_the_terms( $post_id, 'location', array( 'order' => 'DESC', 'hide_empty' => true));
$location = $terms[0]->name;
?>
<?php if( $location === $tax_name ) : ?>
<div class="c-cars_car">
<a href="<?= $link; ?>">
<img class="c-cars_car-image" src="<?= $image; ?>" alt="<?= $image_alt; ?>" title="<?= $image_title; ?>">
</a>
<h4 class="text-center">
<a href="<?= $link; ?>">
<?= $title; ?>
</a>
</h4>
</div>
<?php endif; ?>
<?php endwhile; wp_reset_postdata(); ?>
</div>
</div>
<?php endforeach; ?>
</div>
<?php endif; ?>

Wordpress Sort Posts / Custom Post Type

I have this custom loop and for some reason the "Sort" isn't working.
<?php
if(isset($_REQUEST['sort'])){
if($_REQUEST['sort'] == 'newest' )
$order = "&orderby=title&order=DESC";
else if($_REQUEST['sort'] == 'oldest' )
$order = "&orderby=title&order=ASC";
else if($_REQUEST['views'] == 'oldest' )
$order = "&meta_key=views&orderby=meta_value_num&order=DESC";
}
else
$order = "&orderby=ID&order=DESC";
?>
<form method="post" id="order">
<select name="sort" onchange='this.form.submit()'>
<option value="newest">Sort by Newest</option>
<option value="oldest">Sort by Oldest</option>
<option value="views">Sort by Most Viewed</option>
</select>
</form>
<ul class="acapellas row">
<?php
$loop = new WP_Query( array(
'post_type' => 'acapella',
'posts_per_page' => 10,
'paged' => $paged,
'orderby' => 'date',
'order' => $_POST['sort']
) );
?>
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; ?>
<?php if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php $posts = query_posts($query_string . $order); ?>
<li class="post-<?php the_ID(); ?> col-md-6">
<div class="wrap">
<h2><?php the_title() ?></h2>
<?php if(pmpro_hasMembershipLevel($level_id)) { ?>
<?php the_content(); ?>
<?php } else { ?>
<div class="pro-player">
<div class="upgrade">
<a href="<?php bloginfo('url'); ?>/pro" >Upgrade to unlock</a>
</div>
</div>
<?php } ?>
<a class="download left" href="<?php the_permalink(); ?>">Download</a>
<span class="list-date right">First added: <?php the_time('F jS, Y') ?></span><br>
<?php
global $post;
$post_type = get_post_type(get_the_ID());
$post_type_taxonomies = get_object_taxonomies($post_type);
if (!empty($post_type_taxonomies)) {
echo '<ul class="details">';
foreach ($post_type_taxonomies as $taxonomy) {
$terms = get_the_term_list(get_the_ID(), $taxonomy, '', '</li><li>', '');
if ($terms) {
echo '<li>' . $terms . '</li>';
}
}
echo '</ul>';
}
?>
</div>
</li>
<?php endwhile; endif; ?>
</ul>
You can use function get_posts().
$posts = get_posts([
'orderby' => 'date',
'order' => $_POST['sort']
]);
And send post variable 'ASC' or 'DESC' - that will be the newest or oldest

Capitalise portfolio category string in Wordpress

I'm trying to capitalise the Wordpress portfolio category string of "Corporate identity".
I have tried using this post: Capitalise every word of a string in PHP?, but have had no luck.
URL: http://utopia.gerandeklerk.com/
On the home page, hover over the first portfolio item (Steam Africa 2014), you will notice on the hover-state it lists the portfolio categories "Collateral" and "Corporate identity". The client wants wants it to display "Corporate Identity", as it does in the category filter menu above it.
I added the following string - can someone tell me where I went wrong?
$portfolio_category[] = ucwords(strtolower($term->slug)); // Capitalise Portfolio Category String
Below is the code from portfolio.php
<div class="filter-container">
<ul data-drop-content class="filter-list">
<li>All</li>
<?php
$terms = get_terms("portfolio_category");
foreach ( $terms as $term ) {
echo "<li><a href='#' data-filter='" . "." . strtolower($term->slug) . "'>" . $term->name . "</a></li>";
};
?>
</ul>
<span>| More filters</span> <img src="<?php echo get_template_directory_uri(); ?>/images/down-arrow.png">
<?php
$desired_width = 440;
$desired_height = 440;
if(is_tax()) { // is category page
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
$args = array( 'post_type' => 'portfolio', 'portfolio_category' => $term -> slug, 'posts_per_page' => -1, 'orderby' => 'menu_order', 'order'=>'ASC' );
}
else { // is main portfolio page
$args = array( 'post_type' => 'portfolio', 'posts_per_page' => -1, 'orderby' => 'menu_order','order'=>'ASC' );
}
$loop = new WP_Query( $args );
if($loop->have_posts()) {
?>
<div class="portfolio-gallery group isotope">
<?php
//output the latest projects from the 'my_portfolio' custom post type
while ($loop->have_posts()) : $loop->the_post();
?>
<?php
$terms = get_the_terms( $post->ID, 'portfolio_category' );
if ( $terms && ! is_wp_error( $terms ) ) {
$portfolio_category = array();
foreach ( $terms as $term ) {
$portfolio_category[] = ucwords(strtolower($term->slug)); // Capitalise Portfolio Category String
}
$the_tax = join( " ", $portfolio_category );
} else {
$the_tax = "";
};
?>
<div class="portfolio-item <?php echo $the_tax; ?> portfolio-item-<?php the_ID(); ?>">
<a class="project-link" href="<?php the_permalink(); ?>">
<div class="thumb-container">
<div class="portfolio-thumb">
<?php if( get_field('gray_scale_image') ): ?>
<img src="<?php the_field('gray_scale_image'); ?>" class="grey-scale-image" />
<?php endif; ?>
<?php if( get_field('gray_scale_image') ): ?>
<img src="<?php the_field('colour_image'); ?>" class="colour-image" />
<?php endif; ?>
</div>
</div><!-- /.thumb-container -->
</a><!-- /.project-link -->
<h3 class="portfolio-category"><?php echo str_replace('-',' ',ucwords(join(", ", explode(" ", $the_tax))) ); ?></h3>
<h3 class="portfolio-title"><?php the_title(); ?></h3>
</div><!-- /.portfolio-item -->
<?php endwhile; ?>
</div><!-- .portfolio-gallery -->
<?php } // end if ?>
It goes wrong on this line
<h3 class="portfolio-category"><?php echo str_replace('-',' ',ucwords(join(", ", explode(" ", $the_tax))) ); ?></h3>
On this line you can try the following function as you mentioned earlier
$bar = ucwords(strtolower($bar)); // Hello World!
I Didn't tested the code

if category empty return a message

i have this code to get some elements from wordpress
global $post;
$i=0;
$args = array( 'numberposts' => 5, 'category' =>5,'order'=>'DESC','orderby'=>'post_date','suppress_filters' => 0 );
$myposts = get_posts( $args );
$has_posts = true;
foreach( $myposts as $post ) : setup_postdata($post); ?>
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
<li>
<?php if($image){ ?>
<div class="news_left"><img src="<?php echo $image[0]; ?>" alt="" width="191" height="132" /></div>
<?php } ?>
<?php
$content = apply_filters('the_content', get_the_content());
$content = explode("</p>", $content);
?>
<div class="news_right">
<h2><?php the_title(); ?></h2>
<span class="date">Date: <?php the_time('j/m/Y') ?></span>
<?php echo $content[1] . "</p>";//echo String::content_limit(200,'<p>'); ?>
Read More
</div>
<div class="clear"></div>
</li>
<?php $i++; endforeach; ?>
i need to put a condition to return text message if category is empty.like no posts to display , please note that the post use WPML as the language switcher
<?php
$myposts = get_posts( $args );
if($myposts){
//found posts
}else{
//no posts
}
?>
UPDATE: PLEASE check if the code works probably and THEN compare it with your code, i have commented on the changes i made, so its your chance to learn:
$i=0;
$args = array( 'numberposts' => 5, 'category' =>5,'order'=>'DESC','orderby'=>'post_date','suppress_filters' => 0 );
$myposts = get_posts( $args );
//check if $myposts
if(!$myposts){
//the $myposts has no posts, print the error message
echo "<li>";
echo "This category has zero posts";
echo "</li>";
}else{
//the category has one more or more posts
$has_posts = true;
foreach( $myposts as $post ) : setup_postdata($post); ?>
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
<li>
<?php if($image){ ?>
<div class="news_left"><img src="<?php echo $image[0]; ?>" alt="" width="191" height="132" /></div>
<?php } ?>
<?php
$content = apply_filters('the_content', get_the_content());
$content = explode("</p>", $content);
?>
<div class="news_right">
<h2><?php the_title(); ?></h2>
<span class="date">Date: <?php the_time('j/m/Y') ?></span>
<?php echo $content[1] . "</p>";//echo String::content_limit(200,'<p>'); ?>
Read More
</div>
<div class="clear"></div>
</li>
<?php
$i++; endforeach;
}
?>
Thank you so much!! This solved my problem..
ref: howlingwolfmedia.com/site3/classes/class-categories
see 'Personalized Training'
my code here:
`
<?php
$args=array(
'child_of' => 4,
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => '0'
);
$categories=get_categories($args);
foreach($categories as $category) {
echo '<h3>' . $category->name . '</h3>
<div>';
global $post;
$args = array( 'posts_per_page' => -1, 'category' => $category->term_id, 'orderby' => 'name', 'order' => 'ASC' );
//alternatively this also works: 'nopaging' => true
$myposts = get_posts( $args );
if (!$myposts) {
echo 'Sorry there are currently no classes in this category';
}
else {
foreach( $myposts as $post ) : setup_postdata($post);
echo '<p>' . get_the_title($post->ID) . '</p>';
endforeach;
}
echo '</div>
';
}
?>
`

Categories