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');
Related
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;?>
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 } ?>
i have an custom single post and show the product price and product tags and content , now i need show the product attribute in below of content but i test the many code , i can't show in my single post .
i need a code for echothe product attribute in single post .
this is my single.php
<?php get_header(); ?>
<div itemscope itemtype="http://schema.org/Product" id="single">
<div id="similar">
<h2><i class="icon-film"></i>similar product</h2>
<?php
$related = get_posts( array( 'category__in' => wp_get_post_categories($post->ID), 'post_type' => 'product' , 'orderby' => 'rand' , 'numberposts' => 8, 'post__not_in' => array($post->ID) ) );
if( $related ) foreach( $related as $post ) {
setup_postdata($post); ?>
<?php if ( has_post_thumbnail() ) {the_post_thumbnail('first-thumb');} else { ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<img src="<?php bloginfo('template_directory'); ?>/assets/img/no-image.png" alt="<?php the_title(); ?>" /><?php } ?></a>
<?php }
wp_reset_postdata(); ?>
<ul class="product_tags">
<h3><i class=" icon-uniE887"></i>product tag</h3>
<?php woocommerce_product_loop_tags() ?>
</ul>
</div>
<div id="single_body">
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div class="h1"><i class="icon-quill"></i><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
<h1><span itemprop="name">
<?php the_title(); ?>
</span></h1>
</a>
<ul class="title_icon">
<li title="view"><i class="icon-uniE850"></i><?php if(function_exists('the_views')) { the_views(); } ?></li>
<li title="comment"><i class="icon-bubbles2"></i><?php comments_popup_link('0 ','1 ', '% '); ?></li>
<li title="date"><i class="icon-clock"></i><?php the_time('Y/m/d') ?></li>
</ul>
</div>
<p><?php the_content()?></p>
<?php endwhile;endif; ?>
</div>
<ul class="product_etc">
<h3><i class="icon-uniE02A"></i>detail of productل</h3>
<li itemprop="offers" itemscope itemtype="http://schema.org/Offer"><strong>price</strong>
<div class="price" itemprop="price" ><?php echo $product->get_price_html(); ?></div></li>
<li><strong>category</strong><span><?php echo $product->get_categories( ', ', '' . _n( '', '', sizeof( get_the_terms( $post->ID, '' ) ), '' ) . ' ', '' ); ?></span></li>
<?php woocommerce_template_loop_add_to_cart( $loop->post, $product ); ?>
</ul>
<div class="clear"></div>
</div>
<div class="clear"></div>
<div class="comments-page"><?php comments_template(); ?></div>
<?php wp_reset_query(); ?>
<?php get_footer(); ?>
and this is my function.php
<?php
add_filter('show_admin_bar','__return_false');
function the_content_limit($max_char, $stripteaser = 0, $more_file = '') {
$content = get_the_content($more_link_text, $stripteaser, $more_file);
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
$content = strip_tags($content,'');
if (strlen($_GET['p']) > 0) {
echo "<p>";
echo $content;
echo "</p>";
}
else if ((strlen($content)>$max_char) && ($espacio = strpos($content, " ", $max_char ))) {
$content = mb_substr($content, 0, $espacio);
$content = $content;
echo "<p>";
echo $content;
echo "...";
echo "</p>";
}
else {
echo "<p>";
echo $content;
echo "</p>";
}
}
//thumbnails
if (function_exists('add_theme_support')) {
add_theme_support('post-thumbnails');
}
//comments
if ( $_GET['post_type'] != 'page' ) {add_post_meta($post_id, 'series', '', true);}
function mytheme_comment($comment, $args, $depth) {
$GLOBALS['comment'] = $comment;
extract($args, EXTR_SKIP);
if ( 'div' == $args['style'] ) {
$tag = 'div';
$add_below = 'comment';
} else {
$tag = 'li';
$add_below = 'div-comment';
}
?>
<<?php echo $tag ?> <?php comment_class( empty( $args['has_children'] ) ? '' : 'parent' ) ?> id="comment-<?php comment_ID() ?>">
<?php if ( 'div' != $args['style'] ) : ?>
<div class="reply">
<?php comment_reply_link( array_merge( $args, array( 'add_below' => $add_below, 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>
</div>
<div id="div-comment-<?php comment_ID() ?>" class="comment-body">
<?php endif; ?>
<div id="comments_data">
<?php printf( __( '<span class="">نظر</span> <cite class="fn">%s</cite>' ), get_comment_author_link() ); ?>
<?php if ( $comment->comment_approved == '0' ) : ?>
<em class="comment-awaiting-moderation"><?php _e( 'Your comment is awaiting moderation.' ); ?></em>
<br />
<?php endif; ?>
<div class="comment-meta commentmetadata"><a href="<?php echo htmlspecialchars( get_comment_link( $comment->comment_ID ) ); ?>">
<?php
/* translators: 1: date, 2: time */
printf( __('<span class="">در تاریخ: </span><br/> %1$s'), get_comment_date(), get_comment_time() ); ?></a><?php edit_comment_link( __( '(Edit)' ), ' ', '' );
?>
</div>
</div>
<?php comment_text(); ?>
<!---<div class="comment-author vcard">
<?php if ( $args['avatar_size'] != 0 ) echo get_avatar( $comment, $args['avatar_size'] ); ?>
</div>--->
<?php if ( 'div' != $args['style'] ) : ?>
</div>
<?php endif; ?>
<?php
}
//woocommerce
add_action( 'after_setup_theme', 'woocommerce_support' );
function woocommerce_support() {
add_theme_support( 'woocommerce' );
}
// Ensure cart contents update when products are added to the cart via AJAX (place the following in functions.php)
add_filter( 'woocommerce_add_to_cart_fragments', 'woocommerce_header_add_to_cart_fragment' );
function woocommerce_header_add_to_cart_fragment( $fragments ) {
ob_start();
?>
<a class="cart-contents" href="<?php echo WC()->cart->get_cart_url(); ?>"><?php echo sprintf (_n( '%d محصول', '%d محصول', WC()->cart->get_cart_contents_count() ), WC()->cart->get_cart_contents_count() ); ?> به ارزش <?php echo WC()->cart->get_cart_total(); ?></a>
<?php
$fragments['a.cart-contents'] = ob_get_clean();
return $fragments;
}
add_action( 'woocommerce_after_shop_loop_item', 'woocommerce_product_loop_tags', 5 );
function woocommerce_product_loop_tags() {
global $post, $product;
$tag_count = sizeof( get_the_terms( $post->ID, 'product_tag' ) );
echo $product->get_tags( ', ', '<span class="tagged_as">' . _n( '', '', $tag_count, 'woocommerce' ) . ' ', '.</span>' );
}
add_action( 'woocommerce_single_product_summary', 'product_attribute_after_price', 15 );
function product_attribute_after_price () {
global $product;
// HERE add your product attribute SLUG or taxonomy
$attribute_slug = 'color';
$taxonomy = strpos($url, 'blog') !== false ? $attribute_slug : 'pa_' . $attribute_slug;
$attribute_name = get_taxonomy( $taxonomy )->labels->singular_name;
$term_name = $product->get_attribute( $taxonomy ); // The value
if( empty($term_name) ) return; // Exit if empty value
// If not empty we display it
$output = '<div class="product-attribute '.$taxonomy.'"><p>';
$output .= '<strong> '.$attribute_name.'</strong>: ';
$output .= '<span> '.$term_name.'</span>';
echo $output . '</p></div>';
}
?>
To display product attributes, you will use this in any hooked function or Woocommerce templates:
// Get an instance of the product object
$_product = wc_get_product( get_the_id() );
if( $_product->has_attributes() ){
// Initializing
$attributes = array();
// Loop through product attributes
foreach( $_product->get_attributes() as $taxonomy => $attribute ){
// The product attribute label name
$attribute_name = get_taxonomy( $taxonomy )->labels->singular_name;
// Set each product attribute with its values in an array
$attributes[] = '<strong>'.$attribute_name.'</strong>: '.$_product->get_attribute($taxonomy);
}
// Display (output)
echo '<div class="product-attributes"><span>'. implode( '</span> <span>', $attributes ) . '</span></div>';
}
Here, get_the_id() can be replaced by a dynamic $product_id variable (which is the post ID of a product custom post type)
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');
I need to customise default design of related articles from jet pack in wordpress to my custom design i am unable to find the error where it was wrong in my code. I had written a hook in functions.php file.
This is my hook in functions.php
function jetpackme_custom_related() {
$posts = '<div class="single-article-popularGi">';
$posts .= '<h1>related</h1><div class="row">';
if ( class_exists( 'Jetpack_RelatedPosts' ) && method_exists( 'Jetpack_RelatedPosts', 'init_raw' ) ) {
$related = Jetpack_RelatedPosts::init_raw()
->get_for_post_id(
get_the_ID(),
array( 'size' => 2 )
);
if ( $related ) {
foreach ( $related as $result ) {
// Get the related post IDs
$title = get_the_title( $result[ 'id' ] );
$link = get_permalink( $result[ 'id' ] );
$image = featuredOrFirstImage($result[ 'id' ], 'blog-post-image');
$category_name = get_the_category( $result[ 'id' ] );
$posts .= '<div class="col-sm-3 col-3"><span class="thumbnail-image">'.$image.'</span></div>';
$posts .= '<div class="post-details col-sm-3 col-3 align-self-center"><div class="entry-cat post-category">'.$category_name.'</div>';
$posts .= '<div class="mostPopularTitle"><h2 class="entry-title"><a class="post-title" href="%s" rel="bookmark">'.$title.'</a></h2>';
$posts .= '<div class="readArticle"><a class="readMorePost" href="'.$link.'">MORE GI></a>';
}
}
}
$posts .= '</div>';
$posts .= '</div>';
// Return a list of post titles separated by commas
if( $related ){
return $posts;
}else{
return false;
}
}
add_action('admin_init', 'jetpackme_custom_related');
I had called it in single.php like this
<?php echo $related = jetpackme_custom_related();?>
But there is nothing displayed and getting error some times which is not displaying nothing. Can anyone Please let me solve it out..
I had changed the way of procedure and it works for me.My code is
$related_posts = array();
$query = array();
$query['showposts'] = 2;// Number of posts to show
if ( class_exists( 'Jetpack_RelatedPosts' ) && method_exists( 'Jetpack_RelatedPosts', 'init_raw' ) ) :
$related = Jetpack_RelatedPosts::init_raw()
->set_query_name( 'theme-custom' ) // optional, name can be anything
->get_for_post_id( get_the_ID(), array( 'size' => $query['showposts'] )
);
if ( $related ) :
foreach ( $related as $result ) :
$related_posts[] = $result[ 'id' ];
endforeach;
endif;
endif;
if ( $related_posts ) {
$query['post__in'] = $related_posts;
$query['orderby'] = 'post__in';
$title = __( 'Related', 'prefix' );
} else {
$query['post__not_in'] = array( $post->ID );
$title = __( 'Related', 'prefix' );
}
I had called the related articles like..
<div class="single-article-popularGi">
<h1><?php esc_attr_e( $title ); ?></h1>
<div class="row">
<?php $related = new WP_Query( $query ); ?>
<?php while ( $related->have_posts() ) : $related->the_post(); ?>
<div class="col-sm-3 col-3">
<div class="thumbnail-image">
<span class="thumb-wrap"><?php the_post_thumbnail('medium', array("class" => "img-fluid")); ?></span>
</div>
</div>
<div class="post-details col-sm-3 col-3 align-self-center">
<?php if ( has_category() ) : ?>
<div class="entry-cat post-category">
<?php the_category(' '); ?>
</div><!-- end .entry-cats -->
<?php endif; // has_category() ?>
<div class="mostPopularTitle">
<?php the_title( sprintf( '<h2 class="entry-title"><a class="post-title" href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a></h2>' ); ?>
</div>
<i class="postIntro"><?php echo $intro = wp_trim_words( get_field('intro' ), 20,'...' ); ?></i>
<div class="readArticle">
<a class="readMorePost" href="<?php echo get_permalink(); ?>">MORE GI></a>
</div>
</div>
<?php endwhile;
wp_reset_query(); ?>
</div>