Have been fiddling around with the while loop as well as the query params but can't seem to figure out what is causing the following custom code to not limit posts:
/**
* Plugin Name: Latest posts
*/
add_action( 'widgets_init', 'weart_latest_posts' );
function weart_latest_posts() {
register_widget( 'weart_latest_posts' );
}
class weart_latest_posts extends WP_Widget {
//widget setup
function weart_latest_posts() {
$widget_ops = array( 'classname' => 'weart_latest_posts', 'description' => esc_html__('A widget that displays the latest posts.', 'custom') );
$control_ops = array( 'width' => 250, 'height' => 350, 'id_base' => 'weart_latest_posts' );
$this->__construct( 'weart_latest_posts', esc_html__('Custom: Latest Posts Widget', 'custom'), $widget_ops, $control_ops );
}
//display the widget
function widget( $args, $instance ) {
extract( $args );
global $post;
$title = apply_filters('widget_title', $instance['title'] );
$number = $instance['number'];
/* Before widget (defined by themes). */
echo $before_widget;
/* Display the widget title if one was input (before and after defined by themes). */
if ( $title )
echo $before_title . $title . $after_title;
?>
<?php
$args1 = array(
'cat' => 'RECIPES',
'nopaging' => false,
'posts_per_page' => '5',
'found_posts' => '5',
'max_num_pages' => '5',
'post_count' => '5',
'order' => 'DESC',
'orderby' => 'ID',
);
$query = new WP_Query( $args1 );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
;?>
<a href="<?php the_permalink(); ?>" class="block cf item border-top">
<?php if ( has_post_thumbnail()) : ?>
<div class="featured-image col col-one-third img" data-mh="latest_posts_height">
<div class=""><div class="bg lazy" data-src="<?php the_post_thumbnail_url( 'weart-related-thumb'); ?>"></div></div>
</div>
<?php endif; ?>
<div class="text col <?php if ( has_post_thumbnail()) : ?>col-two-third<?php endif; ?>" data-mh="latest_posts_height">
<div class="<?php if ( has_post_thumbnail()) : ?>inner-left text-vertical<?php endif; ?>">
<h3 class="title h4"><?php the_title(); ?></h3>
<div class="meta c-grey link-grey meta-meta"><time datetime="<?php echo esc_attr(get_the_date('Y-m-d')); ?>"> <?php echo esc_attr(get_the_date()); ?></time></div>
</div>
</div>
</a>
<?php }
} else {
} wp_reset_postdata();
?>
<?php
/* After widget (defined by themes). */
echo $after_widget;
}
//update widget
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags( $new_instance['title'] );
$instance['number'] = '5';
return $instance;
}
//form for update
function form( $instance ) {
//defaults
$defaults = array( 'title' => esc_html__('Latest Recipes', 'custom'), 'number' => 5, 'popular_days' => 30 );
$instance = wp_parse_args( (array) $instance, $defaults ); ?>
<p><!-- title -->
<label for="<?php echo esc_attr($this->get_field_id( 'title' )); ?>">Title:</label>
<input id="<?php echo esc_attr($this->get_field_id( 'title' )); ?>" name="<?php echo esc_attr($this->get_field_name( 'title' )); ?>" value="<?php echo esc_attr($instance['title']); ?>" style="width:90%;" />
</p><!-- title -->
<p><!-- posts num -->
<label for="<?php echo esc_attr($this->get_field_id( 'number' )); ?>">Number of posts to display:</label>
<input id="<?php echo esc_attr($this->get_field_id( 'number' )); ?>" name="<?php echo esc_attr($this->get_field_name( 'number' )); ?>" value="<?php echo esc_attr($instance['number']); ?>" size="3" />
</p><!-- posts num -->
<?php
}
}
?>
I thought the query params would lead to what I needed and played around with having them both as a variable and also directly in the statement.
Try to remove these:
'nopaging' => false,
'found_posts' => '5',
'max_num_pages' => '5',
'post_count' => '5',
And pass a category ID to this:
'cat' =>
Related
I am using dokan plugin for a multivendor website, I want to add image upload custom field in the template
new-product.php, I used CMB2 plugin to create image upload custom field with WooCommerce like this
function themebox_metaboxes() {
// Start with an underscore to hide fields from custom fields list
$prefix = 'themebox_met_';
// Product Settings
$header_settings = new_cmb2_box( array(
'id' => 'Extra_settings',
'title' => esc_html__( 'Extra Settings', 'themebox' ),
'object_types' => array( 'product'), // Post type
'context' => 'normal',
'priority' => 'high',
'show_names' => true,
) );
$header_settings->add_field( array(
'name' => esc_html__( 'Add Image Detail size 590x300 px', 'themebox' ),
'id' => $prefix . 'img_detail',
'type' => 'file'
) );
}
I want to add this custom image upload field in template form new-product.php and when save form in dokan
the image upload custom field update with an image added in dokan .....exactly like featured product image in WooCommerce
You can override the new-product.php and new-product-single.php file and then add your field on the product upload/edit file. To add new field for product edit/add the template you will have to add a new field in this template (Override via child-theme) - dokan-lite/templates/products/new-product-single.php. However, there are some other steps required to successfully save them.
You need to modify the Dokan product upload template and then you have to add an extra field by overriding the template. After adding the input filed you have to save the value of the field. On that place you have to use do_action( 'dokan_new_product_added', $product_id, $post_data ); this hook to save the field data.
When you will edit the product that time you have to use do_action( 'dokan_product_updated', $post_id ); to re-save.
Thanks :)
function save_add_product_meta($product_id, $postdata){
if ( ! dokan_is_user_seller( get_current_user_id() ) ) {
return;
}
if ( ! empty( $postdata['title'] ) ) {
update_post_meta( $product_id, 'title', $postdata['title'] );
}
if ( ! empty( $postdata['subtitle'] ) ) {
update_post_meta( $product_id, 'subtitle', $postdata['subtitle'] );
}
if ( ! empty( $postdata['subdescription'] ) ) {
update_post_meta( $product_id, 'subdescription', $postdata['subdescription'] );
}
if ( ! empty( $postdata['vidimg'] ) ) {
update_post_meta( $product_id, 'vidimg', $postdata['vidimg'] );
}
}
/*
* Showing field data on product edit page
*/
add_action('dokan_product_edit_after_product_tags','show_on_edit_page',99,8);
function show_on_edit_page($post, $post_id){
$subtitle = get_post_meta( $post_id, 'subtitle', true );
$title = get_post_meta( $post_id, 'title', true );
$subdesc = get_post_meta( $post_id, 'subdescription', true );
$vidimg = get_post_meta( $post_id, 'vidimg', true );
?>
<div class="dokan-form-group">
<h6 class="auto">Ajoutez du contenu pour mettre en valeur cette oeuvre !</h6>
<input type="hidden" name="title" id="dokan-edit-product-id" value="<?php echo esc_attr( $post_id ); ?>"/>
<label for="new_field" class="form-label"><?php esc_html_e( 'Autre Titre', 'dokan-lite' ); ?></label>
<?php dokan_post_input_box( $post_id, 'title', array( 'placeholder' => __( 'product code', 'dokan-lite' ), 'value' => $title ) ); ?>
<p class="help-block">50 caractères maximum (conseillé)</p>
</div>
<div class="dokan-form-group">
<input type="hidden" name="subtitle" id="dokan-edit-product-id" value="<?php echo esc_attr( $post_id ); ?>"/>
<label for="subtitle" class="form-label"><?php esc_html_e( 'Sous titre', 'dokan-lite' ); ?></label>
<?php dokan_post_input_box( $post_id, 'subtitle', array( 'placeholder' => __( 'product code', 'dokan-lite' ), 'value' => $subtitle ) ); ?>
<p class="help-block">80 caractères maximum (conseillé)</p>
</div>
<div class="dokan-form-group">
<label for="subdescription" class="form-label">Paragraphe d'introduction</label>
<div class="dokan-rich-text-wrap">
<?php dokan_post_input_box( $post_id, 'subdescription', array('placeholder' => 'ajouter une description', 'value' => $subdesc ), 'textarea' ); ?>
</div>
</div>
<div class="dokan-feat-image-upload">
<?php
$wrap_class = ' dokan-hide';
$instruction_class = '';
$feat_image_id = 0;
if (!empty($vidimg) ) {
$wrap_class = '';
$instruction_class = ' dokan-hide';
$imaid =attachment_url_to_postid($vidimg);
}
?>
<div class="instruction-inside<?php echo esc_attr( $instruction_class ); ?>">
<input type="hidden" name="vidimg" class="dokan-feat-image-id" value="<?php echo esc_attr($vidimg ); ?>">
<i class="fa fa-cloud-upload"></i>
<?php esc_html_e( 'Upload a product cover image', 'dokan-lite' ); ?>
</div>
<div class="image-wrap<?php echo esc_attr( $wrap_class ); ?>">
<a class="close dokan-remove-feat-image">×</a>
<?php if ( ! empty($vidimg) ) { ?>
<img src="<?php echo esc_url(wp_get_attachment_url($vidimg ) ); ?>" alt="">
<?php } else { ?>
<img height="" width="" src="" alt="">
<?php } ?>
</div>
</div><!-- .dokan-feat-image-upload -->
<?php
}
I am using a template which has a widget to show woocommetce product on the home page, Currently price of product is just a number E.g. ₹18500/- without any decimal or comma, I want to show price with Thousand separator and Decimal separator E.g. ₹ 18,500/-
Note: Thousand separator and Decimal separator is enabled from woocommerce settings, and here I want to show price on a widget.
I am attaching code of the widget.
any help will be appreciated.
* Filter the arguments for the Recent Posts widget.
*
* #since 1.0.0
*
* #see WP_Query
*
*/
$query_args = array(
'posts_per_page' => $post_number,
'post_status' => 'publish',
'post_type' => 'product',
'no_found_rows' => 1,
'order' => $order,
'meta_query' => array(),
'tax_query' => array(
'relation' => 'AND',
),
);
switch ( $wc_advanced_option ) {
case 'featured' :
if( !empty( $product_visibility_term_ids['featured'] )){
$query_args['tax_query'][] = array(
'taxonomy' => 'product_visibility',
'field' => 'term_taxonomy_id',
'terms' => $product_visibility_term_ids['featured'],
);
}
break;
case 'onsale' :
$product_ids_on_sale = wc_get_product_ids_on_sale();
if( !empty( $product_ids_on_sale ) ){
$query_args['post__in'] = $product_ids_on_sale;
}
break;
case 'cat' :
if( !empty( $travel_way_wc_product_cat )){
$query_args['tax_query'][] = array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $travel_way_wc_product_cat,
);
}
break;
case 'tag' :
if( !empty( $travel_way_wc_product_tag )){
$query_args['tax_query'][] = array(
'taxonomy' => 'product_tag',
'field' => 'term_id',
'terms' => $travel_way_wc_product_tag,
);
}
break;
}
switch ( $orderby ) {
case 'price' :
$query_args['meta_key'] = '_price';
$query_args['orderby'] = 'meta_value_num';
break;
case 'sales' :
$query_args['meta_key'] = 'total_sales';
$query_args['orderby'] = 'meta_value_num';
break;
case 'ID' :
case 'author' :
case 'title' :
case 'date' :
case 'modified' :
case 'rand' :
case 'comment_count' :
case 'menu_order' :
$query_args['orderby'] = $orderby;
break;
default :
$query_args['orderby'] = 'date';
}
$travel_way_featured_query = new WP_Query( $query_args );
if ($travel_way_featured_query->have_posts()) :
echo $args['before_widget'];
$animation = "init-animate zoomIn";
?>
<section id="<?php echo esc_attr( $unique_id ); ?>" class="at-widgets acme-abouts <?php echo $bg_gray_class;?>">
<div class="container">
<?php
if ( ! empty( $title ) ) {
echo $args['before_title'] . esc_html( $title ) . $args['after_title'];
}
$div_attr = 'class="featured-entries-col woocommerce"';
?>
<div class="row at-cat-product-wrap clearfix ">
<div <?php echo $div_attr;?>>
<?php
$travel_way_featured_index = 1;
while ( $travel_way_featured_query->have_posts() ) :$travel_way_featured_query->the_post();
$travel_way_list_classes = 'single-list';
if ( 1 == $column_number ) {
$travel_way_list_classes .= " col-sm-12";
} elseif ( 2 == $column_number ) {
$travel_way_list_classes .= " col-sm-6";
} elseif ( 3 == $column_number ) {
$travel_way_list_classes .= " col-sm-4 col-md-4";
} else {
$travel_way_list_classes .= " col-sm-4 col-md-3";
}
?>
<div class="<?php echo esc_attr( $travel_way_list_classes ); ?>">
<a href="<?php the_permalink();?>">
<?php the_post_thumbnail($travel_way_img_size)?>
<div class="caption">
<h3 class="at-woo-title"><?php the_title();?></h3>
<?php
woocommerce_template_loop_rating();
$currency = get_woocommerce_currency_symbol();
$price = get_post_meta( get_the_ID(), '_regular_price', true);
$sale = get_post_meta( get_the_ID(), '_sale_price', true);
if($sale) :
global $post, $product;
echo apply_filters( 'woocommerce_sale_flash', '<span class="onsale">' . esc_html__( 'Sale!', 'travel-way' ) . '</span>', $post, $product );
?>
<p class="product-price">
<del>
<?php
echo esc_html($currency);
echo esc_html( $price );
?>
</del>
<?php
echo esc_html($currency);
echo esc_html( $sale . '/-' );
?>
</p>
<?php elseif($price) : ?>
<p class="product-price">
<?php
echo esc_html($currency);
echo esc_html( $price . '/-');
?>
</p>
<?php endif;
?>
</div>
</a>
</div><!--dynamic css-->
<?php
$travel_way_featured_index++;
endwhile;
?>
</div><!--featured entries-col-->
</div><!--cat product wrap-->
<?php
echo $args['after_widget'];
echo "<div class='clearfix'></div>";
// Reset the global $the_post as this query will have stomped on it
?>
</div>
</section>
<?php
endif;
wp_reset_postdata();
}
} // Class Travel_Way_Wc_Products ends here
} ```
You can use number_format. Should be something like:
$price = floatval($price);
$decimal_separator = wc_get_price_decimal_separator();
$thousand_separator = wc_get_price_thousand_separator();
$decimals = wc_get_price_decimals();
$formatted_price = number_format( $price, $decimals, $decimal_separator, $thousand_separator );
echo esc_html( $formatted_price . '/-');
I am trying to get grouped products highest price so that i can show only highest price on custom template page.I am using WP_Query to get list of products.
Here is the snippet:
$series_term = $_GET['series'];
$args=array(
'series' => $term_name->slug,
'post_type' => 'product',
'posts_per_page' => 8,
'filter' =>$valuecol,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'hidden' ), // Don't display products in the knives category on the shop page
'operator' => 'NOT IN'
)
)
);
// $do_not_duplicate[] = $post->ID;
$my_query = null;
$my_query = new WP_Query($args);
/* Start the Loop */
while ($my_query->have_posts()) : $my_query->the_post();
$feat_prod_img = wp_get_attachment_image_src(get_post_thumbnail_id(), 'post-thumb-single');
$feat_product_img = wp_get_attachment_url( get_post_thumbnail_id($my_query->post->ID) );
?>
<div class="col-md-3 col-sm-4 col-xs-6">
<div class="product-detail">
<div class="product-thmbnial">
<a href="<?php the_permalink(); ?>">
<img src="<?php $src=$feat_product_img; echo image_product_resize_crop( $src, $w=440, $h=275, $dest = null, $override = false, $createNewIfExists = false ); ?>" alt="<?php the_title(); ?>"/>
</a>
</div>
<div class="info"><p><?php the_title(); ?><br/>
<span>
<?php $wp_product = new WC_Product( get_the_ID() );
echo get_woocommerce_currency_symbol(); ?></sup><?php
echo $wp_product->get_price();?></span></p></div>
</div>
</div>
<?php endwhile; ?>
I want grouped product highest price to show in place of $wp_product->get_price();
I tried to get children products and but the array is empty.
foreach ( $wp_product->get_children() as $child_id ) {
$child_prices[] = get_post_meta( $child_id, '_price', true );
}
can anyone suggest me how i can get highest price.
Thanks in advance.
I found the solution.. I tried the same solution earlier but that time it wasn't working.
Here is the fix:
$series_term = $_GET['series'];
$args=array(
'series' => $term_name->slug,
'post_type' => 'product',
'posts_per_page' => 8,
'filter' =>$valuecol,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'hidden' ), // Don't display products in the knives category on the shop page
'operator' => 'NOT IN'
)
)
);
// $do_not_duplicate[] = $post->ID;
$my_query = null;
$my_query = new WP_Query($args);
/* Start the Loop */
while ($my_query->have_posts()) : $my_query->the_post();
$feat_prod_img = wp_get_attachment_image_src(get_post_thumbnail_id(), 'post-thumb-single');
$feat_product_img = wp_get_attachment_url( get_post_thumbnail_id($my_query->post->ID) );
?>
<div class="col-md-3 col-sm-4 col-xs-6">
<div class="product-detail">
<div class="product-thmbnial">
<a href="<?php the_permalink(); ?>">
<img src="<?php $src=$feat_product_img; echo image_product_resize_crop( $src, $w=440, $h=275, $dest = null, $override = false, $createNewIfExists = false ); ?>" alt="<?php the_title(); ?>"/>
</a>
</div>
<div class="info"><p><?php the_title(); ?><br/>
<span>
<?php $wp_product = new WC_Product( get_the_ID() );
$child_prices = array();
foreach ( $product->get_children() as $child_id ) {
$child_prices[] = get_post_meta( $child_id, '_price', true );
}
$child_prices = array_unique( $child_prices );
if ( ! empty( $child_prices ) ) {
$min_price = min( $child_prices );
$max_price = max( $child_prices );
} else {
$min_price = '';
$max_price = '';
}
if($min_price == $max_price && !empty($min_price))
{
echo get_woocommerce_currency_symbol(); ?></sup><?php
echo $min_price;
}
elseif(!empty($max_price))
{
echo get_woocommerce_currency_symbol(); ?></sup><?php
echo $max_price;
}
else
{
echo get_woocommerce_currency_symbol(); ?></sup><?php
echo $wp_product->get_price();
}
?></span></p></div>
</div>
</div>
<?php endwhile; ?>
Thanks for reply I am very grateful Kārlis Millers, dingo-d now my widget it looks how.
<?php
class ultimos_productos_widget_1 extends WP_Widget {
function __construct() {
parent::__construct(
'ultimos_productos_widget', __( 'ultimos_productos_widget_1', 'tutsplustextdomain' ),
array(
'classname' => 'ultimos_productos_widget_1',
'description' => __( 'A basic text widget to demo the Tutsplus series on creating your own widgets.', 'tutsplustextdomain' )
)
);
}
public function widget($args,$instance){
extract( $args );
echo $before_widget = '<div class="sidebar woocommerce">' ;
?>
<h3 class="widget-title"> <?php _e('Ultimate Products', 'pruebadesarrollo'); ?></h3>
<ul class="products">
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => 5,
'product_cat' => 'marble',
'orderby' => 'ASC',
/*'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'marble',
),
),*/
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
<li class="product">
<a href="<?php echo get_permalink( $loop->post->ID ) ?>" title="<?php echo esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID); ?>">
<?php woocommerce_show_product_sale_flash( $post, $product ); ?>
<?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog'); else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" width="300px" height="300px" />'; ?>
<h3><?php the_title(); ?></h3>
<span class="price"><?php echo $product->get_price_html(); ?></span>
</a>
<?php if(is_user_logged_in() ){ ?>
<?php woocommerce_template_loop_add_to_cart( $loop->post, $product ); ?>
<?php } ?>
</li>
<?php endwhile; ?>
<?php wp_reset_postdata() ?>
</ul><!--/.products-->
<?php
echo $after_widget='</div>';
}
public function update($new_instance, $old_instance){
// Función de guardado de opciones
}
public function form($instance){
// Formulario de opciones del Widget, que aparece cuando añadimos el Widget a una Sidebar
}
}
function myplugin_register_widgets() {
register_widget( 'ultimos_productos_widget_1' );
}
add_action( 'widgets_init', 'myplugin_register_widgets' );
?>
It was working before of update a to wordpress 4.3.
In wp_debug show me this message:
Notice: The called constructor method for WP_Widget is deprecated since version 4.3.0! Use
Internet research I found that I should change the next line.
$this->WP_Widget('ultimos_productos_widget', "ultimos 5 proyectos", $widget_ops);
for
parent::__construct('ultimos_productos_widget', "ultimos 5 proyectos", $widget_ops);
I dont know what happened?
Widget need register after you created it
Just put after your class:
register_widget('ultimos_productosaluminum_widget');
Or in wordpress style:
function register_ultimos_widget() {
register_widget('ultimos_productosaluminum_widget');
}
add_action( 'widgets_init', 'register_ultimos_widget' );
See more at CODEX
Thanks for your comments,
I modified the code with the suggestions y this is my custom widget.
class ultimos_productos_widget extends WP_Widget {
function __construct() {
$widget_ops = array('classname' => 'ultimos_productos_widget',
'description' => "5 ultimos 5 productos por categoría" );
parent::__construct('ultimos_productos_widget', "ultimos 5 productos por categoría", $widget_ops);
}
public function widget($args,$instance){
extract( $args );
$title = apply_filters( 'widget_title', $instance['title']);
$ids = $instance['ids'];
$query_args = array(
'post_status' => 'publish',
'post_type' => 'product',
'posts_per_page' => 5,
'orderby' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $ids,
),
),
);
$r = new WP_Query( $query_args );
if ( $r->have_posts() ) {
echo $before_widget;
if ( $title )
echo $before_title . $title . $after_title;
echo '<ul class="product_list_widget">';
while ( $r->have_posts()) {
$r->the_post();
global $product;
?>
<li>
<a href="<?php echo esc_url( get_permalink( $product->id ) ); ?>" title="<?php echo esc_attr( $product->get_title() ); ?>">
<?php echo $product->get_image(); ?>
<?php echo $product->get_title(); ?>
</a>
<?php if ( ! empty( $show_rating ) ) echo $product->get_rating_html(); ?>
<?php if(is_user_logged_in() ){ ?>
<?php echo $product->get_price_html(); ?>
<?php } ?>
</li>
<?php
}
echo '</ul>';
echo $after_widget;
}
wp_reset_postdata();
echo $content;
}
public function form($instance){
$title = (isset($instance['title'])) ? $instance['title'] : __( 'Products', 'ndb' );
$ids = $instance['ids'];
?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title :'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('ids'); ?>"><?php _e('Category :'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('ids'); ?>" name="<?php echo $this->get_field_name( 'ids' ); ?>" type="text" value="<?php echo esc_attr( $ids ); ?>" />
</p>
<?php
}
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
$instance['ids'] = ( ! empty( $new_instance['ids'] ) ) ? strip_tags( $new_instance['ids'] ) : '';
return $instance;
}
}
function ultimos_productos() {
register_widget( 'ultimos_productos_widget' );
}
add_action( 'widgets_init', 'ultimos_productos' );
This a plugin and works perfectly.
Thanks.
I am building a shopping website and I am trying to put a shortcode in that will show the customer a buy button and the quantity of the product the customer wants to purchase. On my post page the shortcode works fine:
http://warringah-plastics.com.au/blog/dt_catalog/recess-gasket-large/
but on the archive page:
http://warringah-plastics.com.au/store/
the shortcode id displayed as text and not the actual button and quantity e.g. [add_to_cart item="FPROWAR-160713-1" showprice="no" quantity="user:1" ajax="yes" ].
The code that works in the post page is this:
<?php
$my_textbox_value = mtbxr_val("shopping_shortcode");
echo do_shortcode("$my_textbox_value");
?>
but it just displays the shortcode text on that archive page. Anyone have any ideas? Much appreciated,
UPDATE
THIS IS THE CODE THAT DISPLAYS THE SHORTCODE CORRECTLY:
<?php get_header(); ?>
<?php dt_storage('have_sidebar', true); ?>
<?php get_template_part('top-bg'); ?>
<?php get_template_part('parallax'); ?>
<div id="wrapper">
<?php get_template_part('nav'); ?>
<div id="container">
<?php if( have_posts() ): while( have_posts() ): the_post(); ?>
<h1><?php the_title(); ?></h1>
<h1 style="color: #3C3C3B !important; margin-top:-20px !important;"><?php $terms_as_text = strip_tags( get_the_term_list( $wp_query->post->ID, 'dt_catalog_category', '', ', ', '' ) );
echo $terms_as_text; ?></h1>
<?php
global $post;
$post_opts = get_post_meta($post->ID, '_dt_catalog-post_options', true);
if( !isset($post_opts['hide_media']) || (isset($post_opts['hide_media']) && !$post_opts['hide_media']) ) {
$args = array(
'post_type' => 'attachment',
'post_status' => 'inherit',
'posts_per_page' => -1,
'post_parent' => $post->ID,
'post_mime_type' => 'image',
'orderby' => 'menu_order',
'order' => 'ASC'
);
if( !empty($post_opts['hide_thumbnail']) )
$args['post__not_in'] = array( get_post_thumbnail_id() );
$dt_tmp_query = new WP_Query( $args );
if( $dt_tmp_query->have_posts() ) {
$slides = array();
foreach( $dt_tmp_query->posts as $slide ) {
$video = get_post_meta( $slide->ID, '_dt_catalog_video_link', true );
$tmp_arr = array();
$tmp_arr['caption'] = $slide->post_excerpt;
if ( ! $video ) {
$slide_src = dt_get_resized_img( wp_get_attachment_image_src( $slide->ID, 'full' ), array( 'w' => 710 ) );
$tmp_arr['alt'] = get_post_meta( $slide->ID, '_wp_attachment_image_alt', true );
$tmp_arr['src'] = $slide_src[0];
$tmp_arr['size_str'] = $slide_src[3];
} else {
$tmp_arr['is_video'] = true;
$tmp_arr['src'] = $video;
$tmp_arr['size_str'] = array( 710, 1024 );
}
$slides[] = $tmp_arr;
}
dt_get_anything_slider( array( 'id' => 'slider2', 'items_arr' => $slides ) );
}
}
?>
<?php $opts = get_post_meta($post->ID, '_dt_catalog-goods_options', true); ?>
<?php if( !empty($opts['price']) ): ?>
<span class="price"><?php _e('Price: ', LANGUAGE_ZONE); echo esc_html($opts['price']); ?></span>
<?php endif; ?>
<?php
$my_textbox_value = mtbxr_val("shopping_shortcode");
echo do_shortcode("$my_textbox_value");
?>
<?php
the_content();
if( dt_is_page_soc_buttons_enabled('catalog') ) {
dt_get_like_buttons( get_the_ID() );
}
?>
<?php if( !empty($opts['p_link']) ): ?>
<span><i class="dol"></i><?php _e('Make purchase!', LANGUAGE_ZONE); ?></span>
<?php endif; ?>
<p class="gap"></p>
<?php
$rel_works = get_post_meta($post->ID, '_dt_catalog_related', true);
if( isset($rel_works['show_related']) && $rel_works['show_related'] ):
if( 'same' == $rel_works['related'] ) {
$rel_works['related'] = wp_get_post_terms(
$post->ID,
'dt_catalog_category',
array('fields' => 'ids')
);
}
if( !empty($rel_works['related']) ):
?>
<p class="hr hr-narrow gap-small"></p>
<div class="gap"></div>
<div class="full-width w-photo">
<h2><?php _e('Related Items', LANGUAGE_ZONE); ?></h2>
<?php
if( 'same' == $rel_works['related'] ) {
$rel_works['related'] = wp_get_post_terms(
$post->ID,
'dt_catalog_category',
array('fields' => 'ids')
);
}
$dt_tmp_query = new WP_Query( array(
'posts_per_page' => -1,
'post_type' => 'dt_catalog',
'post_status' => 'publish',
'post__not_in' => array($post->ID),
'tax_query' => array( array(
'taxonomy' => 'dt_catalog_category',
'field' => 'id',
'terms' => $rel_works['related'],
'operator' => 'IN'
) )
) );
if( $dt_tmp_query->have_posts() ) {
$thumb_arr = dt_core_get_posts_thumbnails( $dt_tmp_query->posts );
$items = array();
foreach( $dt_tmp_query->posts as $rel_post ) {
$item = array();
$img = dt_get_resized_img(
dt_get_thumb_meta($thumb_arr['thumbs_meta'], 'full', $rel_post->ID),
array('w' => 196, 'h' => 123, 'use_noimage' => true)
);
$item['src'] = $img[0];
$item['size_str'] = $img[2];
$item['post_id'] = $rel_post->ID;
$item['desc'] = apply_filters('get_the_excerpt', $rel_post->post_excerpt);
$item['title'] = apply_filters('the_title', $rel_post->post_title, $rel_post->ID);
$item['alt'] = esc_attr( $item['title'] );
$items[] = $item;
}
$args = array( 'items_arr' => $items, 'id' => '', 'class' => 'list-carousel recent bx', 'ul_class' => 'slider1' );
$args['wrap'] = '<div class="%CLASS% bx">%SLIDER%</div>';
if( ! empty( $rel_works['show_desc'] ) || ! empty( $rel_works['show_title'] ) ) {
$title = '';
if( ! empty( $rel_works['show_title'] ) ) {
$title = '<h3>%TITLE%</h3>';
}
$desc = '';
if( ! empty( $rel_works['show_desc'] ) ) {
$desc = '<p>%DESC%</p>';
}
$args['item_wrap'] = '
<li>
<div class="textwidget">
<div class="textwidget-photo">
<a class="photo" href="%LINK%"><img src="%IMG_SRC%" alt="%ALT%" %IMG_SIZE% /></a>
</div>
<div class="widget-info">
<div class="info">
' . $title . $desc . '
</div>
</div>
</div>
</li>
';
}
dt_get_carousel_slider( $args );
}
?>
</div>
<?php endif; endif; ?>
<?php comments_template(); ?>
<?php
endwhile;
endif;
?>
</div>
<?php dt_widget_area('sidebar', null, 'sidebar_4'); ?>
</div>
<?php get_footer(); ?>
AND THIS IS THE CODE THAT DISPLAYS THE SHORTCODE JUST AS TEXT:
<?php
global $post;
$page_data = dt_storage( 'page_data' );
$page_opts = ! empty( $page_data['page_options'] ) ? $page_data['page_options'] : array();
$add_data = dt_storage( 'add_data' );
$first_class = '';
if( 1 === dt_storage('post_is_first') ) {
$first_class = ' first';
dt_storage( 'post_is_first', -1 );
}
$opts = get_post_meta($post->ID, '_dt_catalog-goods_options', true);
?>
<div class="<?php dt_portfolio_classes( '2_col-list', 'block' ); echo $first_class; ?>">
<?php
$h = 220;
if ( ! empty ( $page_opts['thumb_height'] ) ) {
$h = $page_opts['thumb_height'];
}
dt_get_thumb_img( array(
'class' => 'photo',
'use_noimage' => true,
'href' => get_permalink(),
'thumb_opts' => array( 'w' => 343, 'h' => $h )
),
'<div class="textwidget-photo">
<a %HREF% %CLASS% %TITLE% %CUSTOM%><img %ALT% %SRC% %IMG_CLASS% %SIZE% /></a>
</div>'
);
?>
<div class="<?php dt_portfolio_classes( '2_col-list', 'info' ); ?>">
<a class="<?php dt_portfolio_classes( '2_col-list', 'head' ); ?>" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php if( !empty($opts['price']) ): ?>
<span class="price"><?php _e('Price: ', LANGUAGE_ZONE); echo esc_html($opts['price']); ?></span>
<?php endif; ?>
<?php
dt_the_content();
dt_details_link();
dt_edit_link();
?>
<div id="specialpriceshortcode">
<?php
$my_textbox_value = mtbxr_val("shopping_shortcode");
echo do_shortcode("$my_textbox_value");
?>
</div>
</div>
</div>
Try using single quotes in the do_shortcode call, like so:
echo do_shortcode('$my_textbox_value');
More likely though is that the shortcode isn't defined on the archive page so you'd need to look at where it is being instantiated to see if that is the issue. Normally when a shortcode just echoes out the content it means that shortcode doesn't exist. You can test easily enough by using the shortcode_exists() function:
<?php if ( shortcode_exists( 'add_to_cart' ) ) { echo "The shortcode exists";} ?>
If that doesn't work then you know the issue is with the shortcode not being registered on your archives page. If it does work then you know it's something with the format of the content being passed to the shortcode.
Add this to your functions.php
// Allow shortcodes on widgets
add_filter('widget_text','do_shortcode');
// Allow shortcodes on pages (not tested, but should work)
add_filter('the_content','do_shortcode');
Typically your shortcode is getting registered in a plugin or your theme's functions.php file. In a plugin it's often something like:
add_action('init', 'register_my_shortcode');
function register_my_shortcode(){
add_shortcode('my_shortcode', 'do_my_shortcode');
}
And then you'd have a function do_my_short_code() that actually outputs the content. With something like that the shortcode is getting registered via the 'init' hook (http://codex.wordpress.org/Plugin_API/Action_Reference) which is called before WP has started figuring out what template to use, what content to output, etc.
But some plugins will register the shortcode in a way that it is only available on pages / posts where it's going to potentially be used. For example, I can think of one plugin where they register the shortcode and enqueue some javascripts in the same function. That function checks to see if you're on a particular page before it executes so that the js files are not included unnecessarily all over the place. Since the shortcode registration takes place in the same function it means the shortcode only exists on those pages.
Anyhow, if the shortcode is showing as existing on your archives page you know that isn't the problem, so check that first and let me know what you find.