Dear fellow coders and community,
I have really researched this issue for quite a while now, and though I have some coding background, I can't seem to figure out the issue. No links to the topic helped since they all only suggest the code snippet I am using already.
I have a working Word Press installation : Version 4.8.2 & WooCommerce : Version 3.3.5
My Theme consists of essentially 2 Files, since I had to breakdown the whole thing in order to find the issue.
The functions.php
<?php
add_filter('loop_shop_columns', 'loop_columns');
if (!function_exists('loop_columns')) {
function loop_columns() {
return 5; // 3 products per row
}
}
add_filter( 'loop_shop_per_page', 'new_loop_shop_per_page', 999999999999 );
function new_loop_shop_per_page( $cols ) {
// $cols contains the current number of products per page based on the value stored on Options -> Reading
// Return the number of products you wanna show per page.
//
var_dump($cols);
$cols = 3;
return $cols;
}
?>
The code is just supposed to limit the amount of products displayed on the product category page to 3.
And the index.php
<?php
get_header();
?>
<div class="container content pr-0 pl-0" >
<div class="index">
<?php if ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endif; ?>
</div>
</div>
<?php
get_footer();
?>
This is just the essential index file needed by any wordpress installation.
Now I tried changing backend settings already with the default 2017 theme and it worked just fine...so it cannot be some backend thing.
What could be the issue here?
Any help or leads will be much appreciated! Thanks!
The loop_shop_columns filter doesn't effect the number of posts shown, just the number of columns on the page (number of products going across before a new line is started).
The number of products shown on any page is actually taken from the WordPress Settings > Reading "Blog pages show at most" setting.
You can also change the number programatically by using the pre_get_posts filter.
add_action( 'pre_get_posts', 'rc_modify_query_exclude_category' );
// Create a function to excplude some categories from the main query
function rc_modify_query_exclude_category( $query ) {
// Check if on frontend and main query is modified
if ( ! is_admin() ) {
$query->set( 'posts_per_page', '-1' );
} // end if
}
Related
In the theme there are two files:
single.php
f-template.php → For default post type. It has different designs.
Home.php will fetch first 10 posts, and they can be among any of the above templates.
But, what is needed is when the content on home.php is coming from f-template.php
then these two things should be implemented on home.php
function folder_paragrapgh($content){
return preg_replace('/<p([^>]+)?>/', '<p class="para para2">', $content);
}
add_filter('the_content', 'folder_paragrapgh');
and
<script>
(function($) {
// do all your $ based jquery in here.
$(function() {
$('p.class2').prepend('<img src="http:/sample.com/img/folder.svg" width="50" height="25" alt="">');
});
})(jQuery);
</script>
I tried this:
if( is_page_template( 'f-template.php' ) ) {
function folder_paragrapgh($content){
return preg_replace('/<p([^>]+)?>/', '<p class="para para2">', $content);
}
add_filter('the_content', 'folder_paragrapgh');
}
the_content();
}
But this didn't work. actually it is flawed because the template that we are dealing is home.php.
So do we have any solution to achive what we wanted to achieve?
Correct me if I'm wrong but it sounds like you want to display a loop with posts where some posts have a different design depending on the Page Template you have selected for it.
You can check which template is being used with the get_page_template_slug() function. Use it inside the loop along with the ID of the post.
// checks if there are any posts that match the query
if (have_posts()) :
// If there are posts matching the query then start the loop
while ( have_posts() ) : the_post();
// Assign postId from within the loop
$postId = the_ID();
if( get_page_template_slug($postId) === 'f-template' ) {
// Display what you want to see when f-template is selected.
} else {
// Display what you want to see by default if no condition is met.
}
// Stop the loop when all posts are displayed
endwhile;
// If no posts were found
else :
echo '<p>Sorry no posts matched your criteria.</p>';
endif;
The is_page_template() function won't work because it will check what the page template for the current page in the Main Query is.
It all depends on your use case but personally I would have added an extra field using Advanced Custom Fields for this effect.
Good luck!
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'));.
My Woocommerce website shows 3 columns of products in every loop page.
I'm making some custom landing page where i want to show the loop of products in 4 column instead of 3.
I tryed with this code inside function.php:
add_filter('loop_shop_columns', 'loop_columns');
if (!function_exists('loop_columns')); {
function loop_columns() {
if (is_page(91040)){
return 4;
}else{
return 3;
}
}
}
without success.
Is there a way to have in a single or specific page a different number of column compared to the others?
If you are using woothemes theme or any other theme which is already utlisiing the loop_columns filter, then you need to override that with two things, by removing the if !function exit condition, and also adding pririty to filter high enough like 999 so it is applied at the last:
e.g.
// Override theme default specification for product # per row
function loop_columns() {
return 5; // 5 products per row
}
add_filter('loop_shop_columns', 'loop_columns', 999);
You may adjust the function as per you needs like adding page template conditional as you have in your original code.
Source:
https://docs.woothemes.com/document/change-number-of-products-per-row/ Second headline
I solved the issue. The problem was in the is_page function. It can't get the correct ID so i have found a workaround:
function loop_columns() {
global $woocommerce;
$url = explode('?', 'http://'.$_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]);
$ID = url_to_postid($url[0]);
if ($ID == 91040){
return 4;
}else{
return 3;
}
}
add_filter('loop_shop_columns', 'loop_columns', 999);
I spent a lot of brain matter trying to control the number of columns in a custom template. I am new to WooCommerce and Wordpress so am on a learngin curve. However, I started off with:
<ul class="products">
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => 12
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
?>
</ul><!--/.products-->
When applied as a template it gave me a custom loop that I could fiddle with. However, from there I wanted 5 columns and just could not figure it out. I tried all the things Google could throw up such as filers and such. However, I finally discovered that the above snippet starts off with the
<ul class="products">
tag. All I did was dd the appropriate colum class:
<ul class="products columns-5">
This might require additional thinking regarding the responsive aspects, but it crosses the immediate barrier of controlling columns with that snippet. However, it does actually seem to respond quite well.
I am developing locally on MAMP using "Custom Post Types" and "Custom Fields" plugins. I have a custom post type of "Products" and a custom taxonomy of "Types."
I'm trying to use my pagination in "Taxonomy.php" because that's where I'm going to display my custom post types of "products" with my custom taxonomy "types" and use conditionals to generate different content on that same taxonomy.php.
This is the code in taxonomy.php that I got from css-tricks:
<?php
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query();
$wp_query->query('showposts=3&post_type=products'.'&paged='.$paged);
while ($wp_query->have_posts()) : $wp_query->the_post();
?>
// my content from my custom field that I want paginated
<p><?php the_field('image'); ?></p>
<?php endwhile; ?>
<nav>
<?php previous_posts_link('« Newer') ?>
<?php next_posts_link('Older »') ?>
</nav>
<?php
$wp_query = null;
$wp_query = $temp; // Reset
?>
This question has been asked a lot around the web and I have read at least 30 different articles and blogs about this and still can't seem to find a solution. With this method of pagination I got from css-tricks the only problem is I keep getting a 404 error message whenever I try to go to the next page unless I set my "Blog pages show at most" to 1 in settings > reading. If I do that then it works, otherwise it gives me the 404 page. My permalinks are set to Post Name, by the way.
So I can see my pagination here:
http://localhost/MyWebsite/types/cars/
but when I get here I get the 404 page.
http://localhost/MyWebsite/types/cars/page/2/
I have read about the Flush Method but that does not work. I only have 2 plugins activated (custom post types and fields) and those are not the problem. How can I fix the 404 error?
I have diagnosed the problem further and I figured out whats going on. This is now my code in terminology.php:
<?php
$args = array(
'post_type' => array( 'products' ),
'posts_per_page' => 5,
'paged' => ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1
);
$products_query = new WP_Query( $args );
$temp = $wp_query;
$wp_query = null;
$wp_query = $products_query;
while ($products_query->have_posts()) : $products_query->the_post();
?>
// content to display
<p><?php the_field('image'); ?></p>
<?php endwhile; ?>
<nav>
<?php previous_posts_link('« Newer') ?>
<?php next_posts_link('Older »') ?>
</nav>
<?php
$wp_query = null;
$wp_query = $temp; // Reset
?>
Bear with me because this is a little bit confusing. I'll try my best to clearly explain the difference between "Blog pages show at most" in settings > reading and my posts_per_page arguments.
I have a category of Honda with a sub-term of Accord and I have 3 products of my custom post type assigned to Accord
Now if I changed my posts_per_page argument what that does is no matter how many "Products" I have under "Accord" it will always demonstrate that amount even if you do not have enough.
So if I set it to 5 and I have 3 products it will still show 5 either repeated or from another category. When I click on the next pagination the pages will only go as far as however many items that I have. That is if "Blog pages show at most" is set to 1, which brings me to my next point.
Now the difference in changing "Blog pages show at most" is that even if I have my posts per pages set to 5 and I have only 3 "Products" but I change my "Blog pages show at most" to 3 it will not go to the next page because I only have 3 "products" in accord term.
If I were to change my "Blog pages show at most" to 1, I would be able to go forward 3 pages because I have 3 items.
It's sort of like changing "Blog pages show at most" changes the core of WordPress so no matter how many you put to display in the posts_per_page args it will only really count however many you set in "Blog pages show at most".
It seems like the pagination count is based upon "Blog pages show at most", not posts_per_page. I can have posts per page set to 10 and "Blog pages show at most" set to 2 and it would show 10 but only count as if there were really 2 on each page meaning I would only be able to go to the 2nd page of my "Accord" term that has 10 displaying in both of them.
Get the pattern? WordPress counted 2 products per page even though it was displaying 10. So it told the pagination that it can't go past page 2 because I only have 3 products.
But if you have a custom post type products and a taxonomy types you will show your content on: archive-products.php and taxonomy-types.php you could use pre_get_posts action to modify the query if you need and then write the markup you want on those files, example:
Put this on your functions.php
<?php
add_action( 'pre_get_posts', 'mr_modify_archive_taxonomy_query' );
function mr_modify_archive_taxonomy_query( $query ) {
if ( !is_admin() && $query->is_main_query() ) {
if ( is_post_type_archive( 'products' ) || is_tax( 'types' ) ) {
$query->set( 'posts_per_page', 3 );
}
}
}
?>
an use a default loop
<?php
if ( have_posts() ):
while ( have_posts() ): the_post();
printf( '<p>%s</p>', get_the_field( 'image' ) );
endwhile;
previous_posts_link('« Newer');
next_posts_link('Older »');
endif;
?>
I am developing a wordpress blog but for some reason the posts won't limit on the home page. I am trying to limit to 3 but it falls back to what is set in the administration area instead of 2. How come?
What am I doing wrong?
<?php
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query('posts_per_page=-1&paged=' . $paged);
query_posts('showposts = 2');
$flag = 1;
while ($wp_query->have_posts()) : $wp_query->the_post();
?>
Try using this code in the functions.php to limit the posts on the homepage, it should limit the posts to 2:
function posts_on_homepage( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( 'posts_per_page', 2 );
}
}
add_action( 'pre_get_posts', 'posts_on_homepage' );
There are 2 option available to do your task, you can easily limit post on your home page.
1) From the Dashboard:
(a) Settings->
(b) Reading ->
(c) Blog Posts - set to 3
Or you can do this....
2) From the Dashboard:
(a) Appearance
(b) Editor
(c) home page file (usually index.php)
(d) Locate this line:
(e) Just before this line add this line:
I hope this process will work for you, if you need more help I'm always available to help you
You cannot use WP_Query and query_posts together. And you should never use query_posts. You should either use pre_get_posts or WP_Query.
You should set posts_per_page to 3 if you need to display 3 posts.