How to strip HTML from WooCommcerce Archive Description - php

In WooCommerce, my theme displays descriptions for categories, archives, etc. I am trying to strip HTML from one of them, which is the WooCommerce Archive Description. The description is called with:
<?php endif; ?>
<?php do_action( 'woocommerce_archive_description' ); ?>
<?php if ( have_posts() ) : ?>
I need to apply the wp_strip_all_tags function to the do_action... is that possible? I know this is wrong, but something like:
<?php do_action( wp_strip_all_tags('woocommerce_archive_description') ); ?>

You need to strip the tags in each of the functions attached to that action. Search your theme and plugins to find the functions. For instance, in woocommerce 2.0.20 there are two - woocommerce_taxonomy_archive_description and woocommerce_product_archive_description. Since those function declarations have a check to see if they exist, you could override them by adding your own with the same name in the theme.

What you could do is to replace:
<?php do_action( 'woocommerce_archive_description' ); ?>
with:
<?php echo $queried_object->description; ?>
I prefer to do it like this:
<?php echo '<h2 class="term-description">' . $queried_object->description . '</h2>'; ?>
Because I want to get rid of the paragraph tags surrounding the description and I want to replace the DIV whose class is "term-description" with an H2 and I'm fine to retain the original class.
Enjoy!
Kind Regards,
Mihai Bocsaru

Related

WordPress change search results

I have got the problem that my WordPress theme ("Sydney") is displaying the search results wrong. Instead of just listing posts I want to display WooCommerce products only.
Those should be ordered in a nice grid as shown in this picture:
.
At the moment the results are listed like this
.
How can I change the way the search results are displayed?
At the moment my search.php is looking like this:
<?php get_header(); ?>
<div id="primary" class="content-area col-md-9">
<main id="main" class="post-wrap" role="main">
<?php if ( have_posts() ) : ?>
<header class="page-header">
<h3><?php printf( __( 'Search Results for: %s', 'sydney' ), '<span>' . get_search_query() . '</span>' ); ?></h31>
</h3>
</header><!-- .page-header -->
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php
/**
* Run the loop for the search to output the results.
* If you want to overload this in a child theme then include a file
* called content-search.php and that will be used instead.
*/
get_template_part( 'content', 'search' );
?>
<?php endwhile; ?>
<?php the_posts_navigation(); ?>
<?php else : ?>
<?php get_template_part( 'content', 'none' ); ?>
<?php endif; ?>
</main><!-- #main -->
</div><!-- #primary -->
Thank you guys a lot!
** UPDATE
The solution provided by kashalo partly worked, but search results still look slightly different from the product page and are listed in only one column instead of a grid.
** UPDATE
The solution suggested by Alexander Wigmore looks almost the way I wanted it so look like. The only Problem with copying the page.phpin the search.phpis that the products are still getting displayed wheird, but not when they are displayed in the category they fit in. For example: When searching for saat the results are displaying the products at first with text only, but normaly under the Saatgut category.
my advise is not to directly modify the search file of wp or theme template but is a good practice to work with the child theme. For the design display of the result you can play a little with css to customize it as you want, instead for the results to show only the products you can use any filter builded on the theme you are using (if it have) or create yourself a function to filter the results (also called hooks).
An example for filtering results is like the above code:
add_action( 'pre_get_posts', 'filter_woocommerce_products' ); // the hook
function filter_woocommerce_products( $query ) { // the function
if( ! is_admin() && is_search() && $query->is_main_query() ) {
// verify if can use the search function and the search string is from the search call
$query->set( 'post_type', 'product' ); // filter the posts with type products that corrispond to woocommerce products.
}
}
and this function must be saved on the function.php file of your theme or the child theme.
For more information i suggest you to read more about child theme and the pre_get_posts documentation on the wp page.
Hope it helps and feel free to ask.
You'll want to modify the way search is handled by Wordpress.
This can easily be done by adding an "action", add the below code to your functions.php file.
function searchfilter($query) {
if ($query->is_search && !is_admin() ) {
$query->set('post_type',array('product'));
}
return $query;
}
add_filter('pre_get_posts','searchfilter');
This will then make the search page only show products. It's worth noting that you can add other post types back in via the array in this line: $query->set('post_type',array('product'));.

How to add a custom block as a usual "product" to shop page / archive-product.php

