How to sort entries by rubrics in Wordpress? - php

I do not know the wordpress very well, but I really need to understand the issue. The page displays a list of categories and at the bottom of the entry. When you click on a column, entries should be sorted, it works, but only the title is displayed, there is no link to the entry, and the picture and excerpt, date, segment.
enter image description here
<ul class="p-list">
<?php
if( $terms = get_terms( 'category', 'orderby=name' ) ) :
foreach ($terms as $term) :
echo '<li data-termid="'. $term->term_id .'">' . $term->name . '</li>';
endforeach;
endif;
?>
</ul>
<?php
query_posts( array(
'post_type' => 'post'
) );
if( have_posts() ){
while( have_posts() ){
the_post();
$i = get_the_post_thumbnail_url();
?>
<div class="project">
<div class="project-img" style="background: url('<?php echo $i;?>')"></div>
<span><?php the_title(); ?></span>
<?php the_excerpt(); ?>
<p class="post-date"><?php the_date('d/m/Y'); ?></p>
</div>
<?php
}
} else {
}
?>
here js code for sort
$('#filter .p-list li').click(function(){
var that = this;
var filter = $("#filter");
var datas = {
action: 'myfilter'
};
if($('section').is('.may.blog')){
datas['postcat'] = jQuery(that).data("termid");
}else{
datas['foliocat'] = jQuery(that).data("termid");
}
console.log(datas);
$.ajax({
url:filter.attr('action'),
data: datas,
type:filter.attr('method'),
beforeSend:function(xhr){
filter.find('#response').text('Loading...');
},
success:function(data){
$('#response').attr("data-curcat", jQuery(that).data("termid"));
$('#response').html(data);
}
});
and functions.php
function true_filter_function(){
// для таксономий
if(!empty($_POST['foliocat'])) :
$args['tax_query'][] = array(
'taxonomy' => 'folio_cat',
'field' => 'id',
'terms' => $_POST['foliocat']
);
endif;
if(!empty($_POST['postcat'])) :
$args['tax_query'][] = array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => $_POST['postcat']
);
endif;
$query = new WP_Query( $args );
if(!empty($_POST['foliocat'])) {
if( $query->have_posts() ) :
while( $query->have_posts() ): $query->the_post();
echo '<div class="portfolio"><div class="portfolio-img" style="background:url('.get_the_post_thumbnail_url().')"></div>';
echo '<p>'. get_field('folio_desc') .'</p>';
echo '<h4>'. get_the_title() .'</h4></div>';
endwhile;
wp_reset_postdata();
else :
echo 'No posts found';
endif;
}
if(!empty($_POST['postcat'])) {
if( $query->have_posts() ) :
while( $query->have_posts() ): $query->the_post(); ?>
<div class="project">
<div class="project-img"></div>
<p><?php the_title(); ?></p>
</div>
<?php endwhile;
wp_reset_postdata();
else :
echo 'No posts found';
endif;
}
echo '<div id="maxpagecat" data-maxpagecat="'. $query->max_num_pages .'"></div>';
die();
}
add_action('wp_ajax_myfilter', 'true_filter_function');
add_action('wp_ajax_nopriv_myfilter', 'true_filter_function');

Related

Woocommerce Product categories as tabs

I have writtern a function which shows my woocommerce product categories as tabs and related products as tab content.
`<?php
$product = wc_get_product( $post->ID );
if ( has_post_thumbnail( $product->id ) ) {
$attachment_ids[0] = get_post_thumbnail_id( $product->id );
$attachment = wp_get_attachment_image_src($attachment_ids[0], 'full' );
}
$categories = get_terms( array(
'taxonomy' => 'product_cat',
'hide_empty' => false,
) );
echo '<div class="tabs-container">';
echo '<ul class="tabs">';
foreach ( $categories as $category ) {
echo '<li class="tab">' . $category->name . '</li>';
}
echo '</ul>';
foreach ( $categories as $category ) {
$args = array(
'post_type' => 'product',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $category->slug,
),
),
);
$query = new WP_Query( $args );
echo '<div id="' . $category->slug . '" class="tab-content car">';
while ( $query->have_posts() ) {
$query->the_post();
echo '<div class="post-cont">';
if ( has_post_thumbnail( $product->id ) ) {
$attachment_ids[0] = get_post_thumbnail_id( $product->id );
$attachment = wp_get_attachment_image_src($attachment_ids[0], 'full' ); ?>
<img src="<?php echo $attachment[0] ; ?>" class="card-image" />
<?php } ?> <br>
<div class="post-details">
<div class="post-title">
<?php the_title(); ?>
</div>
<div class="shop-button-cont">
<div class="shop-button"><?php echo 'Shop Now'; ?></div>
<div class="sale-prise">
<?php echo $price = get_post_meta( get_the_ID(), '_sale_price', true);?>
</div>
<div class="reg-prise">
<?php echo $sale = get_post_meta( get_the_ID(), '_regular_price', true); ?>
</div>
</div>
</div><?php
echo '</div>';
}
echo '</div>';
}
echo '</div>';
wp_reset_postdata();
?>
<script>document.addEventListener("DOMContentLoaded", function() {
var tabs = document.querySelectorAll(".tabs li");
var tabContents = document.querySelectorAll(".tab-content");
tabs.forEach(function(tab) {
tab.addEventListener("click", function() {
var tabId = this.querySelector("a").getAttribute("href");
tabContents.forEach(function(content) {
content.style.display = "none";
});
document.querySelector(tabId).style.display = "flex";
tabs.forEach(function(tab) {
tab.classList.remove("active");
});
this.classList.add("active");
});
});
});</script>
`
but when we load the page all the tabs are closed, but i want to add a tab which shows all the product and is active by default when page is load.
is there any way to achive this? and if you want to change the whole fuction with a better function than it is also welcomed
Edit:
<?php
$product = wc_get_product( $post->ID );
if ( has_post_thumbnail( $product->id ) ) {
$attachment_ids[0] = get_post_thumbnail_id( $product->id );
$attachment = wp_get_attachment_image_src($attachment_ids[0], 'full' );
}
$categories = get_terms( array(
'taxonomy' => 'product_cat',
'hide_empty' => false,
) );
echo '<div class="tabs-container">';
echo '<ul class="tabs">';
echo '<li class="tab active">All</li>';
foreach ( $categories as $category ) {
echo '<li class="tab">' . $category->name . '</li>';
}
echo '</ul>';
echo '<div id="all" class="tab-content car">';?>
// Query and display all products here
<h1>Prodcuts</h1>
<div class="post-intro">Kuch Saste mai dikahu madam? <img src="https://images.all-free-download.com/images/graphicwebp/road_tested_208353.webp" alt=""></div>
<?php
// Set the number of posts to fetch and the offset (i.e. how many posts to skip)
$num_posts = 3;
$offset = 0;
$product = wc_get_product( $post->ID );
$price = get_post_meta( get_the_ID(), '_regular_price', true);
// $price will return regular price
$sale = get_post_meta( get_the_ID(), '_sale_price', true);
// $sale will return sale price
// Create a new instance of the WP_Query class
$posts = new WP_Query( array(
'post_type' => 'product', // Fetch only posts (not pages or other post types)
'posts_per_page' => $num_posts, // Set the number of posts to fetch
'offset' => $offset // Set the offset (how many posts to skip)
) );
// Check if the query has posts
if ( $posts->have_posts() ) {
// Loop through the posts and output their data
while ( $posts->have_posts() ) {
$posts->the_post(); ?>
<div class="post-cont">
<?php if ( has_post_thumbnail( $product->id ) ) {
$attachment_ids[0] = get_post_thumbnail_id( $product->id );
$attachment = wp_get_attachment_image_src($attachment_ids[0], 'full' ); ?>
<img src="<?php echo $attachment[0] ; ?>" class="card-image" />
<?php } ?><br>
<div class="post-details">
<div class="post-title">
<?php the_title(); ?>
</div>
<div class="shop-button-cont">
<div class="shop-button"><?php echo 'Shop Now'; ?></div>
<div class="sale-prise">
<?php echo $price = get_post_meta( get_the_ID(), '_sale_price', true);?>
</div>
<div class="reg-prise">
<?php echo $sale = get_post_meta( get_the_ID(), '_regular_price', true); ?>
</div>
</div>
</div>
</div>
<?php
}
} else {
}
// Reset the post data after the query
wp_reset_postdata();
?>
</div>
<?php echo '</div>';
foreach ( $categories as $category ) {
$args = array(
'post_type' => 'product',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $category->slug,
),
),
);
$query = new WP_Query( $args );
echo '<div id="' . $category->slug . '" class="tab-content car">';
while ( $query->have_posts() ) {
$query->the_post();
echo '<div class="post-cont">';
if ( has_post_thumbnail( $product->id ) ) {
$attachment_ids[0] = get_post_thumbnail_id( $product->id );
$attachment = wp_get_attachment_image_src($attachment_ids[0], 'full' ); ?>
<img src="<?php echo $attachment[0] ; ?>" class="card-image" />
<?php } ?> <br>
<div class="post-details">
<div class="post-title">
<?php the_title(); ?>
</div>
<div class="shop-button-cont">
<div class="shop-button"><?php echo 'Shop Now'; ?></div>
<div class="sale-prise">
<?php echo $price = get_post_meta( get_the_ID(), '_sale_price', true);?>
</div>
<div class="reg-prise">
<?php echo $sale = get_post_meta( get_the_ID(), '_regular_price', true); ?>
</div>
</div>
</div><?php
echo '</div>';
}
echo '</div>';
}
echo '</div>';
wp_reset_postdata();
?>
<script>
document.addEventListener("DOMContentLoaded", function() {
var tabs = document.querySelectorAll(".tabs li");
var tabContents = document.querySelectorAll(".tab-content");
tabs.forEach(function(tab) {
tab.addEventListener("click", function() {
var tabId = this.querySelector("a").getAttribute("href");
if (tabId === "#all") {
tabContents.forEach(function(content) {
content.style.display = "none";
});
} else {
tabContents.forEach(function(content) {
content.style.display = "none";
});
document.querySelector(tabId).style.display = "flex";
}
tabs.forEach(function(tab) {
tab.classList.remove("active");
});
this.classList.add("active");
});
});
});
</script>
You can add 'active' class in the PHP on one of the li tags.
echo '<div class="tabs-container">';
echo '<ul class="tabs">';
foreach ( $categories as $category ) {
$active_class = ( $category->slug === 'first-category' ) ? 'active' : '';
echo '<li class="tab ' . $active_class . '">' . $category->name . '</li>';
}
echo '</ul>';
Or you can use javascript to add the active class after the page is loaded.
// Get the list of tabs
var tabsList = document.querySelector('.tabs');
// Get the first tab
var firstTab = tabsList.firstElementChild;
// Add the "active" class to the first tab
firstTab.classList.add('active');

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;?>

