ACF Checkbox loop not displaying all posts - wordpress

I've created a shortcode that displays a loop, in which it SHOULD display all posts in 'products', that have the checkbox selected as value 'yes'. However, it's not displaying all the posts, it's only displaying one.
Here's my code:
function featured_products() {
$args = array(
'posts_per_page' => -1,
'post_type' => 'products',
'meta_query' => array(
array(
'key' => 'home_featured',
'value' => '1',
'compare' => 'LIKE'
),
),
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
$output = '<p>' . get_the_title() . '</p>';
}
echo '</ul>';
wp_reset_postdata();
} else {
$output = "<p>There aren't any products to display.</p>";
}
return $output;
}
add_shortcode('featured_products', 'featured_products');
What am I doing wrong here in terms of displaying more than one post?
This is what my custom field setup looks like:
Now I'm having trouble with the output. For some reason, the loop is displaying right at the top of the page's content container:
function featured_products() {
$args = array(
'posts_per_page' => 10,
'post_type' => 'products',
'meta_query' => array(
array(
'key' => 'home_featured',
'value' => '1',
'compare' => 'LIKE'
),
),
);
$output .= '<div class="products">';
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
$output .= '<div class="col-eq-height col-md-6" style="margin-bottom:30px;">';
$output .= '<div class="product">';
$output .= '<div id="thumbnail"><a href="' . get_the_permalink() . '">';
$output .= get_the_post_thumbnail();
$output .= '</a></div>';
$output .= '<div id="content">';
$output .= '<p id="title">' . get_the_title() . '</p>';
echo '<p>' . the_field('product_price_exc_vat') . '</p>';
$output .= '</div>';
$output .= '</div>';
$output .= '</div>';
}
wp_reset_postdata();
} else {
$output .= "<p>There aren't any products to display.</p>";
}
$output .= '</div>';
return $output;
}
add_shortcode('featured_products', 'featured_products');

The problem here is, that $output is being overwritten every time it iterates.
Meaning that you will only get the last title.
putting a dot infront of the = sign, will gather it instead of overwriting it.
So to sum up -
Initiate
$output ="";
Outside the loop, and then push it together inside your while
$output .="whateverHtml".$variable."whateverHtml";

Related

How To Filter and Display ONLY Simple WooCommerce Products using Shortcode

I have a functional shortcode that outputs all products. Problem is, I need this to only "find" simple products.
add_shortcode('product_list', 'simple_product_list');
function simple_list_shortcode() {
$query = new WP_Query( array(
'posts_per_page' => -1,
'post_type' => 'product',
'post_status' => 'publish',
'hide_empty' => 0,
'orderby' => 'title',
'post_id' => 'id'));
$output = '<ul style="list-style-type:none">';
while ( $query->have_posts() ) : $query->the_post();
$product = wc_get_product($query->post->ID);
$price = $product->get_regular_price();
$output .= '<li>' . $query->post->post_title . ' costs '.wc_price($price) . ', <a class="atc" href="'. $product->add_to_cart_url() .'">order now</a>.</li>';
endwhile;
wp_reset_postdata();
return $output.'</ul>';
}
Since it is already using 'post_type' => 'product', I cannot add another with simple.
I tried using a if statement for ->is_type('simple'), but doing so turned the page white and nothing is displayed.
Your shortcode callback function and your actual function name is different.
You can get product type using $product->get_type() function.
UPDATE 1: Using WP_Query().
add_shortcode('product_list', 'simple_list_shortcode');
function simple_list_shortcode()
{
ob_start();
$query = new WP_Query(array(
'posts_per_page' => -1,
'post_type' => 'product',
'post_status' => 'publish',
'hide_empty' => 0,
'orderby' => 'title',
'post_id' => 'id'
));
$output = '<ul style="list-style-type:none">';
while ($query->have_posts()) : $query->the_post();
$product = wc_get_product($query->post->ID);
if($product->get_type() == 'simple'){
$price = $product->get_regular_price();
$output .= '<li>' . $query->post->post_title . ' costs ' . wc_price($price) . ', <a class="atc" href="' . $product->add_to_cart_url() . '">order now</a>.</li>';
}
endwhile;
wp_reset_postdata();
echo $output . '</ul>';
$contents = ob_get_clean();
return $contents;
}
UPDATE 2: you can also use wc_get_products() function to get products with arguments.
add_shortcode('product_list', 'simple_product_list');
function simple_product_list()
{
$arg = array(
'type' => 'simple',
'orderby' => 'date',
'order' => 'DESC',
) ;
$products = wc_get_products($arg);
ob_start();
$output = '<ul style="list-style-type:none">';
foreach( $products as $product){
$price = $product->get_regular_price();
$output .= '<li>' . $product->get_name() . ' costs ' . wc_price($price) . ', <a class="atc" href="' . $product->add_to_cart_url() . '">order now</a>.</li>';
}
echo $output . '</ul>';
$contents = ob_get_clean();
return $contents;
}

How to display first post of each tag

I have multiple posts, some of them have same tag, i need to display only first posts of each tag. I was unable to find the solution on google or here.
Wordpress codex show this as example which displays each tag. Is this possible to combine with the query posts
<?php $tags = get_tags();
$html = '<div class="post_tags">';
foreach ( $tags as $tag ) {
$tag_link = get_tag_link( $tag->term_id );
$html .= "<a href='{$tag_link}' title='{$tag->name} Tag' class='{$tag->slug}'>";
$html .= "{$tag->name}</a>";
}
$html .= '</div>';
echo $html; ?>
Only you have to do it, just call a post query with specific post tag inside your each tag loop as follows -
$tags = get_tags();
$html = '<div class="post_tags">';
foreach ( $tags as $tag ) :
$tag_link = get_tag_link( $tag->term_id );
$html .= ''.$tag->name.'';
// fetch tag specific post
$args = array(
'numberposts' => 1,
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => $tag->slug
)
)
);
$postslist = get_posts( $args );
foreach ( $postslist as $post ) :
$html .= '<h2>'.$post->post_title.'</h2>';
$html .= $post->post_content;
endforeach;
endforeach;
$html .= '</div>';
echo $html;

wordpress custom latest post functions code issue

I am using this code in function.php for latest posts:
function last_article($atts){
$a = shortcode_atts( array(
'posts' => 1,
), $atts );
$q = new WP_Query(array('orderby' => 'date', 'order' => 'DESC' ,
'posts_per_page' => 1 , 'category__in' => $atts ));
if ($q->have_posts()) {
while ($q->have_posts()) {
$q->the_post();
$return_string .= '<div class="content"><div class="thumb">'.featured_image_thumb().'</div>';
$return_string .= '<h2>'.get_the_title().'</h2>';
the_excerpt();
$return_string .= '<div class="detail"><div class="data"><span><i class="icon-user"></i>'.the_author().'</span><span><i class="icon-calendar"></i>'.the_time('j F Y').'</span></div>';
$return_string .= '<div class="more">more... <i class="fa fa-arrow-left"></i> </div></div></div>';
}
}
wp_reset_postdata(); return $return_string; }
And then use echo last_article in theme.
But it first show featured image and then shows <div class="content"> empty. and also shows some other HTML tags empty.
Can someone tell me what is wrong?
Try this:
function last_article($atts){
$a = shortcode_atts( array(
'posts' => 1,
), $atts );
$q = new WP_Query(array('orderby' => 'date', 'order' => 'DESC' ,
'posts_per_page' => 1 , 'category__in' => $atts ));
if ($q->have_posts()) {
while ($q->have_posts()) {
$q->the_post();
$return_string .= '<div class="content"><div class="thumb">'.featured_image_thumb().'</div>';
$return_string .= '<h2>'.get_the_title().'</h2>'
. get_the_excerpt()
. '<div class="detail"><div class="data"><span><i class="icon-user"></i>'.the_author().'</span><span><i class="icon-calendar"></i>'.the_time('j F Y').'</span></div>';
$return_string .= '<div class="more">more... <i class="fa fa-arrow-left"></i> </div></div></div>';
}
}
wp_reset_postdata(); return $return_string; }

custom post list item shortcode

