Woocommerce search.php - wordpress

How can I add price to 'search.php' in Woocommerce?
Current code is:
<?php
get_header();
global $wp_query;
?>
<div class="wapper">
<div class="contentarea clearfix">
<div class="content">
<h1 class="search-title"> <?php echo $wp_query->found_posts; ?>
<?php _e( 'Search Results Found For', 'locale' ); ?>: "<?php the_search_query(); ?>" </h1>
<?php if ( have_posts() ) { ?>
<ul>
<?php while ( have_posts() ) { the_post(); ?
<hr width=“20%”>
<h3><b><a href="<?php echo get_permalink(); ?>"></b>
<?php the_title(); ?>
</a></h3>
<?php the_post_thumbnail( 'shop_thumbnail' ) ?>
<br/>
<div class="h-readmore"> <I>Click to see full details</I></div>
</hr width=“20%”>
<?php } ?>
</ul>
<?php paginate_links(); ?>
<?php } ?>
</div>
</div>
</div>
<?php
do_action( 'storefront_sidebar' );
get_footer();
This also has the sidebar at the bottom and not the side - so if anyone can advise how to have the sidebar on the LEFT that too would help.

<?php
get_header();
global $wp_query;
?>
<div class="wapper">
<div class="contentarea clearfix">
<div class="content">
<h1 class="search-title"> <?php echo $wp_query->found_posts; ?>
<?php _e( 'Search Results Found For', 'locale' ); ?>: "<?php the_search_query(); ?>" </h1>
<?php if ( have_posts() ) { ?>
<ul>
<?php while ( have_posts() ) { the_post(); ?>
<hr width=“20%”>
<h3><b><a href="<?php echo get_permalink(); ?>"></b>
<?php the_title(); ?>
</a></h3>
<?php the_post_thumbnail( 'shop_thumbnail' ) ?>
<br/>
<p itemprop="price" class="price"><?php echo $product->get_price_html(); ?></p>
<div class="h-readmore"> <I>Click to see full details</I></div>
</hr width=“20%”>
<?php } ?>
</ul>
<?php paginate_links(); ?>
<?php } ?>
</div>
</div>
</div>
<?php
do_action( 'storefront_sidebar' );
get_footer();
update you search.php with above code the price can we call with this <p itemprop="price" class="price"><?php echo $product->get_price_html(); ?></p>

Related

Regular posts first (descending order), then custom post type (alphabetical) in WP search