How to use Isotope on wordpress with custom loops and custom design?

Does someone know if it's possible to make isotopte work with a custom grid layout ?
For example, I have a custom blog page design like this
In my loop, I say : if it's the first post, take 12 col width, if it's the second and third one, take 6 col width, and all others posts, take 4 col width..
And whatever filter button I click I would like to keep that design, for example the 4th post would become the first one if I click on the corresponding category filter:
But what I get is this
Isotope doesn't respect the loop condition..
Does someone know how to acheive this?
Ok finaly I managed to make it work; Here is the code. In my home page I first build filters with checkbox. Then in my loop, I count the posts to differentiate the post size with different column sizes.
get_header(); ?>
<div id="primary" class="content-area">
<main id="main" class="site-main">
<div class="flex-container">
<div class="flex-row">
<header class="page-header">
<?php
single_post_title('<h1 class="page-title">', '</h1>' );
?>
</header><!-- .page-header -->
</div>
<div class="flex-row">
<form action="#" method="POST" id="post_filters">
<p>Clear</p>
<?php
if( $terms = get_terms( array( 'taxonomy' => 'category' ) ) ) :
foreach( $terms as $term ) :
echo '<p><input type="radio" id="' . $term->term_id . '" value="' . $term->term_id . '" name="category_filters" class="category_filter"/><label for="' . $term->term_id. '">' . $term->name . '</label></p>';
endforeach;
endif;
?>
<!-- required hidden field for admin-ajax.php -->
<input type="hidden" name="action" value="ccfilter" />
</form>
</div>
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 5,
'paged' => $paged
);
$query = new WP_Query( $args ); ?>
<?php
if ( $query->have_posts() ) :
$count = (int)0;?>
<div id="cc_posts_wrap" class="flex-row">
<?php
while ( $query->have_posts() ) : $count++;
$query->the_post();
if($count == 1){
$span = 'flex-col-xs-12';
$limit = 20;
}
if($count == 2 || $count == 3){
$span = 'flex-col-sm-6';
$limit = 13;
}
if($count > 3){
$span = 'flex-col-sm-4';
$limit = 13;
}
//If its not 3 or higher, increase the count
$termsArray = get_the_terms($post->ID, "category"); //Get the terms for this particular item
$termsString =""; //initialize the string that will contain the terms
foreach ( $termsArray as $term ) { // for each term
$termsString .= $term->slug;
}
?>
<div class="<?php echo $termsString .' ' . $span ;?> item">
<article id="post-<?php the_ID(); ?>" <?php post_class();?>>
<div class="post-featured-thumbnail">
<?php
if ( has_post_thumbnail() ) {
if($count == 1){
the_post_thumbnail('blog_featured');
}else{
the_post_thumbnail();
}
}
if ( 'post' === get_post_type() ) :
?>
<div class="entry-meta">
<?php
numgrade_category_sticker();
?>
</div><!-- .entry-meta -->
<?php endif; ?>
</div>
<div class="post-content">
<header class="entry-header">
<?php
if ( is_singular() ) :
the_title( '<h1 class="entry-title">', '</h1>' );
else :
the_title( '<h2 class="entry-title">', '</h2>' );
endif;
?>
</header><!-- .entry-header -->
<div class="entry-content">
<?php echo excerpt($limit); ?>
</div><!-- .entry-content -->
</div>
</article><!-- #post-<?php the_ID(); ?> -->
</div>
<?php endwhile;?>
</div> <!-- end isotope-list -->
<?php global $wp_query; // you can remove this line if everything works for you
if ( $wp_query->max_num_pages > 1 ) :
echo '<div id="cc_loadmore">More posts</div>'; // you can use <a> as well
endif;
else :
get_template_part( 'template-parts/content', 'none' );
endif; ?>
</div>
</main><!-- #main -->
</div><!-- #primary -->
Then in functions.php, here are the admin ajax call, the load more function et the filtering function. Load more function et filtering function work well, and as we can see, I just duplicate the count loop in filtering post so it display my post as in the normal loop, with different size posts.
/*FUNCTION FILTER AND AJAX LOAD MORE*/
add_action( 'wp_enqueue_scripts', 'cc_script_and_styles');
function cc_script_and_styles() {
if ( is_home() ) {
global $wp_query;
wp_register_script( 'cc_scripts', get_stylesheet_directory_uri() . '/js/script.js', array('jquery') );
wp_localize_script( 'cc_scripts', 'cc_loadmore_params', array(
'ajaxurl' => site_url() . '/wp-admin/admin-ajax.php', // WordPress AJAX
'posts' => json_encode( $wp_query->query_vars ), // everything about your loop is here
'current_page' => $wp_query->query_vars['paged'] ? $wp_query->query_vars['paged'] : 1,
'max_page' => $wp_query->max_num_pages
) );
wp_enqueue_script( 'cc_scripts' );
}
}
add_action('wp_ajax_loadmorebutton', 'cc_loadmore_ajax_handler');
add_action('wp_ajax_nopriv_loadmorebutton', 'cc_loadmore_ajax_handler');
function cc_loadmore_ajax_handler(){
$params = json_decode( stripslashes( $_POST['query'] ), true );
$params['paged'] = $_POST['page'] + 1;
$params['post_status'] = 'publish';
query_posts( $params );
if( have_posts() ) :
while( have_posts() ): the_post();
$termsArray = get_the_terms($post->ID, "category");
$termsString ="";
foreach ( $termsArray as $term ) {
$termsString .= $term->slug;
}
?>
<div class="<?php echo $termsString ;?> flex-col-sm-4 item">
<article id="post-<?php the_ID(); ?>" <?php post_class();?>>
<div class="post-featured-thumbnail">
<?php
if ( has_post_thumbnail() ) {
if($count == 1){
the_post_thumbnail('blog_featured');
}else{
the_post_thumbnail();
}
}
;
if ( 'post' === get_post_type() ) :
?>
<div class="entry-meta">
<?php
numgrade_category_sticker();
?>
</div><!-- .entry-meta -->
<?php endif; ?>
</div>
<div class="post-content">
<header class="entry-header">
<?php
if ( is_singular() ) :
the_title( '<h1 class="entry-title">', '</h1>' );
else :
the_title( '<h2 class="entry-title">', '</h2>' );
endif;
?>
</header><!-- .entry-header -->
<div class="entry-content">
<?php echo excerpt($limit); ?>
</div><!-- .entry-content -->
</div>
</article><!-- #post-<?php the_ID(); ?> -->
</div>
<?php
endwhile;
wp_reset_postdata();
endif;
die;
}
add_action('wp_ajax_ccfilter', 'cc_filter_function');
add_action('wp_ajax_nopriv_ccfilter', 'cc_filter_function');
function cc_filter_function(){
if( isset( $_POST['category_filters'] ) )
$args['tax_query'] = array(
array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => $_POST['category_filters'],
"posts_per_page" => 5
)
);
query_posts( $args );
global $wp_query;
if( have_posts() ) : $count = (int)0;
ob_start();
while( have_posts() ): $count++;
the_post();
if($count == 1){
$span = 'flex-col-xs-12';
$limit = 20;
}
if($count == 2 || $count == 3){
$span = 'flex-col-sm-6';
$limit = 13;
}
if($count > 3){
$span = 'flex-col-sm-4';
$limit = 13;
}
$termsArray = get_the_terms($post->ID, "category");
$termsString ="";
foreach ( $termsArray as $term ) {
$termsString .= $term->slug;
}
?>
<div class="<?php echo $termsString .' ' . $span ;?> item">
<article id="post-<?php the_ID(); ?>" <?php post_class();?>>
<div class="post-featured-thumbnail">
<?php
if ( has_post_thumbnail() ) {
if($count == 1){
the_post_thumbnail('blog_featured');
}else{
the_post_thumbnail();
}
}
;
if ( 'post' === get_post_type() ) :
?>
<div class="entry-meta">
<?php
numgrade_category_sticker();
?>
</div><!-- .entry-meta -->
<?php endif; ?>
</div>
<div class="post-content">
<header class="entry-header">
<?php
if ( is_singular() ) :
the_title( '<h1 class="entry-title">', '</h1>' );
else :
the_title( '<h2 class="entry-title">', '</h2>' );
endif;
?>
</header><!-- .entry-header -->
<div class="entry-content">
<?php echo excerpt($limit); ?>
</div><!-- .entry-content -->
</div>
</article><!-- #post-<?php the_ID(); ?> -->
</div>
<?php
endwhile;
wp_reset_postdata();
$posts_html = ob_get_contents();
ob_end_clean();
else:
$posts_html = '<p>Nothing found for your criteria.</p>';
endif;
echo json_encode( array(
'posts' => json_encode( $wp_query->query_vars ),
'max_page' => $wp_query->max_num_pages,
'found_posts' => $wp_query->found_posts,
'content' => $posts_html
) );
die();
}
Then finaly, here is the js code in a separate js file
jQuery(function($){
/* LOAD MORE FUNCTION ON FORMATION ARCHIVE PAGE */
$('#cc_loadmore').click(function(){
$.ajax({
url : cc_loadmore_params.ajaxurl, // AJAX handler
data : {
'action': 'loadmorebutton', // the parameter for admin-ajax.php
'query': cc_loadmore_params.posts, // loop parameters passed by wp_localize_script()
'page' : cc_loadmore_params.current_page // current page
},
type : 'POST',
beforeSend : function ( xhr ) {
$('#cc_loadmore').text('Loading...'); // some type of preloader
},
success : function( posts ){
if( posts ) {
$('#cc_loadmore').text( 'More posts' );
$('#cc_posts_wrap').append( posts ); // insert new posts
cc_loadmore_params.current_page++;
if ( cc_loadmore_params.current_page == cc_loadmore_params.max_page )
$('#cc_loadmore').hide(); // if last page, HIDE the button
} else {
$('#cc_loadmore').hide(); // if no data, HIDE the button as well
}
}
});
return false;
});
/* FILTERING FUNCTION ON FORMATION ARCHIVE PAGE */
$('#post_filters').change(function(){
$.ajax({
url : cc_loadmore_params.ajaxurl,
data : $('#post_filters').serialize(), // form data
dataType : 'json', // this data type allows us to receive objects from the server
type : 'POST',
success : function( data ){
// when filter applied:
// set the current page to 1
cc_loadmore_params.current_page = 1;
// set the new query parameters
cc_loadmore_params.posts = data.posts;
// set the new max page parameter
cc_loadmore_params.max_page = data.max_page;
// change the button label back
// insert the posts to the container
$('#cc_posts_wrap').html(data.content);
// hide load more button, if there are not enough posts for the second page
if ( data.max_page < 2 ) {
$('#cc_loadmore').hide();
} else {
$('#cc_loadmore').show();
}
}
});
// do not submit the form
return false;
});
});

