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; }
Related
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;
}
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";
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"]
I am hitting a wall here, although it sounds pretty simple: I want to return a hierarchical list of custom post type taxonomy terms. What I get is the first level of terms and nested uls. But the sub terms are not showing. Any ideas?
Here's the code:
function return_terms_index() {
$taxonomies = array(
'taxonomy_name',
);
$args = array(
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => false,
'fields' => 'all',
'parent' => 0,
'hierarchical' => true,
'child_of' => 0,
'pad_counts' => false,
'cache_domain' => 'core'
);
$terms = get_terms($taxonomies, $args);
$return .= '<ul>';
foreach ( $terms as $term ) {
// return terms (working)
$return .= sprintf(
'<li id="category-%1$s" class="toggle">%2$s <span class="cat-description">%3$s</span>',
$term->term_id,
$term->name,
$term->description
);
$subterms = get_terms( array(
'parent' => $term->term_id,
'hide_empty' => false
));
$return .= '<ul>';
foreach ( $subterms as $subterm ) {
//return sub terms (not working :( )
$return .= sprintf(
'<li id="category-%1$s" class="toggle">%2$s <span class="cat-description">%3$s</span>',
$subterm->term_id,
$subterm->name,
$subterm->description
);
$return .= '</li>'; //end subterms li
}
$return .= '</ul>'; //end subterms ul
$return .= '</li>'; //end terms li
} //end foreach term
$return .= '</ul>';
return $return;
}
Thanks!
Edit: here's the output.
<ul>
<li id="category-176">
1. <span class="post-count">0</span><span class="cat-description" style="display: none;">Description</span>
<ul id="subTerm-176" style="display: block;"></ul>
</li>
<li id="category-49">
2. <span class="post-count">0</span><span class="cat-description" style="display: none;">Langtitel/Beschreibung</span>
<ul id="subTerm-49" style="display: none;"></ul>
</li>
</ul>
Edit: taxonomies are returned in hierarchical list now, YAY!
But I want to query and display posts of third level taxonomy terms as well and this bit of code doesn't do the trick.
$post_query = new WP_Query($taxonomies, array(
'term' => $subsubterm->term_id
)); ?>
<?php if ( $post_query->have_posts() ) :
$return .= '<ul>';
while ( $post_query->have_posts() ) : $post_query->the_post();
$return .= '<li><a class="link" href="' . get_permalink() . '">' . get_the_title() . '</a></li>' . "\n";
endwhile;
$return .= '</ul>';
wp_reset_postdata();
else:
endif;
It has to be dynamic, so I can't specify a term by name/slug. But is this even possible?
'term' => $subsubterm->term_id
Thanks again.
You have missed to pass $taxonomies in
$subterms = get_terms($taxonomies, array(
'parent' => $term->term_id,
'hide_empty' => false
));
Try following code
function return_terms_index() {
$taxonomies = array(
'taxonomy_name',
);
$args = array(
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => false,
'fields' => 'all',
'parent' => 0,
'hierarchical' => true,
'child_of' => 0,
'pad_counts' => false,
'cache_domain' => 'core'
);
$terms = get_terms($taxonomies, $args);
$return .= '<ul>';
foreach ( $terms as $term ) {
// return terms (working)
$return .= sprintf(
'<li id="category-%1$s" class="toggle">%2$s <span class="cat-description">%3$s</span>',
$term->term_id,
$term->name,
$term->description
);
$subterms = get_terms($taxonomies, array(
'parent' => $term->term_id,
'hide_empty' => false
));
$return .= '<ul>';
foreach ( $subterms as $subterm ) {
//return sub terms (not working :( )
$return .= sprintf(
'<li id="category-%1$s" class="toggle">%2$s <span class="cat-description">%3$s</span>',
$subterm->term_id,
$subterm->name,
$subterm->description
);
$return .= '</li>'; //end subterms li
}
$return .= '</ul>'; //end subterms ul
$return .= '</li>'; //end terms li
} //end foreach term
$return .= '</ul>';
return $return;
}
Just use the wp_list_categories() function :
https://developer.wordpress.org/reference/functions/wp_list_categories/
$tax_args = array(
'taxonomy' => 'my_custom_taxonomy',
'orderby' => 'name',
'show_count' => 1,
'hierarchical' => 1,
'title_li' => 'My list title'
);
wp_list_categories($tax_args);
I want to show my custom post using shortcode
my custom post code is below-
/Custom Post/
add_action( 'init', 'create_post_type' );
function create_post_type() {
register_post_type( 'service_items',
array(
'labels' => array(
'name' => __( 'Service Items' ),
'singular_name' => __( 'Service Items' ),
'add_new' => __( 'Add New' ),
'add_new_item' => __( 'Add New Service Items' ),
'edit_item' => __( 'Edit Service Items' ),
'new_item' => __( 'New Service Items' ),
'view_item' => __( 'View Service Items' ),
'not_found' => __( 'Sorry, we couldn\'t find the Service Items you are looking for.' )
),
'public' => true,
'publicly_queryable' => true,
'exclude_from_search' => true,
'menu_position' => 14,
'has_archive' => false,
'hierarchical' => false,
'capability_type' => 'page',
'rewrite' => array( 'slug' => 'custompost_id' ),
'supports' => array( 'title', 'editor', 'custom-fields','thumbnail' )
)
);
}
And Shortcode code is below -
function service_item_shortcode() {
$args = array(
'post_type' => 'service_items',
'order' => 'DESC',
'posts_per_page'=> -1
);
$service_items = new WP_Query( $args );
if( $service_items->have_posts() ):
while ( $service_items->have_posts() ) : $service_items->the_post();
$service_output = '<div class="span3 serv">';
$service_output .='<div class="serv-img">' ;
$service_output .=get_the_post_thumbnail( $service_items->post->ID, service-image) ;
$service_output .='</div>';
$service_output .= '<h3>' . get_the_title() . '</h3> ';
$service_output .= '<p>' . get_the_content() . '</p> ';
$service_output .= '</div>';
endwhile;
endif;
return $service_output;
}
add_shortcode( 'service_item', 'service_item_shortcode' );
I want to show on my post 4 custom post items per page.
But when I put [service-item] shortcode output only show 1 post.
I need 4 post per page. Please help.
You should write the output from all posts in a variable, instead of resetting the variable on each iteration. To do this, define an empty variable before the loop, and simply add to it all of the output of each post. Here is the changed code that should work for you (untested):
function service_item_shortcode() {
$service_output = '';
$paged = !empty($_GET['service_item_page']) ? absint($_GET['service_item_page']) : 1;
if (!$paged) {
$paged = 1;
}
$args = array(
'post_type' => 'service_items',
'order' => 'DESC',
'posts_per_page'=> 4,
'paged' => $paged
);
$service_items = new WP_Query( $args );
if( $service_items->have_posts() ):
while ( $service_items->have_posts() ) : $service_items->the_post();
$service_output .= '<div class="span3 serv">';
$service_output .= '<div class="serv-img">' ;
$service_output .= get_the_post_thumbnail( get_the_ID(), 'service-image');
$service_output .= '</div>';
$service_output .= '<h3>' . get_the_title() . '</h3> ';
$service_output .= '<p>' . get_the_content() . '</p> ';
$service_output .= '</div>';
endwhile;
endif;
if ($service_items->max_num_pages > 1) {
$service_output .= '<div class="pagination">';
for($i = 1; $i <= $service_items->max_num_pages; $i++) {
$service_output .= '' . $i . ' ';
}
$service_output .= '</div>';
}
wp_reset_postdata();
return $service_output;
}
add_shortcode( 'service_item', 'service_item_shortcode' );