I want to slider the "dhelp_lite_slider"'s post thumbnails using owl carousel in wordpress site. I'm calling my guess wrong. I would be glad if you help.
function main_slider()
{
$html = '';
$slider = get_posts( array(
'post_type'=> 'dhelp_lite_slider',
'numberposts' => 10,
'post_status' => 'private',
'meta_key' => '_ordering_slider',
'orderby' => 'meta_value_num',
'order' => 'ASC',
));
$html .= '<div class="owl-carousel owl-theme">';
foreach ( $slider as $post ) {
//var_dump($post);
$html .= '<div class="item">'.$post->post_title.'</div>';
}
$html .= '</div>';
return $html;
}
add_shortcode( 'main_slider_hook', 'main_slider' );
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 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; }
in our wordpress site we modified image gallery style with using theme functions. We changed div classes, ids to use jquery image slider.
To make this we used this codes on functions.php
add_filter('post_gallery', 'ct_post_gallery', 10, 2);
function ct_post_gallery($output, $attr) {
global $post;
if (isset($attr['orderby'])) {
$attr['orderby'] = sanitize_sql_orderby($attr['orderby']);
if (!$attr['orderby'])
unset($attr['orderby']);
}
extract(shortcode_atts(array(
'order' => 'ASC',
'orderby' => 'menu_order ID',
'id' => $post->ID,
'itemtag' => 'dl',
'icontag' => 'dt',
'captiontag' => 'dd',
'columns' => 3,
'size' => 'thumbnail',
'include' => '',
'exclude' => ''
), $attr));
$id = intval($id);
if ('RAND' == $order) $orderby = 'none';
if (!empty($include)) {
$include = preg_replace('/[^0-9,]+/', '', $include);
$_attachments = get_posts(array('include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby));
$attachments = array();
foreach ($_attachments as $key => $val) {
$attachments[$val->ID] = $_attachments[$key];
}
}
if (empty($attachments)) return '';
// Here's your actual output, you may customize it to your need
$output = "<div id=\"icmimarlikreferans\">\n";
$output .= "<ul id=\"icmimarlikreferansimg\">\n";
// Now you loop through each attachment
foreach ($attachments as $id => $attachment) {
// Fetch the thumbnail (or full image, it's up to you)
// $img = wp_get_attachment_image_src($id, 'medium');
// $img = wp_get_attachment_image_src($id, 'my-custom-image-size');
$img = wp_get_attachment_image_src($id, 'full');
$output .= "<li class=\"item\">\n";
$output .= "<img src=\"{$img[0]}\" width=\"{$img[1]}\" height=\"{$img[2]}\" alt=\"\" />\n";
$output .= "</li>\n";
}
$output .= "</ul>\n";
$output .= "</div>\n";
return $output;
}
When we use it without making edits WordPress displaying image captions in single post pages at bottom of images in galleries . But when we use this code at functions.php captions are not displaying. We know the caption codes are missing, in this code block what we are using...
We tried to add caption functions in this codes but we have failed.
We need to display image captions at bottom of images in galleries.
We will love you if you help.
Working with a plugin that provides a shortcode with only a couple of parameters. https://wordpress.org/plugins/radio-station I'm using the [list-shows] shortcode. I would like to also pull in an excerpt of the content, I imagine that would be applicable to a lot of situations and improve my understanding of shortcodes.
I was able to add in the thumbnail to the output, but not the content, which seems to be referred to in the atts of the other shortcodes as 'show_desc'. You can see the output on this page: http://www.kzmuradio.org/kzmu-programs/
Here is the code for the shortcode. Apologies for my ignorance. Learning curve.
/* Shortcode for displaying a list of all shows * Since 2.0.0*/
function station_shortcode_list_shows($atts) {
extract( shortcode_atts( array(
'genre' => '',
'show_desc' => 1
), $atts ) );
//grab the published shows
$args = array(
'numberposts' => -1,
'offset' => 0,
'orderby' => 'title',
'order' => 'ASC',
'post_type' => 'show',
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'show_active',
'value' => 'on',
)
)
);
if($genre != '') {
$args['genres'] = $genre;
}
$shows = get_posts($args);
//if there are no shows saved, return nothing
if(!$shows) {
return false;
}
$output = '';
$output .= '<div id="station-show-list">';
$output .= '<ul>';
foreach($shows as $show) {
$output .= '<li>' . get_the_post_thumbnail($show->ID, 'thumbnail') . ''.get_the_title($show->ID).'</li>';
$output .= '</li>';
}
$output .= '</ul>';
$output .= '</div>';
return $output;
You can access all the data through $show in your foreach-loop. To see what they contain, you can use print_r():
print_r( $show );
For excerpt and content you can use this:
echo $show->post_excerpt;
echo wpautop( $show->post_content );
See also: https://codex.wordpress.org/Function_Reference/$post
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);