how to add this 2 short code in one, because I want to use this in several categories. please also inform me how to add category in this short code.
now my short code is [featured-post]
I want this [featured-post='category name']
exactly, I need a shortcode 5 post will display from a category but first post have trumbanail then title then excerpt then rest of 4 list cetegory post only show title and permalink
function featured_post_shortcode($atts){
extract( shortcode_atts( array(),
$atts, 'featured-post' ) );
$q = new WP_Query(
array( 'category' => $category, 'posts_per_page' => '1', 'post_type' => 'post')
);
$list = '';
while($q->have_posts()) : $q->the_post();
//get the ID of your post in the loop
$id = get_the_ID();
$post_thumbnail= get_the_post_thumbnail( $post->ID, 'lead-thumbnail' );
$list .= '
<div class="featured">
'.$post_thumbnail.'
'.get_the_title().'
<p>' . get_the_excerpt() . '</p>
</div>
';
endwhile;
$list.= '</div>';
wp_reset_query();
return $list;
}
add_shortcode('featured-post', 'featured_post_shortcode');
function more_item_shortcode($atts){
extract( shortcode_atts( array(),
$atts, 'featured-post' ) );
$q = new WP_Query(
array( 'category' => $category, 'posts_per_page' => '4', 'post_type' => 'post')
);
$list = '<ul>';
while($q->have_posts()) : $q->the_post();
//get the ID of your post in the loop
$id = get_the_ID();
$list .=
'<li>' . get_the_title() . '</li>';
endwhile;
$list.= '</ul>';
wp_reset_query();
return $list;
}
add_shortcode('more-item', 'more_item_shortcode');
function featured_post_shortcode($atts){
extract(shortcode_atts( array('cat' => ''), $atts));
$q = new WP_Query(
array( 'cat' => $cat, 'posts_per_page' => '5', 'post_type' => 'post')
//Use category slug: array( 'category_name' => $cat, 'posts_per_page' => '5', 'post_type' => 'post')
);
$count = 0;
if( $q->have_posts() ):
$output = '<ul>';
while($q->have_posts()) : $q->the_post(); $count++; global $post;
if($count == 1){
$output .= '
<div class="featured">'.
get_the_post_thumbnail($post->ID, 'lead-thumbnail').
''.get_the_title().'
<p>' . get_the_excerpt() . '</p>
</div>
';
}else{
$output .= '
<li>
'.get_the_title().'
</li>
';
}
endwhile;
$output .= '</ul>';
endif;
wp_reset_query();
return $output;
}
add_shortcode('featured-post', 'featured_post_shortcode');
if you want to use the category slug then use category_name otherwise use cat which takes the category ID
in your callback: [featured-post cat="1"]

Display posts of a Sub Category of my taxonomy

I created a Shortcode to display Term of my Taxonomy. But for one of my taxonomy I have a sub term or sub category. And I don't understand how can I display posts of a Sub Category of my Taxonomy.
My Code in functions.php
function theme_lasts_posts_shortcode( $atts, $content = null ) {
extract( shortcode_atts( array(
"posttype" => '',
"taxonomy" => '',
"term" => '',
"class" => '',
"exclude" => '',
), $atts));
$output = '<div class="derniersarticles">';
if ( $posttype != '' ) {
$loop = new WP_Query( array( 'post_type' => $posttype, 'taxonomy' => $taxonomy,'term' => $term, 'posts_per_page' => 100, 'post__not_in' => array($exclude) ) );
} else {
$loop = new WP_Query( array( 'post_type' => $posttype, 'posts_per_page' => 100 ) );
}
while ( $loop->have_posts() ) : $loop->the_post();
$output .= '<div class="'. $class .'">';
$output .= '<div class="thumb">';
$output .= '' . get_the_post_thumbnail() . '';
$output .= '</div>';
$output .= '<h3>' . get_the_title() . '</h3>';
$output .= '</div>';
endwhile;
$output .= '</div>';
return $output;
}
add_shortcode( 'DerniersArticles', 'theme_lasts_posts_shortcode' );
IF i understood question corectly you need to get posts from children of parent category from custom taxonomy?
In That case, try this :
<?php
$father = get_term_by('name', 'main_category_name', 'product_cat' );
$father_id = $father->term_id;
$taxonomy_name = 'product_cat';
$children = get_term_children( $father_id, $taxonomy_name );
echo '<div class="sidebar_cat">';
echo '<h5 class="sidebar_heading">'.'main_category_name'.'</h5>';
echo '<ul class="sidebar_text">';
foreach ( $children as $child ) {
$term = get_term_by( 'id', $child, $taxonomy_name );
echo '<li>'.$term->name . '</li>';
}
echo '</ul>'; ?>

Resources