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'
);
Related
I want to add a number navigation in order to browse through the posts.
In the home of my theme I have this code that shows the posts.
<div class="container mt-80 mb-80">
<div class="card-columns">
<?php $args['tax_query'] = array(
array(
'taxonomy' => 'category',
'terms' => array('portfolio'),
'field' => 'slug',
'operator' => 'NOT IN',
),
);
query_posts($args);?>
<?php while(have_posts()) : the_post(); ?>
<div class="card">
<div class="card-body">
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php the_post_thumbnail('full', array('class' => 'image-post card-img-top')); ?>
</a>
<h3 class="card-title"><?php the_title(); ?></h3>
<p class="card-text"><?php echo get_the_excerpt(); ?> ...</p>
<span class="entry-date"><?php echo get_the_date(); ?></span>
</div>
</div>
<?php endwhile; wp_reset_query(); ?>
</div>
</div>
To show the pagination I tried to add this code on the function.php
I want to insert the page numbers below the posts to browse them but I don't want to install any plugins.
function numeric_posts_nav() {
if( is_singular() )
return;
global $wp_query;
/** Stop execution if there's only 1 page */
if( $wp_query->max_num_pages <= 1 )
return;
$paged = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1;
$max = intval( $wp_query->max_num_pages );
/** Add current page to the array */
if ( $paged >= 1 )
$links[] = $paged;
/** Add the pages around the current page to the array */
if ( $paged >= 3 ) {
$links[] = $paged - 1;
$links[] = $paged - 2;
}
if ( ( $paged + 2 ) <= $max ) {
$links[] = $paged + 2;
$links[] = $paged + 1;
}
echo '<div class="navigation"><ul>' . "\n";
/** Previous Post Link */
if ( get_previous_posts_link() )
printf( '<li>%s</li>' . "\n", get_previous_posts_link() );
/** Link to first page, plus ellipses if necessary */
if ( ! in_array( 1, $links ) ) {
$class = 1 == $paged ? ' class="active"' : '';
printf( '<li%s>%s</li>' . "\n", $class, esc_url( get_pagenum_link( 1 ) ), '1' );
if ( ! in_array( 2, $links ) )
echo '<li>…</li>';
}
/** Link to current page, plus 2 pages in either direction if necessary */
sort( $links );
foreach ( (array) $links as $link ) {
$class = $paged == $link ? ' class="active"' : '';
printf( '<li%s>%s</li>' . "\n", $class, esc_url( get_pagenum_link( $link ) ), $link );
}
/** Link to last page, plus ellipses if necessary */
if ( ! in_array( $max, $links ) ) {
if ( ! in_array( $max - 1, $links ) )
echo '<li>…</li>' . "\n";
$class = $paged == $max ? ' class="active"' : '';
printf( '<li%s>%s</li>' . "\n", $class, esc_url( get_pagenum_link( $max ) ), $max );
}
/** Next Post Link */
if ( get_next_posts_link() )
printf( '<li>%s</li>' . "\n", get_next_posts_link() );
echo '</ul></div>' . "\n"; }
And this on the index.php
<?php numeric_posts_nav(); ?>
The navigation appears but does not work properly. Does anyone know why?
put below code in function.php file
function pagination_nav() {
global $wp_query;
if ( $wp_query->max_num_pages > 1 ) { ?>
<nav class="pagination" role="navigation">
<div class="nav-previous"><?php next_posts_link( '← Older posts' ); ?></div>
<div class="nav-next"><?php previous_posts_link( 'Newer posts →' ); ?></div>
</nav>
}
}
and this code in index.php file :
pagination_nav();
Add below thing in the argument
$args = array(
'post_per_page' => 10,
'paged'=> 'paged', //'paged' for the home page and for other pages we need to set 'page',
'tax_query'=> array(
'taxonomy' => 'category',
'terms' => array('portfolio'),
'field' => 'slug',
'operator' => 'NOT IN',
),
);
And add the function just after your loop ends
<?php numeric_posts_nav(); ?>
<?php
$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
$args = array(
'post_per_page' => 10,
'paged'=> $paged,
'tax_query'=> array(
'taxonomy' => 'category',
'terms' => array('portfolio'),
'field' => 'slug',
'operator' => 'NOT IN',
),
); ?>
<?php $wp_query = new WP_Query($args); ?>
<?php if ($wp_query->have_posts()) : ?>
<?php while ( $wp_query -> have_posts() ) : $wp_query->the_post(); ?>
// Post content goes here...
<?php endwhile; ?>
<?php numeric_posts_nav(); ?>
<?php endif; ?>
<?php wp_reset_postdata(); ?>
I am getting this error
Trying to get property of non-object in /wp-includes/post-template.php
Others have suggested using wp_reset_postdata() to fix the issue however, it doesnt seem to work.
I have the code from the post-template.php below:
// If post password required and it doesn't match the cookie.
if ( post_password_required( $post ) )
return get_the_password_form( $post );
if ( $page > count( $pages ) ) // if the requested page doesn't exist
$page = count( $pages ); // give them the highest numbered page that DOES exist
$content = $pages[$page - 1];
if ( preg_match( '/<!--more(.*?)?-->/', $content, $matches ) ) {
$content = explode( $matches[0], $content, 2 );
if ( ! empty( $matches[1] ) && ! empty( $more_link_text ) )
$more_link_text = strip_tags( wp_kses_no_null( trim( $matches[1] ) ) );
$has_teaser = true;
} else {
$content = array( $content );
}
if ( false !== strpos( $post->post_content, '<!--noteaser-->' ) && ( ! $multipage || $page == 1 ) )
$strip_teaser = true;
$teaser = $content[0];
if ( $more && $strip_teaser && $has_teaser )
$teaser = '';
$output .= $teaser;
Here are my post functions/queries
function my_header_add_to_cart_fragment( $fragments ) {
ob_start();
$count = WC()->cart->cart_contents_count;
?><a class="cart-contents" href="<?php echo WC()->cart->get_cart_url(); ?>" title="<?php _e( 'View your shopping cart' ); ?>"><?php
if ( $count > 0 ) {
?>
<span class="cart-contents-count"><?php echo esc_html( $count ); ?></span>
<?php
}
?></a><?php
$fragments['a.cart-contents'] = ob_get_clean();
return $fragments;
}
add_filter( 'woocommerce_add_to_cart_fragments', 'my_header_add_to_cart_fragment' );
function global_menu($location,$class){
wp_nav_menu( array(
'theme_location' => $location,//primary
'depth' => 2, // 1 = no dropdowns, 2 = with dropdowns.
'container' => null,
'menu_class' => $class,//navbar-nav mr-auto
'fallback_cb' => 'WP_Bootstrap_Navwalker::fallback',
'walker' => new WP_Bootstrap_Navwalker(),
) );
}
add_filter( 'menu-init', 'global_menu' );
function portal_cart_link($url) {
?>
<a class="cart-contents" href="<?php echo $url; ?>" title="<?php esc_attr_e( 'View your shopping cart', permaslug() ); ?>">
<span class="cart-contents-count"><?php echo WC()->cart->get_cart_contents_count();?></span>
</a>
<?php
}
add_filter( 'woo-init', 'portal_cart_link' );
function product_query() {
$args = array(
'post_type' => 'product',
);
$loop = new WP_Query( $args );
if($loop->have_posts()):while ( $loop->have_posts() ) : $loop->the_post();
global $product;
echo '<div class="col-md-3 product"><img class="img-responsive" src="' . get_the_post_thumbnail_url( $post->ID) .'" /><br /><h3 class="text-center">' . get_the_title() . '</h3></div>';
endwhile;endif;
wp_reset_postdata();
}
add_filter( 'woo-init', 'product_query' );
I was translating the titles and sentences right on the editor. I don't know what went wrong, but when I hit upload I keep getting this:
Parse error: syntax error, unexpected 'Comentários' (T_STRING) in /home2/tkleinow/public_html/wp-content/themes/fashionistas/inc/template-tags.php on line 94
Then I tried to copy the original code for that section (template tags) on the zip files, but I keep getting the same error.
I pasted it on Excel to find what the line was, and this is wat I got:
<?php printf( __( '%s', 'athemes' ), sprintf( '<cite class="fn">%s</cite>', get_comment_author_link() ) ); ?>
My website was doing just fine, I don't know how I screwed it like that.
This is the whole thing:
<?php
/**
* Custom template tags for this theme.
*
* Eventually, some of the functionality here could be replaced by core features
*
* #package aThemes
*/
if ( ! function_exists( 'athemes_content_nav' ) ) :
/**
* Display navigation to next/previous pages when applicable
*/
function athemes_content_nav( $nav_id ) {
global $wp_query, $post;
// Don't print empty markup on single pages if there's nowhere to navigate.
if ( is_single() ) {
$previous = ( is_attachment() ) ? get_post( $post->post_parent ) : get_adjacent_post( false, '', true );
$next = get_adjacent_post( false, '', false );
if ( ! $next && ! $previous )
return;
}
// Don't print empty markup in archives if there's only one page.
if ( $wp_query->max_num_pages < 2 && ( is_home() || is_archive() || is_search() ) )
return;
$nav_class = ( is_single() ) ? 'post-navigation' : 'paging-navigation';
?>
<nav role="navigation" id="<?php echo esc_attr( $nav_id ); ?>" class="<?php echo $nav_class; ?>">
<h1 class="screen-reader-text"><?php _e( 'Post navigation', 'athemes' ); ?></h1>
<?php if ( is_single() ) : // navigation links for single posts ?>
<?php previous_post_link( '<div class="nav-previous"><span>Artigo Anterior</span>%link</div>', '<span class="meta-nav">' . _x( '←', 'Link do Post Anterior', 'athemes' ) . '</span> %title' ); ?>
<?php next_post_link( '<div class="nav-next"><span>Próximo Artigo</span>%link</div>', '%title <span class="meta-nav">' . _x( '→', 'Link para o próximo artigo', 'athemes' ) . '</span>' ); ?>
<?php elseif ( $wp_query->max_num_pages > 1 && ( is_home() || is_archive() || is_search() ) ) : // navigation links for home, archive, and search pages ?>
<?php if ( get_next_posts_link() ) : ?>
<div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">←</span> Artigos Anteriores', 'athemes' ) ); ?></div>
<?php endif; ?>
<?php if ( get_previous_posts_link() ) : ?>
<div class="nav-next"><?php previous_posts_link( __( 'Artigos Recentes <span class="meta-nav">→</span>', 'athemes' ) ); ?></div>
<?php endif; ?>
<?php endif; ?>
</nav><!-- #<?php echo esc_html( $nav_id ); ?> -->
<?php
}
endif; // athemes_content_nav
if ( ! function_exists( 'athemes_comment' ) ) :
/**
* Template for comments and pingbacks.
*
* Used as a callback by wp_list_comments() for displaying the comments.
*/
function athemes_comment( $comment, $args, $depth ) {
$GLOBALS['comment'] = $comment;
if ( 'pingback' == $comment->comment_type || 'trackback' == $comment->comment_type ) : ?>
<li id="comment-<?php comment_ID(); ?>" <?php comment_class(); ?>>
<div class="comment-body">
<?php _e( 'Pingback:', 'athemes' ); ?> <?php comment_author_link(); ?> <?php edit_comment_link( __( 'Edit', 'athemes' ), '<span class="edit-link">', '</span>' ); ?>
</div>
<?php else : ?>
<li id="comment-<?php comment_ID(); ?>" <?php comment_class( empty( $args['has_children'] ) ? '' : 'parent' ); ?>>
<article id="div-comment-<?php comment_ID(); ?>" class="comment-body">
<footer class="clearfix comment-meta">
<div class="reply">
<?php comment_reply_link( array_merge( $args, array( 'add_below' => 'div-comment', 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>
</div><!-- .reply -->
<div class="clearfix comment-author vcard">
<?php if ( 0 != $args['avatar_size'] ) echo get_avatar( $comment, $args['avatar_size'] ); ?>
<div class="comment-metadata">
<a href="<?php echo esc_url( get_comment_link( $comment->comment_ID ) ); ?>">
<time datetime="<?php comment_time( 'c' ); ?>">
<?php printf( _x( '%1$s', '1: date, 2: time', 'athemes' ), get_comment_date(), get_comment_time() ); ?>
</time>
</a>
</div><!-- .comment-metadata -->
<?php printf( __( '%s', 'athemes' ), sprintf( '<cite class="fn">%s</cite>', get_comment_author_link() ) ); ?>
</div><!-- .comment-author -->
<?php if ( '0' == $comment->comment_approved ) : ?>
<p class="comment-awaiting-moderation"><?php _e( 'Seu comentário será aprovado assim que passar pela moderação.', 'athemes' ); ?></p>
<?php endif; ?>
</footer><!-- .comment-meta -->
<div class="comment-content">
<?php comment_text(); ?>
</div><!-- .comment-content -->
</article><!-- .comment-body -->
<?php
endif;
}
endif; // ends check for athemes_comment()
if ( ! function_exists( 'athemes_the_attached_image' ) ) :
/**
* Prints the attached image with a link to the next attached image.
*/
function athemes_the_attached_image() {
$post = get_post();
$attachment_size = apply_filters( 'athemes_attachment_size', array( 1200, 1200 ) );
$next_attachment_url = wp_get_attachment_url();
/**
* Grab the IDs of all the image attachments in a gallery so we can get the
* URL of the next adjacent image in a gallery, or the first image (if
* we're looking at the last image in a gallery), or, in a gallery of one,
* just the link to that image file.
*/
$attachment_ids = get_posts( array(
'post_parent' => $post->post_parent,
'fields' => 'ids',
'numberposts' => -1,
'post_status' => 'inherit',
'post_type' => 'attachment',
'post_mime_type' => 'image',
'order' => 'ASC',
'orderby' => 'menu_order ID'
) );
// If there is more than 1 attachment in a gallery...
if ( count( $attachment_ids ) > 1 ) {
foreach ( $attachment_ids as $attachment_id ) {
if ( $attachment_id == $post->ID ) {
$next_id = current( $attachment_ids );
break;
}
}
// get the URL of the next image attachment...
if ( $next_id )
$next_attachment_url = get_attachment_link( $next_id );
// or get the URL of the first image attachment.
else
$next_attachment_url = get_attachment_link( array_shift( $attachment_ids ) );
}
printf( '%3$s',
esc_url( $next_attachment_url ),
the_title_attribute( array( 'echo' => false ) ),
wp_get_attachment_image( $post->ID, $attachment_size )
);
}
endif;
if ( ! function_exists( 'athemes_posted_on' ) ) :
/**
* Prints HTML with meta information for the current post-date/time and author.
*/
function athemes_posted_on() {
$time_string = '<time class="entry-date published" datetime="%1$s">%2$s</time>';
//if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) )
//$time_string .= '<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">%1$s</span>', 'athemes' ),
sprintf( '%3$s',
esc_url( get_permalink() ),
esc_attr( get_the_time() ),
$time_string
)
);
}
endif;
/**
* Returns true if a blog has more than 1 category
*/
function athemes_categorized_blog() {
if ( false === ( $all_the_cool_cats = get_transient( 'all_the_cool_cats' ) ) ) {
// Create an array of all the categories that are attached to posts
$all_the_cool_cats = get_categories( array(
'hide_empty' => 1,
) );
// Count the number of categories that are attached to the posts
$all_the_cool_cats = count( $all_the_cool_cats );
set_transient( 'all_the_cool_cats', $all_the_cool_cats );
}
if ( '1' != $all_the_cool_cats ) {
// This blog has more than 1 category so athemes_categorized_blog should return true
return true;
} else {
// This blog has only 1 category so athemes_categorized_blog should return false
return false;
}
}
/**
* Flush out the transients used in athemes_categorized_blog
*/
function athemes_category_transient_flusher() {
// Like, beat it. Dig?
delete_transient( 'all_the_cool_cats' );
}
add_action( 'edit_category', 'athemes_category_transient_flusher' );
add_action( 'save_post', 'athemes_category_transient_flusher' );
I am using the eList wordpress theme.
Now on the main page, there is a thumbnail display of all the categories listed. Now, on the theme you will see that they have no sub-categories under the categories, only listings
Now in my theme, I have sub-categories, and the theme does make provision for that. See my site here and the dropdown list next to the search bar in the header to see what I mean about the subcategories
Question:
How do I only list the MAIN categories on the home page, and NOT the sub-categories of the main categories as well?
Here is the code of the home page part that controls the thumbnails:
<?php
$elist_categories_args = array( 'hide_empty' => 0 );
if ( 'on' == get_option('elist_listings_hide_empty') ) $elist_categories_args['hide_empty'] = 1;
if ( is_tax() ) {
$et_term = get_queried_object();
$elist_categories_args['child_of'] = $et_term->term_id;
}
$categories = get_categories( 'taxonomy=listing-category' );
$elist_listing_categories = get_terms( 'listing_category', apply_filters( 'listing_categories_args', $elist_categories_args ) );
$elist_category_images = false !== get_option( 'elist_category_images' ) ? (array) get_option( 'elist_category_images' ) : array();
$et_count = 0;
if ( $elist_listing_categories ){ ?>
<section id="listing-categories">
<div class="container clearfix">
<h1><?php esc_html_e( 'Listing Categories', 'eList' ); ?></h1>
<?php foreach( $elist_listing_categories as $elist_listing_category ) { ?>
<?php
$et_current_term_query = new WP_Query(
array(
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'listing_category',
'field' => 'id',
'terms' => $elist_listing_category->term_id
)
)
)
);
?>
<?php $et_count++; ?>
<div class="l-category<?php if ( $et_count % 3 == 0 ) echo ' last'; ?>">
<?php $et_listing_category_link = get_term_link( $elist_listing_category ); ?>
<?php if ( isset( $elist_category_images[$elist_listing_category->term_id] ) && '' != $elist_category_images[$elist_listing_category->term_id] ) { ?>
<div class="thumb">
<a href="<?php echo esc_url( $et_listing_category_link ); ?>">
<img class="item-image" alt="<?php echo esc_attr( $elist_listing_category->name ); ?>" src="<?php echo esc_attr( et_new_thumb_resize( et_multisite_thumbnail( $elist_category_images[$elist_listing_category->term_id] ), 70, 70, '', true ) ); ?>"/>
<span class="overlay"></span>
</a>
</div> <!-- end .thumb -->
<?php } ?>
<div class="description">
<h2><?php echo esc_html( $elist_listing_category->name ); ?></h2>
<p class="info"><?php if ( 1 == $et_current_term_query->found_posts ) printf( __('%d Listing','eList'), $et_current_term_query->found_posts ); else printf( __('%d Listings'), $et_current_term_query->found_posts ); ?></p>
<?php if ( '' != $elist_listing_category->description ) { ?>
<p><?php echo esc_html( $elist_listing_category->description ); ?></p>
<?php } ?>
</div> <!-- end .description -->
</div> <!-- end .l-category -->
<?php } ?>
</div> <!-- end .container -->
</section> <!-- end #listing-categories -->
<?php } ?>
To pinpoint out of the above code, here is the html part that holds the dynamically populated thumbnail:
<div class="l-category<?php if ( $et_count % 3 == 0 ) echo ' last'; ?>">
<?php $et_listing_category_link = get_term_link( $elist_listing_category ); ?>
<?php if ( isset( $elist_category_images[$elist_listing_category->term_id] ) && '' != $elist_category_images[$elist_listing_category->term_id] ) { ?>
<div class="thumb">
<a href="<?php echo esc_url( $et_listing_category_link ); ?>">
<img class="item-image" alt="<?php echo esc_attr( $elist_listing_category->name ); ?>" src="<?php echo esc_attr( et_new_thumb_resize( et_multisite_thumbnail( $elist_category_images[$elist_listing_category->term_id] ), 70, 70, '', true ) ); ?>"/>
<span class="overlay"></span>
</a>
</div> <!-- end .thumb -->
<?php } ?>
<div class="description">
<h2><?php echo esc_html( $elist_listing_category->name ); ?></h2>
<p class="info"><?php if ( 1 == $et_current_term_query->found_posts ) printf( __('%d Listing','eList'), $et_current_term_query->found_posts ); else printf( __('%d Listings'), $et_current_term_query->found_posts ); ?></p>
<?php if ( '' != $elist_listing_category->description ) { ?>
<p><?php echo esc_html( $elist_listing_category->description ); ?></p>
<?php } ?>
</div> <!-- end .description -->
</div> <!-- end .l-category -->
Given the code presented, there seem to be two possibilities.
1) Modify WP_Query arguments
$et_current_term_query = new WP_Query(
array(
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'listing_category',
'field' => 'id',
'terms' => $elist_listing_category->term_id,
'include_children' => false // <--- ADD THIS
)
)
)
);
2) Modify the listing terms call used with get_terms
$elist_categories_args = array( 'hide_empty' => 0, 'parent' => 0 ); // ADD parent
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..