cancel
Showing results for 
Search instead for 
Did you mean: 

How to display products of a specific category on homepage

How to display products of a specific category on homepage

Hello everyone,

i try to show products of the specific category on homepage.

i had posted the code into the "CONTENT" of "PAGE"

Code: {{block type="catalog/product_list" column_count="4" category_id="4" template="catalog/product/list.phtml"}}

The reulst: it just showed the grey color as the same as following picture: 

THANK YOU!

magento 2.png

2 REPLIES

Re: How to display products of a specific category on homepage

Hi,

 

You can try the following code -

 

      

 $collection =  $this->_productcollection->create()
                        ->addAttributeToFilter('status', '1');
        $collection->setVisibility($this->_catalogProductVisibility->getVisibleInCatalogIds()); 
        $collection = $this->_addProductAttributesAndPrices($collection)
                            ->setPageSize($productCount);

        /* To display product by category id */
        if($category_id=$this->getData("category_id")){
            $category = $this->categoryRepository->get($category_id);
            $collection->addCategoryFilter($category);
        }

        $collection->getSelect()->order('rand()');

        return $collection;

Re: How to display products of a specific category on homepage

  1. Open Magento admin panel and go to Catalog > Manage Categories.
  2. Select the necessary category and look for the category ID at the top. Remember the category ID value as you’ll need it later:
  3. In Magento admin panel go to CMS> Pages>Home
  4. Switch to the Content taband into the html code block paste the following code:

 

1

2

{{block  type="catalog/product_list" category_id="4"  template="catalog/product/list.phtml"}}

5. In case you want to control the number of columns in product listing, please use the following code:

 

1

2

{{block type="catalog/product_list" column_count="4" category_id="4" template="catalog/product/list.phtml"}}

Where the column_count value is the number of columns.

6. Navigate to System->Permissions->Blocks.

7. Click on the Add new block

8. Specify your Block Name, which is: catalog/product_list.

9. And allow the Block using the YES/No Dropdownlist: