I need to configure this script so that it fetches the first image that is in the article and shows it in a widjet.
At the moment the widjet only shows featured images, I would like it to show external images that were in the post.
For example, the first image in the article would be shown.
Code
<?php
/**
* Featured Posts widget
*/
class colormag_featured_posts_widget extends WP_Widget {
function __construct() {
$widget_ops = array(
'classname' => 'widget_featured_posts widget_featured_meta',
'description' => __( 'Display latest posts or posts of specific category.', 'colormag' ),
'customize_selective_refresh' => true,
);
$control_ops = array( 'width' => 200, 'height' => 250 );
parent::__construct( false, $name = __( 'TG: Featured Posts (Style 1)', 'colormag' ), $widget_ops );
}
function form( $instance ) {
$tg_defaults['title'] = '';
$tg_defaults['text'] = '';
$tg_defaults['number'] = 4;
$tg_defaults['type'] = 'latest';
$tg_defaults['category'] = '';
$instance = wp_parse_args( ( array ) $instance, $tg_defaults );
$title = esc_attr( $instance['title'] );
$text = esc_textarea( $instance['text'] );
$number = $instance['number'];
$type = $instance['type'];
$category = $instance['category'];
?>
<p><?php _e( 'Layout will be as below:', 'colormag' ) ?></p>
<div style="text-align: center;"><img src="<?php echo get_template_directory_uri() . '/img/style-1.jpg' ?>">
</div>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', 'colormag' ); ?></label>
<input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo $title; ?>" />
</p>
<?php _e( 'Description', 'colormag' ); ?>
<textarea class="widefat" rows="5" cols="20" id="<?php echo $this->get_field_id( 'text' ); ?>" name="<?php echo $this->get_field_name( 'text' ); ?>"><?php echo $text; ?></textarea>
<p>
<label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php _e( 'Number of posts to display:', 'colormag' ); ?></label>
<input id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" type="text" value="<?php echo $number; ?>" size="3" />
</p>
<p>
<input type="radio" <?php checked( $type, 'latest' ) ?> id="<?php echo $this->get_field_id( 'type' ); ?>" name="<?php echo $this->get_field_name( 'type' ); ?>" value="latest" /><?php _e( 'Show latest Posts', 'colormag' ); ?>
<br />
<input type="radio" <?php checked( $type, 'category' ) ?> id="<?php echo $this->get_field_id( 'type' ); ?>" name="<?php echo $this->get_field_name( 'type' ); ?>" value="category" /><?php _e( 'Show posts from a category', 'colormag' ); ?>
<br /></p>
<p>
<label for="<?php echo $this->get_field_id( 'category' ); ?>"><?php _e( 'Select category', 'colormag' ); ?>
:</label>
<?php wp_dropdown_categories( array(
'show_option_none' => ' ',
'name' => $this->get_field_name( 'category' ),
'selected' => $category,
) ); ?>
</p>
<?php
}
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags( $new_instance['title'] );
if ( current_user_can( 'unfiltered_html' ) ) {
$instance['text'] = $new_instance['text'];
} else {
$instance['text'] = stripslashes( wp_filter_post_kses( addslashes( $new_instance['text'] ) ) );
}
$instance['number'] = absint( $new_instance['number'] );
$instance['type'] = $new_instance['type'];
$instance['category'] = $new_instance['category'];
return $instance;
}
function widget( $args, $instance ) {
extract( $args );
extract( $instance );
global $post;
$title = isset( $instance['title'] ) ? $instance['title'] : '';
$text = isset( $instance['text'] ) ? $instance['text'] : '';
$number = empty( $instance['number'] ) ? 4 : $instance['number'];
$type = isset( $instance['type'] ) ? $instance['type'] : 'latest';
$category = isset( $instance['category'] ) ? $instance['category'] : '';
$post_status = 'publish';
if ( get_option( 'fresh_site' ) == 1 ) {
$post_status = array( 'auto-draft', 'publish' );
}
$args = array(
'posts_per_page' => $number,
'post_type' => 'post',
'ignore_sticky_posts' => true,
'no_found_rows' => true,
'post_status' => $post_status,
);
// Display from category chosen.
if ( $type == 'category' ) {
$args['category__in'] = $category;
}
$get_featured_posts = new WP_Query( $args );
echo $before_widget;
?>
<?php
if ( $type != 'latest' ) {
$border_color = 'style="border-bottom-color:' . colormag_category_color( $category ) . ';"';
$title_color = 'style="background-color:' . colormag_category_color( $category ) . ';"';
} else {
$border_color = '';
$title_color = '';
}
if ( ! empty( $title ) ) {
echo '<h3 class="widget-title" ' . $border_color . '><span ' . $title_color . '>' . esc_html( $title ) . '</span></h3>';
}
if ( ! empty( $text ) ) {
?> <p> <?php echo esc_textarea( $text ); ?> </p> <?php } ?>
<?php
$i = 1;
while ( $get_featured_posts->have_posts() ):$get_featured_posts->the_post();
?>
<?php if ( $i == 1 ) {
$featured = 'colormag-featured-post-medium';
} else {
$featured = 'colormag-featured-post-small';
} ?>
<?php if ( $i == 1 ) {
echo '<div class="first-post">';
} elseif ( $i == 2 ) {
echo '<div class="following-post">';
} ?>
<div class="single-article clearfix">
<?php
if ( has_post_thumbnail() ) {
$image = '';
$thumbnail_id = get_post_thumbnail_id( $post->ID );
$image_alt_text = get_post_meta( $thumbnail_id, '_wp_attachment_image_alt', true );
$title_attribute = get_the_title( $post->ID );
if ( empty( $image_alt_text ) ) {
$image_alt_text = $title_attribute;
}
$image .= '<figure>';
$image .= '<a href="' . get_permalink() . '" title="' . the_title( '', '', false ) . '">';
$image .= get_the_post_thumbnail( $post->ID, $featured, array(
'title' => esc_attr( $title_attribute ),
'alt' => esc_attr( $image_alt_text ),
) ) . '</a>';
$image .= '</figure>';
echo $image;
}
?>
<div class="article-content">
<?php colormag_colored_category(); ?>
<h3 class="entry-title">
<?php the_title(); ?>
</h3>
<div class="below-entry-meta">
<?php
$time_string = '<time class="entry-date published updated" datetime="%1$s">%2$s</time>';
if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) {
$time_string = '<time class="entry-date published" datetime="%1$s">%2$s</time><time class="updated" datetime="%3$s">%4$s</time>';
}
$time_string = sprintf( $time_string,
esc_attr( get_the_date( 'c' ) ),
esc_html( get_the_date() ),
esc_attr( get_the_modified_date( 'c' ) ),
esc_html( get_the_modified_date() )
);
printf( __( '<span class="posted-on"><i class="fa fa-calendar-o"></i> %3$s</span>', 'colormag' ), esc_url( get_permalink() ), esc_attr( get_the_time() ), $time_string
);
?>
<span class="byline"><span class="author vcard"><i class="fa fa-user"></i><a class="url fn n" href="<?php echo esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ); ?>" title="<?php echo get_the_author(); ?>"><?php echo esc_html( get_the_author() ); ?></a></span></span>
<?php if ( ! post_password_required() && comments_open() ) { ?>
<span class="comments"><i class="fa fa-comment"></i><?php comments_popup_link( '0', '1', '%' ); ?></span>
<?php } ?>
</div>
<?php if ( $i == 1 ) { ?>
<div class="entry-content">
<?php the_excerpt(); ?>
</div>
<?php } ?>
</div>
</div>
<?php if ( $i == 1 ) {
echo '</div>';
} ?>
<?php
$i ++;
endwhile;
if ( $i > 2 ) {
echo '</div>';
}
// Reset Post Data
wp_reset_query();
?>
<!-- </div> -->
<?php
echo $after_widget;
}
}
This is the complete code of the widget
Try this code
<?php if (has_post_thumbnail( $post->ID ) ): ?>
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
<div id="custom-bg" style="background-image: url('<?php echo $image[0]; ?>')">
</div>
<?php endif; ?>
Related
My hosting provided upgraded to PHP7.2 and now I started to receive a lot of issues. As my Wordpress theme builder does no longer exist I went through the issues myself fixing problems. For some reason this does not go away and I'm worried about it.
Fatal error: Allowed memory size of 1048576000 bytes exhausted (tried
to allocate 20480 bytes) in
/customers/3/8/4/XXX/httpd.www/wp-content/themes/dejavu/lib/classes/widget-popular.php
on line 14
Whenever I increase the limit in wp-config it just keeps growing. Is the problem in that php file or should I look somewhere else? Content of that php file:
<?php
/**
*
*/
class MySite_PopularPost_Widget extends WP_Widget {
/**
*
*/
function __construct() {
$widget_ops = array( 'classname' => 'mysite_popular_widget', 'description' => __( 'Custom popular post widget with post preview image', MYSITE_ADMIN_TEXTDOMAIN ) );
$control_ops = array( 'width' => 250, 'height' => 200 );
$this->__construct( 'popularwidget', sprintf( __( '%1$s - Popular Post', MYSITE_ADMIN_TEXTDOMAIN ), THEME_NAME ), $widget_ops, $control_ops );
}
/**
*
*/
function widget($args, $instance) {
global $wpdb, $mysite;
$prefix = MYSITE_PREFIX;
extract( $args );
$title = apply_filters('widget_title', empty($instance['title']) ? __('Popular Posts') : $instance['title'], $instance, $this->id_base);
if ( !$number = (int) $instance['number'] )
$number = 3;
else if ( $number < 1 )
$number = 1;
else if ( $number > 15 )
$number = 15;
echo $before_widget;
echo $before_title . $title . $after_title;
$count = ( !empty( $count ) ) ? trim( $count ) : '3';
$disable_thumb = $instance['disable_thumb'] ? '1' : '0';
$popular_query = new WP_Query(array(
'showposts' => $number,
'nopaging' => 0,
'orderby'=> 'comment_count',
'post_status' => 'publish',
'category__not_in' => array( mysite_exclude_category_string( $minus = false )),
'ignore_sticky_posts' => 1
));
$out = '<ul class="post_list small_post_list">';
while ( $popular_query->have_posts() ) {
$popular_query->the_post();
$out .= '<li class="post_list_module">';
if( !$disable_thumb ) {
$widget_thumb_img = $mysite->layout['big_sidebar_images']['small_post_list'];
$out .= mysite_get_post_image(array(
'width' => $widget_thumb_img[0],
'height' => $widget_thumb_img[1],
'img_class' => 'post_list_image',
'preload' => false,
'placeholder' => true,
'echo' => false,
'wp_resize' => ( mysite_get_setting( 'image_resize_type' ) == 'wordpress' ? true : false )
));
}
$out .= '<div class="post_list_content">';
$out .= '<p class="post_title">';
$out .= '<a rel="bookmark" href="' . esc_url( get_permalink() ) . '" title="' . esc_attr( get_the_title() ) . '">' . get_the_title() . '</a>';
$out .= '</p>';
$get_year = get_the_time( 'Y', get_the_ID() );
$get_month = get_the_time( 'm', get_the_ID() );
$out .= '<p class="post_meta">';
$out .= apply_filters( 'mysite_widget_meta', do_shortcode( '[post_date]' ) );
$out .= '</p>';
$out .= '</div>';
$out .= '</li>';
}
$out .= '</ul>';
echo $out;
echo $after_widget;
wp_reset_postdata();
}
/**
*
*/
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['number'] = (int) $new_instance['number'];
$instance['disable_thumb'] = !empty($new_instance['disable_thumb']) ? 1 : 0;
return $instance;
}
/**
*
*/
function form($instance) {
$title = isset($instance['title']) ? esc_attr($instance['title']) : '';
$disable_thumb = isset( $instance['disable_thumb'] ) ? (bool) $instance['disable_thumb'] : false;
if ( !isset($instance['number']) || !$number = (int) $instance['number'] )
$number = 3;
?>
<p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e( 'Title:', MYSITE_ADMIN_TEXTDOMAIN ); ?></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 $title; ?>" /></p>
<p><label for="<?php echo $this->get_field_id('number'); ?>"><?php _e( "Enter the number of popular posts you'd like to display:", MYSITE_ADMIN_TEXTDOMAIN ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('number'); ?>" name="<?php echo $this->get_field_name('number'); ?>" type="text" value="<?php echo $number; ?>" /></p>
<input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('disable_thumb'); ?>" name="<?php echo $this->get_field_name('disable_thumb'); ?>"<?php checked( $disable_thumb ); ?> />
<label for="<?php echo $this->get_field_id('disable_thumb'); ?>"><?php _e( 'Disable Post Thumbnail?', MYSITE_ADMIN_TEXTDOMAIN ); ?></label></p>
<?php
}
}
?>
Trying to build a widget for my simple wordpress blog, which will display categories in the sidebar, but not exactly like the native wordpress category widget. Basically, what I am trying to achieve is to be able to mark certain categories as "New!" or something similar, but from within the widget itself.
So far I have the following code that registers my widget and can display categories list in it on the backend with checkbox next to the name.
When I check the box and trying to save it it returns unchecked again. Not sure if my update function is actually working as serialized array in the DB has not changed on save.
Here is what I have do far:
/* CUSTOM BLOG CATEGORIES WIDGETS */
class Spr123_Categories_Widget extends WP_Widget {
public function __construct() {
$widget_options = array(
'classname' => 'widget_custom_categories_widget',
'description' => 'This is a Custom Blog Categories Widget',
);
parent::__construct( 'custom_categories_widget', 'Custom Categories Widget', $widget_options );
}
public function form( $instance ) {
$title = ! empty( $instance['title'] ) ? $instance['title'] : '';
$name = ! empty( $instance['name'] ) ? $instance['name'] : '';
$checked = isset( $instance['checked'] ) ? (bool) $instance['checked'] : false;
$categories = get_categories( array(
'orderby' => 'name',
'parent' => 0,
"hide_empty" => 0,
) );
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>">Title:</label>
<input type="text" id="<?php echo $this->get_field_id( 'title' ); ?>" class="widefat" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo esc_attr( $title ); ?>" />
</p>
<p>
<label for="Categories">Categories:</label>
</p>
<?php print("<pre>".print_r($categories,true)."</pre>"); ?>
<p>
<?php
foreach ( $categories as $category ) {
?>
<input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id($category->slug); ?>" name="<?php echo $this->get_field_name('checked'); ?>"<?php checked( $checked ); ?> />
<label for="<?php echo $this->get_field_id($category->slug); ?>"><?php _e( 'Display as NEW - ' . esc_attr( $category->name )); ?></label><br />
<?php
}
?>
</p>
<?php
}
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance[ 'title' ] = strip_tags( $new_instance[ 'title' ] );
$instance[ 'name' ] = strip_tags( $new_instance[ 'name' ] );
$instance[ 'checked' ] = !empty($new_instance['checked']) ? 1 : 0;
return $instance;
}
public function widget( $args, $instance ) {
$title = apply_filters( 'widget_title', $instance[ 'title' ] );
$category_title = apply_filters( 'widget_title', $instance[ 'name' ] );
echo $args['before_widget'] . $args['before_title'] . $title . $args['after_title']; ?>
<p><?php echo $category_title ?></p>
<?php echo $args['after_widget'];
}
}
function spr123_custom_categories_widget() {
register_widget( 'Spr123_Categories_Widget' );
}
add_action( 'widgets_init', 'spr123_custom_categories_widget' );
Try this code
/* CUSTOM BLOG CATEGORIES WIDGETS */
class Spr123_Categories_Widget extends WP_Widget {
public function __construct() {
$widget_options = array(
'classname' => 'widget_custom_categories_widget',
'description' => 'This is a Custom Blog Categories Widget',
);
parent::__construct( 'custom_categories_widget', 'Custom Categories Widget', $widget_options );
}
public function form( $instance ) {
$title = ! empty( $instance['title'] ) ? $instance['title'] : '';
$name = ! empty( $instance['name'] ) ? $instance['name'] : '';
$checked = isset( $instance['checked'] ) ? (bool) $instance['checked'] : false;
$savedcategories = ! empty( $instance['categories'] ) ? $instance['categories'] : '';
$categories = get_categories( array(
'orderby' => 'name',
'parent' => 0,
"hide_empty" => 0,
) );
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>">Title:</label>
<input type="text" id="<?php echo $this->get_field_id( 'title' ); ?>" class="widefat" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo esc_attr( $title ); ?>" />
</p>
<p>
<label for="Categories">Categories:</label>
</p>
<!-- --><?php //print("<pre>".print_r($categories,true)."</pre>"); ?>
<p>
<?php
foreach ( $categories as $category ) {
$checked = in_array($category->term_id, $savedcategories)?'checked':'';
?>
<input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id($category->slug); ?>" name="<?php echo $this->get_field_name('categories[]'); ?>" <?php echo $checked; ?> value="<?php echo $category->term_id; ?>"/>
<label for="<?php echo $this->get_field_id($category->slug); ?>"><?php echo esc_attr( $category->name ) . _e( 'Display as NEW - ' ); ?></label><br />
<?php
}
?>
</p>
<?php
}
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance[ 'title' ] = strip_tags( $new_instance[ 'title' ] );
$instance[ 'name' ] = strip_tags( $new_instance[ 'name' ] );
$instance[ 'categories' ] = $new_instance['categories'];
return $instance;
}
public function widget( $args, $instance ) {
$title = apply_filters( 'widget_title', $instance[ 'title' ] );
$category_title = apply_filters( 'widget_title', $instance[ 'name' ] );
echo $args['before_widget'] . $args['before_title'] . $title . $args['after_title']; ?>
<p><?php echo $category_title ?></p>
<?php echo $args['after_widget'];
}
}
function spr123_custom_categories_widget() {
register_widget( 'Spr123_Categories_Widget' );
}
add_action( 'widgets_init', 'spr123_custom_categories_widget' );
you can the value in frontend by using this code
$widget_instances = get_option('widget_custom_categories_widget');
print_r($widget_instances);
Currently, my theme displays the most recently added WooCommerce products in a section on the homepage. What I would like is to instead show a random list of products instead of the same ones.
I've found the function that displays the products, but am unsure where I need to change the orderby.
Below is the function that writes the products to the page.
function hestia_shop_content() {
?>
<div class="hestia-shop-content">
<?php
$hestia_shop_shortcode = get_theme_mod( 'hestia_shop_shortcode' );
if ( ! empty( $hestia_shop_shortcode ) ) {
echo do_shortcode( $hestia_shop_shortcode );
echo '</div>';
return;
}
$hestia_shop_items = get_theme_mod( 'hestia_shop_items', 4 );
$args = array(
'post_type' => 'product',
);
$args['posts_per_page'] = ! empty( $hestia_shop_items ) ? absint( $hestia_shop_items ) : 4;
$hestia_shop_categories = get_theme_mod( 'hestia_shop_categories' );
if ( sizeof( $hestia_shop_categories ) >= 1 && ! empty( $hestia_shop_categories[0] ) ) {
$args['tax_query'] = array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $hestia_shop_categories,
),
);
}
$hestia_shop_order = get_theme_mod( 'hestia_shop_order', 'DESC' );
if ( ! empty( $hestia_shop_order ) ) {
$args['order'] = $hestia_shop_order;
}
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
$i = 1;
echo '<div class="row">';
while ( $loop->have_posts() ) {
$loop->the_post();
global $product;
global $post;
?>
<div class="col-ms-6 col-sm-6 col-md-3 shop-item">
<div class="card card-product">
<?php
$thumbnail = hestia_shop_thumbnail( null, 'hestia-shop' );
if ( empty( $thumbnail ) && function_exists( 'wc_placeholder_img' ) ) {
$thumbnail = wc_placeholder_img();
}
if ( ! empty( $thumbnail ) ) {
?>
<div class="card-image">
<a href="<?php echo esc_url( get_permalink() ); ?>"
title="<?php the_title_attribute(); ?>">
<?php echo $thumbnail; ?>
</a>
<div class="ripple-container"></div>
</div>
<?php
}
?>
<div class="content">
<?php
if ( function_exists( 'wc_get_product_category_list' ) ) {
$prod_id = get_the_ID();
$product_categories = wc_get_product_category_list( $prod_id );
} else {
$product_categories = $product->get_categories();
}
if ( ! empty( $product_categories ) ) {
$allowed_html = array(
'a' => array(
'href' => array(),
'rel' => array(),
),
);
echo '<h6 class="category">';
echo wp_kses( $product_categories, $allowed_html );
echo '</h6>';
}
?>
<h4 class="card-title">
<a class="shop-item-title-link" href="<?php the_permalink(); ?>"
title="<?php the_title_attribute(); ?>"><?php esc_html( the_title() ); ?></a>
</h4>
<?php
if ( $post->post_excerpt ) {
?>
<div class="card-description"><?php echo apply_filters( 'woocommerce_short_description', $post->post_excerpt ); ?></div>
<?php
}
?>
<div class="footer">
<?php
$product_price = $product->get_price_html();
if ( ! empty( $product_price ) ) {
echo '<div class="price"><h4>';
echo wp_kses(
$product_price, array(
'span' => array(
'class' => array(),
),
'del' => array(),
)
);
echo '</h4></div>';
}
?>
<div class="stats">
<?php hestia_add_to_cart(); ?>
</div>
</div>
</div>
</div>
</div>
<?php
if ( $i % 4 == 0 ) {
echo '</div><!-- /.row -->';
echo '<div class="row">';
}
$i ++;
}
wp_reset_postdata();
echo '</div>';
}
?>
</div>
<?php
}
Use order by rand in your query like this
$args = array(
'post_type' => 'product',
'orderby'=> 'rand'
);
In WooCommerce variable.php I have this code:
<?php
/**
* Variable product add to cart
*
* #author WooThemes
* #package WooCommerce/Templates
* #version 2.1.0
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
global $product, $post;
?>
<?php do_action( 'woocommerce_before_add_to_cart_form' ); ?>
<form class="variations_form cart" method="post" enctype='multipart/form-data' data-product_id="<?php echo $post->ID; ?>" data-product_variations="<?php echo esc_attr( json_encode( $available_variations ) ) ?>">
<?php if ( ! empty( $available_variations ) ) : ?>
<table class="variations" cellspacing="0">
<tbody>
<?php $loop = 0; foreach ( $attributes as $name => $options ) : $loop++; ?>
<tr>
<td class="label"><label for="<?php echo sanitize_title($name); ?>"><?php echo wc_attribute_label( $name ); ?></label></td>
<td class="value"><select id="<?php echo esc_attr( sanitize_title( $name ) ); ?>" name="attribute_<?php echo sanitize_title( $name ); ?>">
<option value=""><?php echo __( 'Choose an option', 'woocommerce' ) ?>…</option>
<?php
if ( is_array( $options ) ) {
if ( isset( $_REQUEST[ 'attribute_' . sanitize_title( $name ) ] ) ) {
$selected_value = $_REQUEST[ 'attribute_' . sanitize_title( $name ) ];
} elseif ( isset( $selected_attributes[ sanitize_title( $name ) ] ) ) {
$selected_value = $selected_attributes[ sanitize_title( $name ) ];
} else {
$selected_value = '';
}
// Get terms if this is a taxonomy - ordered
if ( taxonomy_exists( sanitize_title( $name ) ) ) {
$orderby = wc_attribute_orderby( sanitize_title( $name ) );
switch ( $orderby ) {
case 'name' :
$args = array( 'orderby' => 'name', 'hide_empty' => false, 'menu_order' => false );
break;
case 'id' :
$args = array( 'orderby' => 'id', 'order' => 'ASC', 'menu_order' => false, 'hide_empty' => false );
break;
case 'menu_order' :
$args = array( 'menu_order' => 'ASC', 'hide_empty' => false );
break;
}
$terms = get_terms( sanitize_title( $name ), $args );
foreach ( $terms as $term ) {
if ( ! in_array( $term->slug, $options ) )
continue;
echo '<option value="' . esc_attr( $term->slug ) . '" ' . selected( sanitize_title( $selected_value ), sanitize_title( $term->slug ), false ) . '>' . apply_filters( 'woocommerce_variation_option_name', $term->name ) . '</option>';
}
} else {
foreach ( $options as $option ) {
echo '<option value="' . esc_attr( sanitize_title( $option ) ) . '" ' . selected( sanitize_title( $selected_value ), sanitize_title( $option ), false ) . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $option ) ) . '</option>';
}
}
}
?>
</select> <?php
if ( sizeof( $attributes ) == $loop )
echo '<a class="reset_variations" href="#reset">' . __( 'Clear selection', 'woocommerce' ) . '</a>';
?></td>
</tr>
<?php endforeach;?>
</tbody>
</table>
<?php do_action( 'woocommerce_before_add_to_cart_button' ); ?>
<div class="single_variation_wrap" style="">
<?php do_action( 'woocommerce_before_single_variation' ); ?>
<div class="single_variation"></div>
<div class="variations_button">
<?php woocommerce_quantity_input(); ?>
<button type="submit" class="single_add_to_cart_button button alt"><?php echo $product->single_add_to_cart_text(); ?></button>
</div>
<input name="add-to-cart" value="<?php echo $product->id; ?>" />
<input type="hidden" name="product_id" value="<?php echo esc_attr( $post->ID ); ?>" />
<input type="hidden" name="variation_id" value="" />
<?php do_action( 'woocommerce_after_single_variation' ); ?>
</div>
<?php do_action( 'woocommerce_after_add_to_cart_button' ); ?>
<?php else : ?>
<p class="stock out-of-stock"><?php _e( 'This product is currently out of stock and unavailable.', 'woocommerce' ); ?></p>
<?php endif; ?>
</form>
<?php do_action( 'woocommerce_after_add_to_cart_form' ); ?>
But when I choose the size and then click Add to cart, I get an error with which says "choose product options".
Does somebody know how to fix this? It's like the form and the button aren't working "together".
I found the answer.
The thing you had to fill in, was for the <input type="hidden" name="variation_id" value="" />
So i gave it as value the product id and it worked :D
So like this:
<input type="hidden" name="variation_id" value="<?php echo $product->id; ?>" />
I am adding the register_sidebar function to my functions.php file as shown below:
<?php
add_action( 'after_setup_theme', 'handheld_setup' );
if ( ! function_exists( 'handheld_setup' ) ){
function handheld_setup(){
global $et_mobile_theme_options;
load_theme_textdomain( 'HandHeld', TEMPLATEPATH . '/languages' );
add_action( 'wp_ajax_nopriv_et_show_ajax_posts', 'et_show_ajax_posts' );
add_action( 'wp_ajax_et_show_ajax_posts', 'et_show_ajax_posts' );
if ( isset( $et_mobile_theme_options['bg_color'] ) && '' != $et_mobile_theme_options['bg_color'] ) add_action( 'wp_head','et_add_bgcolor' );
add_filter( 'template_include', 'et_check_homepage_static' );
add_action( 'wp_head', 'et_add_apple_touch_images', 7 );
}
}
function et_add_apple_touch_images(){
global $et_mobile_theme_options;
$webpage_icon_small = isset( $et_mobile_theme_options['webpage_icon_small'] ) && '' != $et_mobile_theme_options['webpage_icon_small'] ? $et_mobile_theme_options['webpage_icon_small'] : get_template_directory_uri() . '/images/ios_icons/apple-touch-icon-precomposed.png';
$webpage_icon_big = isset( $et_mobile_theme_options['webpage_icon_big'] ) && '' != $et_mobile_theme_options['webpage_icon_big'] ? $et_mobile_theme_options['webpage_icon_big'] : get_template_directory_uri() . '/images/ios_icons/apple-touch-icon.png';
$splash_image = isset( $et_mobile_theme_options['splash_image'] ) && '' != $et_mobile_theme_options['splash_image'] ? $et_mobile_theme_options['splash_image'] : get_template_directory_uri() . '/images/ios_icons/splash.png';
echo '<link rel="apple-touch-icon-precomposed" href="' . esc_url( $webpage_icon_small ) . '" />';
echo '<link rel="apple-touch-icon-precomposed" sizes="114x114" href="' . esc_url( $webpage_icon_big ) . '" />';
echo '<link rel="apple-touch-startup-image" href="' . esc_url( $splash_image ) . '" />';
}
function et_check_homepage_static( $template ){
# if static homepage is set ( WP-Admin / Settings / Reading ) and we're on the homepage, load home.php
if ( is_front_page() && ! is_home() ) $template = get_home_template();
return $template;
}
function et_add_bgcolor(){
global $et_mobile_theme_options;
echo '<style>body{ background-color: #'. esc_html( str_replace( '#', '', $et_mobile_theme_options['bg_color'] ) ) .'; }</style>';
}
if ( ! function_exists( 'et_mobile_custom_comments_display' ) ) :
function et_mobile_custom_comments_display($comment, $args, $depth) {
$GLOBALS['comment'] = $comment; ?>
<li <?php comment_class(); ?> id="li-comment-<?php comment_ID() ?>">
<article id="comment-<?php comment_ID(); ?>" class="text_block comment clearfix">
<div class="avatar-box">
<?php echo get_avatar($comment,$size='37'); ?>
<span class="avatar-overlay"></span>
</div> <!-- end .avatar-box -->
<?php printf('<span class="fn">%s</span>', get_comment_author_link()) ?>
<div class="comment-content clearfix">
<?php if ($comment->comment_approved == '0') : ?>
<em class="moderation"><?php esc_html_e('Your comment is awaiting moderation.','HandHeld') ?></em>
<br />
<?php endif; ?>
<?php comment_text() ?>
</div> <!-- end comment-content-->
<div class="comment-meta clearfix">
<span class="comment-date"><?php if ( 1 == $depth ) printf( __( 'Posted on %1$s', 'HandHeld' ), get_comment_date() ); else echo get_comment_date(); ?></span>
<?php
$et_comment_reply_link = get_comment_reply_link( array_merge( $args, array('reply_text' => esc_attr__('Reply','HandHeld'),'depth' => $depth, 'max_depth' => $args['max_depth'])) );
if ( $et_comment_reply_link ) echo '<div class="reply-container">' . $et_comment_reply_link . '</div>';
?>
</div> <!-- end .comment-meta -->
</article>
<?php }
endif;
if ( ! function_exists( 'et_list_pings' ) ){
function et_list_pings($comment, $args, $depth) {
$GLOBALS['comment'] = $comment; ?>
<li id="comment-<?php comment_ID(); ?>"><?php comment_author_link(); ?> - <?php comment_excerpt(); ?>
<?php }
}
if ( ! function_exists( 'et_mobile_regular_post' ) ){
function et_mobile_regular_post(){
global $post; ?>
<article class="post text_block clearfix">
<?php
$thumb = '';
$width = 72;
$height = 72;
$classtext = '';
$titletext = get_the_title();
$thumbnail = et_get_thumbnail($width,$height,$classtext,$titletext,$titletext,false,'Entry');
$thumb = $thumbnail["thumb"];
?>
<?php if( $thumb <> '' ){ ?>
<div class="post-thumb">
<a href="<?php the_permalink(); ?>">
<?php et_print_thumbnail($thumb, $thumbnail["use_timthumb"], $titletext, $width, $height, $classtext); ?>
<span class="overlay"></span>
</a>
<span class="comment_count"><?php comments_popup_link( 0, 1, '%' ); ?></span>
</div> <!-- end .post-thumb -->
<?php } ?>
<div class="post-content">
<h1><?php the_title(); ?></h1>
<p class="meta-info"><?php esc_html_e('Posted on','HandHeld'); ?> <time datetime="<?php the_time( 'Y-m-d' ); ?>" pubdate><?php the_time( 'F jS' ); ?></time></p>
</div> <!-- end .post-content -->
<?php esc_html_e('Read more','HandHeld'); ?>
</article> <!-- end .post -->
<?php }
}
if ( ! function_exists('register_sidebar') ) {
// Register Sidebar
function register_sidebar() {
$args = array(
'id' => 'mobile-sidebar',
'name' => 'Mobile',
'description' => __( 'Sidebar for mobile', 'text_domain' ),
'before_title' => '<h4 class="widgettitle">',
'after_title' => '</h4>',
'before_widget' => '<li id="%1$s" class="widget %2$s">',
'after_widget' => '</li>',
);
register_sidebar( $args );
}
// Hook into the 'widgets_init' action
add_action( 'init', 'register_sidebar' );
}
if ( ! function_exists( 'et_mobile_gallery_post' ) ){
function et_mobile_gallery_post(){
global $post; ?>
<a href="<?php the_permalink(); ?>" class="project">
<?php
$thumb = '';
$width = 70;
$height = 70;
$classtext = '';
$titletext = get_the_title();
$thumbnail = et_get_thumbnail($width,$height,$classtext,$titletext,$titletext,false,'Project');
$thumb = $thumbnail["thumb"];
?>
<?php et_print_thumbnail($thumb, $thumbnail["use_timthumb"], $titletext, $width, $height, $classtext); ?>
<span></span>
</a>
<?php }
}
add_action( 'template_redirect', 'et_mobile_load_ajax_scripts' );
function et_mobile_load_ajax_scripts(){
wp_enqueue_script( 'et_home_load_more', get_template_directory_uri() . '/js/custom.js', array( 'jquery' ) );
wp_localize_script( 'et_home_load_more', 'etmobile', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ), 'et_load_nonce' => wp_create_nonce( 'et_load_nonce' ) ) );
}
function et_show_ajax_posts() {
global $et_mobile_theme_options;
if ( ! wp_verify_nonce( $_POST['et_load_nonce'], 'et_load_nonce' ) ) die(-1);
$posts_num = (int) $_POST['et_posts_num'];
$posts_offset = (int) $_POST['et_posts_offset'];
$gallery = (int) $_POST['et_gallery'];
$args = array(
'posts_per_page' => $posts_num,
'offset' => $posts_offset,
'post_status' => 'publish'
);
if ( isset( $et_mobile_theme_options['home_blog_categories'] ) && !empty( $et_mobile_theme_options['home_blog_categories'] ) && 0 == $gallery )
$args['category__in'] = $et_mobile_theme_options['home_blog_categories'];
if ( 0 != $gallery && isset( $et_mobile_theme_options['home_project_categories'] ) && !empty( $et_mobile_theme_options['home_project_categories'] ) )
$args['category__in'] = $et_mobile_theme_options['home_project_categories'];
ob_start();
$the_query = new WP_Query( $args );
while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php if ( 0 == $gallery ) { ?>
<?php et_mobile_regular_post(); ?>
<?php } else { ?>
<?php et_mobile_gallery_post(); ?>
<?php } ?>
<?php endwhile;
wp_reset_postdata();
$posts = ob_get_clean();
$last_query = ( $the_query->found_posts - $posts_offset ) > $posts_num ? false : true;
echo json_encode( array( 'posts' => $posts, 'last_query' => $last_query ) );
die();
} ?>
The register_sidebar function is declared at line 114.
In my home.phpfile, I am trying to call the sidebar as shown below:
<?php dynamic_sidebar( 'mobile-sidebar' ); ?>
But the sidebar never gets displayed.
What am I missing?
Thanks
Code in Functions.php
if ( function_exists('register_sidebar') ) {
register_sidebar(array(
'name' => 'Homepage Sidebar',
'id' => 'homepage-sidebar',
'description' => 'Appears as the sidebar on the custom homepage',
'after_widget' => '</li>',
'before_title' => '<h2 class="widgettitle">',
'after_title' => '</h2>',
));
}
Code in your home.php
<?php get_sidebar('homepage'); ?>
Create a file "sidebar-homepage.php"
Paste this code:
<div class="custom">
<?php
if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('homepage-sidebar') ) :
endif; ?>
</div>
Go to widgets and add some Text... Enjoy..