I've been cobbling together bits and pieces of various examples together but can't seem to wrap my head around this.
I have regular blog posts (News) that I would like displayed with the most recent on top followed by a custom post type (Businesses) that I would like grouped below the News. I am using Sage and this is my first theme.
Here is my beginner code so far:
<?php get_template_part('templates/page', 'header'); ?>
<?php if (!have_posts()) : ?>
<div class="alert alert-warning">
<?php _e('Sorry, no results were found.', 'sage'); ?>
</div>
<?php get_search_form(); ?>
<?php endif; ?>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php $post_type = get_post_type_object( get_post_type() ); ?>
<?php $type = get_post_type(); ?>
<?php if ($type == 'post') { ?>
<h2>News Results</h2>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<div>
<a href="<?php the_permalink(); ?>">
<?php $cats=get_the_category(); ?>
<?php echo $cats[0]->cat_name; ?>
</a>
</div>
<div>
<h3><?php the_title(); ?></h3>
<div class="result-excerpt">
<?php if ( has_excerpt( $post->ID ) ) {
echo the_excerpt();
} else {
echo get_excerpt();
} ?>
</div>
</div>
</article>
<?php } elseif ($type == 'business') { ?>
<h2>Business Results</h2>
<article <?php post_class(); ?>>
<header>
<h2 class="entry-title"><?php the_title(); ?></h2>
<?php if (get_post_type() === 'post') { get_template_part('templates/entry-meta'); } ?>
</header>
<div class="entry-summary">
<?php the_excerpt(); ?>
</div>
<a href="<?php the_permalink(); ?>">
<?php $cats=get_the_category(); ?>
<?php echo $cats[0]->cat_name; ?> -
<?php the_title(); ?>
</a>
<h3><?php echo the_sub_field('title'); ?></h3>
<?php if( get_sub_field('content') ): ?>
<div class="result-excerpt">
<?php echo custom_field_excerpt(); ?>
</div>
<?php endif; ?>
</article>
<?php } ?>
<?php endwhile; ?>
<?php endif; ?>
I think the problem is that you're looping over every post, in order, and deciding what to do with it. This will result in the output, whatever it is, in the same order as the input.
I don't speak very fluent PHP, but I think what's happening is that the algorithm reads like this:
-for each post:
- if it's a 'post', display it like «this»
- otherwise, if it's a 'business', display it instead like «this»
I think you want to have two loops, like this:
-for each post:
- if it's a 'post', display it like «this»
-for each post:
- if it's a 'business', display it like «this»
Unfortunately, I don't see an obvious reference to store in an array and loop over. I really don't know how sage works, so I'll have to leave the implementation to you. My best guess - and I sincerely doubt this will work - is as follows:
<?php get_template_part('templates/page', 'header'); ?>
<?php if (!have_posts()) : ?>
<div class="alert alert-warning">
<?php _e('Sorry, no results were found.', 'sage'); ?>
</div>
<?php get_search_form(); ?>
<?php endif; ?>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php $post_type = get_post_type_object( get_post_type() ); ?>
<?php $type = get_post_type(); ?>
<?php if ($type == 'post') { ?>
<h2>News Results</h2>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<div>
<a href="<?php the_permalink(); ?>">
<?php $cats=get_the_category(); ?>
<?php echo $cats[0]->cat_name; ?>
</a>
</div>
<div>
<h3><?php the_title(); ?></h3>
<div class="result-excerpt">
<?php if ( has_excerpt( $post->ID ) ) {
echo the_excerpt();
} else {
echo get_excerpt();
} ?>
</div>
</div>
</article>
<?php endwhile; ?>
<?php endif; ?>
<?php if (!have_posts()) : ?>
<div class="alert alert-warning">
<?php _e('Sorry, no results were found.', 'sage'); ?>
</div>
<?php get_search_form(); ?>
<?php endif; ?>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php $post_type = get_post_type_object( get_post_type() ); ?>
<?php $type = get_post_type(); ?>
<?php if ($type == 'business') { ?>
<h2>Business Results</h2>
<article <?php post_class(); ?>>
<header>
<h2 class="entry-title"><?php the_title(); ?></h2>
<?php if (get_post_type() === 'post') { get_template_part('templates/entry-meta'); } ?>
</header>
<div class="entry-summary">
<?php the_excerpt(); ?>
</div>
<a href="<?php the_permalink(); ?>">
<?php $cats=get_the_category(); ?>
<?php echo $cats[0]->cat_name; ?> -
<?php the_title(); ?>
</a>
<h3><?php echo the_sub_field('title'); ?></h3>
<?php if( get_sub_field('content') ): ?>
<div class="result-excerpt">
<?php echo custom_field_excerpt(); ?>
</div>
<?php endif; ?>
</article>
<?php } ?>
<?php endwhile; ?>
<?php endif; ?>
Good luck!

Get Wordpress to alternate displaying 2 and 1 posts in a row

I am trying to code my homepage to have 2 posts in a row, and then the next row have one post, next row 2 posts, and so on.
I have tried using this article, however every time I try I just get glitches.
https://perishablepress.com/two-column-horizontal-sequence-wordpress-post-order/
If anyone has any coding solutions I would really appreciate it.
This is my current index.php file
<?php
get_header();
if (have_posts()) :
while (have_posts()) :
$i++; if(($i % 2) == 0) : $wp_query->next_post(); else :
the_post(); ?>
<article class="post">
<div id="left-column">
<h2><?php the_title(); ?></h2>
<p>
<?php echo get_the_excerpt(); ?>
<a class="moretext" href="<?php the_permalink(); ?>">Read more</a>
</p>
</div>
</article>
<?php endif; endwhile; else: ?>
<div>Alternate content</div>
<?php endif; ?>
<?php $i = 0; rewind_posts(); ?>
<?php if (have_posts()) :
while (have_posts()) :
$i++; if(($i % 2) !== 0) : $wp_query->next_post(); else :
the_post(); ?>
<article class="post">
<div id="right-column">
<h2><?php the_title(); ?></h2>
<p>
<?php echo get_the_excerpt(); ?>
<a class="moretext" href="<?php the_permalink(); ?>">Read more</a>
</p>
</div>
</article>
<?php endif; endwhile; else: ?>
<div>Alternate content</div>
<?php endif;
get_footer();
?>
Each time you call the_post(); the post index is advanced and the next post data becomes in scope.
Create a new template file in your theme and add a new page using that template;
<?php
/*
* Template Name: page-2-col
*/
get_header();
$i = 0;
$args = array(
'posts_per_page' => 5,
'paged' => 1
);
$the_query = new WP_Query($args);
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
if( $i %2 == 1 ) {
$the_query->the_post(); ?>
<article class="post col-md-12">
<h2><?php the_title(); ?></h2>
<p>
<?php echo get_the_excerpt(); ?>
<a class="moretext" href="<?php the_permalink(); ?>">Read more</a>
</p>
</article>
<?php
}
else {
$the_query->the_post(); ?>
<article class="post col-md-6">
<h2><?php the_title(); ?></h2>
<p>
<?php echo get_the_excerpt(); ?>
<a class="moretext" href="<?php the_permalink(); ?>">Read more</a>
</p>
</article>
<?php $the_query->the_post(); ?>
<article class="post col-md-6">
<h2><?php the_title(); ?></h2>
<p>
<?php echo get_the_excerpt(); ?>
<a class="moretext" href="<?php the_permalink(); ?>">Read more</a>
</p>
</article>
<?php
}
?>
<?php
$i++;
}
}
else {
echo '<p>Sorry, no posts matched your criteria.</p>';
}
get_footer();
To add bootstrap, in your functions.php;
function learningWordPress_resources() {
wp_enqueue_style('style', get_stylesheet_uri());
wp_enqueue_script( 'bootstrap-js', 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css', array('jquery'), '3.3.7', true );
wp_enqueue_style( 'bootstrap-style', 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css' );
}
add_action('wp_enqueue_scripts', 'learningWordPress_resources');

wordpress search page don't give search results

I made my own search form page and search page, But know when I search for something the search result gives back nothing I searched alot on the web and following some tutorials to find the issue but I can't. Her's the code:
searchform.php:
<form role="search" method="get" class="visible-lg search-form arrow_box search-back navbar-form navbar-right" action="<?php echo home_url( '/' ); ?>">
<div class="form-group">
<input type="search" class="search-field form-control" size="49" placeholder="بگەڕێ" value="" name="s" title="بگەڕێ" />
</div>
<input type="submit" class="search-submit search-button btn btn-default" value="بگەڕێ" />
</form>
search.php:
<?php
/*
Template Name: Search Page
*/
?>
<?php get_header(); ?>
<div class="container">
<div class="row">
<div class="col-md-5 news">
<?php if ( have_posts() ) : ?>
<?php
global $wp_query;
$total_results = $wp_query->found_posts;
?>
<?php printf( __( 'ئەنجامەکانی گەڕان بۆ: %s', 'shape' ), '<span>' . get_search_query() . '</span>' ); ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', 'search' ); ?>
<?php endwhile; ?>
<?php else : ?>
<?php get_template_part( 'no-results', 'search' ); ?>
<?php endif; ?>
</div>
<?php get_sidebar(); ?>
</div>
</div>
<?php get_footer(); ?>
After I tried many codes I finally found that I have to add the title or the content after the while loop, I don't know why but this is the solution I just changed the search.php:
<?php
/*
Template Name: Search Page
*/
?>
<?php get_header(); ?>
<div class="container">
<div class="row">
<div class="col-md-5 news">
<?php if ( have_posts() ) : ?>
<?php
global $wp_query;
$total_results = $wp_query->found_posts;
?>
<?php printf( __( 'serch results for: %s', 'shape' ), '<span>' . get_search_query() . '</span>' ); ?>
<br/><br/>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<h4 class=""><?php the_title(); ?></h4>
<div class="naskh"><?php the_excerpt(); ?></div>
<?php endwhile; ?>
<?php else : ?>
<h4>Nothing found try something else.</h4>
<?php endif; ?>
</div>
<?php get_sidebar(); ?>
</div>
</div>
<?php get_footer(); ?>

get post thumbnail image in wordpress and display in 3 coulmn

I am trying to display one particular category posts as a three column layout The problem I have is I am not sure how I can use for or forwach to loop the display of each post thumbnail so whn it comes more than three I can use one_thrid_last. css class.
<?php while ( have_posts() ) : the_post(); ?>
<?php if (is_category('actress-gallery') ):?>
<?php if ( function_exists("has_post_thumbnail") && has_post_thumbnail() ) : ?>
<div class="one_fourth_last">
<a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'tie' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark">
<?php echo $image_url = wp_get_attachment_image( get_post_thumbnail_id($post->ID) , 'thumbnail' ); ?>
<h2><?php the_title(); ?></h2>
</a>
</div><!-- post-thumbnail /-->
<?php endif; ?>
<div class="wrapper" style="width:800px; height:auto;">
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php if ( has_post_thumbnail() ) { ?>
<div class="image-wrapper" style=" width:250px; height:300px;" >
<?php the_title();?>
<?php the_content();?>
<?php the_post_thumbnail(); ?>
</div>
<?php } endwhile; endif; ?>
</div>
<div class="wrapper" style="width:750px; height:700px;">
<?php while (have_posts()) : the_post(); ?>
<?php if ( has_post_thumbnail() ) { ?>
<div style="width:250px; height:300px;" >
<?php the_title();?>
<?php the_post_thumbnail(); ?>
</div>
<?php } endif; ?>
</div>
You can use a $count variable and check for the 3rd thumbnail.
<?php if ( have_posts() ) : ?>
<?php $count = 1; ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php if ( has_post_thumbnail() ) : ?>
<?php if ($count % 3 == 0) : ?>
<div class = "one_third_last">
<?php else : ?>
<div class = "other_class">
<?php endif; ?>
<?php the_title();?>
<?php the_content();?>
<?php the_post_thumbnail(); ?>
</div>
<?php endif; ?>
<?php $count++; ?>
<?php endwhile; ?>
<?php endif; ?>

How to call Custom Field in Wordpress Template

I want to call custom field in single.php template to complete a short code and thus show a menu.
I places the below code but it is not working
<?php echo do_shortcode("[custommenu menu=<?php echo get_post_meta($post->ID, ‘tabmenu’, true); ?>]?>")
Please help
Here's my template file
I saved this as single-default.php and calling it category wise from single.php file
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div id="content">
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<h1 class="entry-title"><?php the_title(); ?></h1>
<div class="tab-menu"><?php echo do_shortcode("[custommenu menu='".get_post_meta($post->ID, ‘tabmenu’, true)."']"); ?>
</div>
<div class="entry-content">
<?php if(get_option('resizable_integrate_singletop_enable') == 'on') echo (get_option('resizable_integration_single_top')); ?>
<?php the_content(''); ?>
<?php wp_link_pages( array( 'before' => '<div class="page-link">' . __( 'Pages:', 'themejunkie' ), 'after' => '</div>' ) ); ?>
<?php if(get_option('resizable_integrate_singlebottom_enable') == 'on') echo (get_option('resizable_integration_single_bottom')); ?>
<div class="clear"></div>
<?php printf(the_tags(__('<div class="entry-tags"><span>Tags:</span> ','themejunkie'),', ','</div>')); ?>
<?php edit_post_link('('.__('Edit', 'themejunkie').')', '<span class="entry-edit">', '</span>'); ?>
</div><!-- .entry-content -->
</div><!-- #post-<?php the_ID(); ?> -->
<div class="clear"></div>
<div class="entry-bottom">
<div class="clear"></div>
</div><!-- .entry-bottom -->
</div><!-- #content -->
<?php endwhile; else: ?>
<?php endif; ?>
You need to concatenate the inner echo in the shortcode
<?php echo do_shortcode("[custommenu menu='".get_post_meta($post->ID, ‘tabmenu’, true)."']"); ?>

Resources