I'm looking for a possibility to add a custom html block/post to the woocommerce "shop page" inside the products grid, as a product.
What I mean.. I have a grid of products on the "shop" page (archive-product) and I want to create a special post/page/html block with some text information, that will be inserted into the products grid as a one of "product", but with no price, with no title and unclickable. I've attached the screenshot of the final result I want to have, it's really self explaining - here it is exactly what I'm looking for.
As an idea probably I can create a special product with specific slug or title and the corresponding script with pre_get_posts hook will find this post/product and modify it to look like I need. I'm looking for some code/ideas how to insert this specific block/page/post into the archive-product page on some position in the grid. Thanks!
Thanks for help, guys! I've implemented the functionality I was looking for. I've found the corresponding loop in archive-product.php and as was suggested by JapanGuy, I've added a simple "if i equal let's say 5 then echo < li>[Custom block]< /li>" .
The original snippet from archive-product.php:
<?php woocommerce_product_loop_start(); ?>
<?php woocommerce_product_subcategories(); ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php wc_get_template_part( 'content', 'product' ); ?>
<?php endwhile; // end of the loop. ?>
<?php woocommerce_product_loop_end(); ?>
Modified code with inserted custom block:
<?php woocommerce_product_loop_start(); ?>
<?php woocommerce_product_subcategories(); ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php
if ($i == 5) {
echo "<li>[Custom block]</li>";
}
$i++;
?>
<?php wc_get_template_part( 'content', 'product' ); ?>
<?php endwhile; // end of the loop. ?>
<?php woocommerce_product_loop_end(); ?>
Im such simple way I can add any content to the created [Custom block] and have an usual products grid with extra custom designed block. I'm not very experienced programmer, so probably my code is not perfect, but it works. Thanks!
Edit: Previous code was wrong, changed it here
$i=0;
while ($row = mysqli_fetch_array($query))
{
if ($i == 2) {
echo "Cusom block";
}
echo "<p> Product block " . $row['column'] . " </p>";
$i++;
}
Creating WordPress Custom Post Archives : Hope this meets your requirement.
Custom Post Archives list your custom content. You probably already know the standard WordPress archives. So you can follow this to display both together .
Ref here : https://wp-types.com/documentation/user-guides/creating-wordpress-custom-post-archives/

Genesis Framework Hooks

Is there any possible way to change the loop structure of the Genesis Framework so that the loop header appears within the same div as the entry content?
I have tried messing around with the loop.php file in the genesis template and I have also tried looking up different hooks, but I can't seem to find anything that helps.
Is there anyone that has accomplished this or would have an idea?
To customize the loop in Genesis, before you need to remove Genesis Loop:
remove_action( 'genesis_loop', 'genesis_do_loop' );
then you can add your custom loop
add_action( 'genesis_loop', 'my_custom_loop' );
Here is a basic example for 'standard-looking' loop working in Genesis:
function my_custom_loop() {
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
// Your Code Here
<?php endwhile; ?>
<?php endif; ?>
<?php
}
genesis();
?>
Please note that your function must end with 'genesis();' to work!
So the whole page code can be:
<?php
/*
* Your Custom Page
*/
?>
<?php
remove_action('genesis_loop', 'genesis_do_loop');
add_action('genesis_loop', 'my_custom_loop');
function my_custom_loop() {
?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
// Your Code
<?php endwhile; ?>
<?php endif; ?>
<?php
}
genesis();
?>
Hope it helps ;)
Here is simple approach.
You first remove the entry header stuff from the header.
e.g.
remove_action( 'genesis_entry_header', 'genesis_entry_header_markup_open', 5 );
remove_action( 'genesis_entry_header', 'genesis_do_post_title' );
remove_action( 'genesis_entry_header', 'genesis_post_info', 12 );
remove_action( 'genesis_entry_header', 'genesis_entry_header_markup_close', 15 );
Once the header markup as well as entry title and entry info is removed from top.
You can add them back into your page in your desired position i.e. genesis_entry_content hook.
e.g.
add_action( 'genesis_entry_content', 'genesis_do_post_title' );
Hope this helps. Let me know if you face any issue.
Sorry forgot to add that you will need to add this custom code in your child theme's functions.php file if you want it to impact to whole site. And if you want this to happen on certain template, then in that template file in the child theme of Genesis.
I list all the most common Genesis hooks and filters used by me while developing a website without boring you with more contents.
https://icodefy.com/common-genesis-hooks-filters/

Wordpress: echo a dynamic_sidebar widget inside of a shortcode

I'm trying to insert a widget into a shortcode that I'm storing in my footer.php file...
<?php
echo do_shortcode( '[accordion_full_width title="upcoming events" full_width="yes" background_color="#68676b"]' .
dynamic_sidebar( 'footer_contents' )
. '[/accordion_full_width]'); ?>
...and it renders out the widget first, followed by the shortcode.
If this is a problem with using the dynamic_sidebar function inside of the do_shortcode, I'd think it just wouldn't spit the widget out, but it does. In the wrong place.
Does anybody have anything?
Add this Code in Your Template
<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Name of Widget') ) : ?>
<?php endif; ?>

shortcode not comging through on wordpress template

so I'm using this awesome plugin "anything slider" and have successfully integrated into the template. however, it's bringing in all slides instead of just a category. This is the shortcode I'm using... which brings in all slides...
<?php echo do_shortcode( "[anything_slides cat="ur"]" ); ?>
The shortcode to bring in a category works within the wysiqyg editor like this:
[anything_slides cat="ur"]
Can someone possibly help?
http://wordpress.org/plugins/anythingslider-for-wordpress/
I think that the "double-quote" have to be escaped:
<?php echo do_shortcode( "[anything_slides cat=\"ur\"]" ); ?>
or
<?php echo do_shortcode( '[anything_slides cat="ur"]' ); ?>
Hope it helps!

Categories