change the resualts page - on "no resualt" woocomerce page - php

Can anyone guide me trow the files that need to update in order to get
no result found
on search redirect to other page or different text (like leading into fill form)?
I am using wp woocomerce and woof search plug in. I found on the search.php that gives me a hit.
</div>
<?php graphene_posts_nav(); ?>
<?php else : ?>
<?php get_template_part( 'loop', 'not-found' ); ?>
<?php endif;
}

Yes you need to use the if else condition based on the found posts like below.
<?php if ( have_posts() ) : ?>
//display search template
<?php else : ?>
//display no results template
<?php get_template_part( 'no-results', 'search' ); ?>
<?php endif; ?>
In this case no results template will be no-results-search.php

Related

This custom wordpress loop is giving strange results. Can someone please correct it?

The following query is expected to display four posts of category number 28. What it is doing us that it's showing three posts of category number 28 and one post of category number 32. I have no idea what is wrong with the code.
<?php $CstmLoop = new WP_Query(
array('cat=28','posts_per_page' => 4)
); ?>
<?php if ( $CstmLoop->have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( $CstmLoop->have_posts() ) :$CstmLoop->the_post(); ?>
<?php
get_template_part( 'template-parts/content', get_post_format());
?>
<?php endwhile; ?>
<?php else : ?>
<?php get_template_part( 'template-parts/content', 'none' ); ?>
<?php endif; ?>
<?php WP_reset_postdata(); ?>
But here is the interesting part - if I do not use an array and simply pass 'cat=28' as the only argument, it displays all the posts of category 28.
You can try as below.
array('cat' => 28,'posts_per_page' => 4)
As your WP_Query argument.
This is the difference between providing a query string as cat=28 directly to the query and adding a complete array with query_options so to say.

Fix for wordpress loop

I am new too wordpress and have one problem. I finaly understand post loop and it work well but have one problem. For example: I have two pages of my posts (all are 6). In one page i have five posts (excerpt only) and on secound page i have one post only and in this case (when on one of my pages i have only one post it change to content post but it need to stay excerpt). So my question is what can i change in my code to make it work well in this case when on some page left only one post?
It's my loop:
<?php if (have_posts()) : ?>
<?php if (($wp_query->post_count) > 1) : ?>
<?php while (have_posts()) : the_post(); ?>
<!-- Do your post header stuff here for excerpts-->
<?php the_excerpt() ?>
<!-- Do your post footer stuff here for excerpts-->
<?php endwhile; ?>
<?php else : ?>
<?php while (have_posts()) : the_post(); ?>
<!-- Do your post header stuff here for single post-->
<?php the_content() ?>
<!-- Do your post footer stuff here for single post-->
<?php endwhile; ?>
<?php endif; ?>
<?php else : ?>
<!-- Stuff to do if there are no posts-->
<?php endif; ?>
Use the posts_per_page parameter to an array and start the loop.
$args = array( 'post_type' => 'post', 'posts_per_page' => 4,'category' => 2, 'paged' => $paged );
$wp_query = new WP_Query($args);
while ( have_posts() ) : the_post();
$content = get_the_content();
echo $content;
endwhile;
Here I have printed the 4 contents from the archive. So the page will only have 4 posts in it. You can change the value based on your need.
I have used and got the output. Hope this helps you too.
Ur second if condition should be inside while and while shuld be called once ..
Somthing like
If
While
Then if for count
Elseif
Else
End while
End if
I hope it wil help ..
Answer for all: $wp_query->post_count need to be replaced by $wp_query->found_posts and everything working fine now!

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/

Override default setting for category archive output (in twentytwelve)

I have this snippet in a custom category template which outputs the last X posts of that category.
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); get_template_part( 'content', get_post_format() ); endwhile; twentytwelve_content_nav( 'nav-below' ); ?>
<?php else : ?>
<?php get_template_part( 'content', 'none' ); ?>
<?php endif; ?>
Problem is this outputs the posts as excerpts.. How can I change it to the_content() without affecting all the other loops? I only need to have the_content here.
Do I need to manually output the title, then the date, then author and then the_content() or can I just replace something in the snippet above ?
From what you have given, it seems that you have customized the content.php template.
To show the content just for your category page, look, you'll need to add that condition. As you did not add the code for content.php, I can't exactly say where to add it, but you have to do somthing like this
if(is_category()) {
the_content();
}else{
the_excerpt();
}

PHP/WordPress: Custom Field Value in Loop returns Array instead of Single Result

For the search results page in WordPress Im making a custom template. And in that custom template I want to display custom meta field values.
However, when I do this:
<?php if ( have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php
$pName = get_post_meta($post->ID, $productName, true);
echo $pName;
);
?>
<?php endwhile; ?>
<?php else : ?>
<?php get_template_part( 'content', 'none' ); ?>
<?php endif;
The result I get is not the value of the custom field but:
Array
When I var_dump the $pName it shows all the right custom field content.
Question: Why am I getting an array as the result (when Ive told it its a single result with 'true') and how do I fix it so it displays the proper content?
Thanks!

Categories