wordpress has_post_thumbnail if/else statement not working

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 } ?>

Get posts all posts by Tag / URL Wordpress

I'm looking to create a tag.php page, which displays all tags when the user clicks on the tag from a tag cloud. This is what I've got so far, but this seems to display all of my posts.
<article class="articles">
<?php
echo '<h2>Tag:';
$tag = single_tag_title();
echo '</h2>';
$args = array(
'taxonomy' => $tag,
'terms' => $tag,
);
$postslist = get_posts( $args );?>
<?php foreach( $postslist as $post ) : setup_postdata($post); ?>
<div class="clear"></div>
<span class="timestamp"><?php echo mysql2date('j M Y', $post->post_date) ;?></span></h2>
<p class="about"><?php the_title(); ?></p>
<?php the_content(''); ?>
<?php endforeach;?>
</div>
I cannot seem to figure this out, I've been googling it but I can't seem to find out the information I want...
Your single_tag_title it's not returning to variable:
$tag = single_tag_title('', false);
try with:
<?php
$tag = single_tag_title('', false);
echo '<h2>Tag: '.$tag.'</h2>';
$args = array(
'taxonomy' => $tag,
'terms' => $tag,
);
$postslist = get_posts( $args );?>
<?php foreach( $postslist as $post ) : setup_postdata($post); ?>
<div class="clear"></div>
<span class="timestamp"><?php echo mysql2date('j M Y', $post->post_date) ;?></span></h2>
<p class="about"><?php the_title(); ?></p>
<?php the_content(); ?>
</div>
<?php endforeach;?>
Try using wp_query
$tag = single_tag_title('', false);
echo '<h2>Tag: '.$tag.'</h2>';
// The Query
$the_query = new WP_Query( $tag );
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
$post = get_queried_object();
echo "<div class='clear'></div>
<span class='timestamp'>" . mysql2date('j M Y', $post->post_date) . " </span></h2>
<p class='about'" . the_title() . "</p>";
<?php the_content(); ?>
</div>
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
Please note, I haven't tested this code!

Categories