Jump to content

[Solved] Add CMS content to CMS categories


Duckett

Recommended Posts

Hi,

I'm using Prestashop 1.6.0.6. I would like to add cms pages for each cms category page so I can provide some explanation about the contents of each cms category. I've created a table in the cms.tpl file with two columns showing the cms subcategories and cms pages of each cms category in left column. But now I want to add a specified cms page content in the right column to provide more information about that  (each) cms category.

 

I don't know how to modify cmscontroller.php in order to display different cms pages to each cms category page.

 

Thanks for your help!

Edited by Duckett (see edit history)
Link to comment
Share on other sites

hello

you mean that you want to display some contents on CMS category pages?

by default unfortunately it's not possible, but with some modifications of controller (cms) and template (cms.tpl) it's possible to create what you expect.

 

you can also achieve it with module to create custom contents like html block pro

Link to comment
Share on other sites

Hi vekia thanks for your reply!

 

I've already changed those files to add other things, such as other pages links of the same cms category of the cms page. That's why I would like to make the modifications for this topic by myself and learn how to do it.

 

I'm not been able to call cms pages from the cms category section with the codes I've seen in other posts fot cms.tpl and cmscontroller.php. I need to know how to call or get this pages content for each defined cms category.

 

I've seen your knowledge in PHP in many other posts so I would appreciate your help in this matter. I'll buy a module like yours If I'm not able to do it by myself.

Edited by Duckett (see edit history)
Link to comment
Share on other sites

by default prestashop already query everything from every cms page in the category you are so here is an easy way, a little hardcoded beacuse you need to know the ids of your cms pages but it doesn't require any override and you can use in cms.tpl, but probably even in other tpl:

{if isset($cms_category) && isset($cms_pages) && !empty($cms_pages)}
    {foreach from=$cms_pages item=cmspages}
        <!-- SHOW CONTENT FROM CMS PAGE 1 IF WE ARE IN CATEGORY 2 -->
        {if $cmspages.id_cms == '1' && $cms_category->id_cms_category == '2'}
            {assign "mycms" $cmspages}
        {/if}
        <!-- SHOW CONTENT FROM CMS PAGE 2 IF WE ARE IN CATEGORY 3 -->
        {if $cmspages.id_cms == '2' && $cms_category->id_cms_category == '3'}
            {assign "mycms" $cmspages}
        {/if}
    {/foreach}
        
    {if isset($mycms) && !empty($mycms)}
        <h1>{$mycms.meta_title|escape:'html':'UTF-8'}</h1>
        <p>{$mycms.content}</p>
    {/if}
{/if}

maybe can be optimized a bit, this will show title and content from cms page with id 1 if you are on category 2, on category 3 it will show cms page 2, you can add as many conditions you want and change the category id and page id

 

this can be probably optimized assuming that prestashop only query the cmspages for the actual category you can skip the second condition $cms_category->id_cms_category == 'X'

 

and make the code cleaner with just one condition {if $cmspages.id_cms == '1' || $cmspages.id_cms == '2'} and adding all the cms page you want to use since the cmspage with id 1 will not exist in a different category etc..

Link to comment
Share on other sites

Hi misthero,

thanks for your reply!

 

I've tried your code but sadly the category don't show the content... Do I have to do something with the cmscontroller.php file in order to allow this? I am missing something?

Link to comment
Share on other sites

hello,

 

did you changed the numbers here {if $cmspages.id_cms == '1' && $cms_category->id_cms_category == '2'} with your ids?

 

I don't know your cms page ID and category ID, this is just an example

 

do you get something if you write in your tpl {$cms_pages|print_r} ?

Link to comment
Share on other sites

Yes, I've changed the numbers. For example, 65 for page id and 2 for category id.

 

I also wrote {$cms_pages|print_rand this is what I get:

Array ( [0] => Array ( [id_cms] => 60 [id_cms_category] => 2 [position] => 0 [active] => 1 [indexation] => 0 [id_lang] => 1 [meta_title] => Test [meta_description] => [meta_keywords] => [content] => This is a test CMS page. [link_rewrite] => test [id_shop] => 1 )) 1

Edited by Duckett (see edit history)
Link to comment
Share on other sites

Correct, it's not in the same category.

 

It's not in the same category because I put code for the CMS Categories to show a list of the pages of each category, but the pages with the content to show in each cms category it's not a page or topic on itself of that category. If a I have the pages to show as content in each category in the same category, then they will show in the list as well as the "real" pages of that category. That's why I created a hidden CMS category with all the pages for the content of the categories only.

 

I hope you can understand me....  :unsure:

Link to comment
Share on other sites

ok you are right in this case you cannot use the default query prestashop does.

 

at this point you need a little override for /cmscontrolle.php

 

this is the idea, by default prestashop , when you are in a category page get the content of every active cms page. You can do this, force him to get every cms page active and unactive, than show only the active one in the tpl and use the unactive for your category description.

 

in this case you must put as UNACTIVE your page with id 65 (and any other page you wanna use as category description)

 

(btw: is not enough using the category description instead of using another cms page????)

 

if it is not keep the previous code in your tpl and add this:

 

change cmscontroller.php around line 128 the paramater TRUE to FALSE

'cms_pages' => CMS::getCMSPages($this->context->language->id, (int)($this->cms_category->id), true, (int)$this->context->shop->id),

to

'cms_pages' => CMS::getCMSPages($this->context->language->id, (int)($this->cms_category->id), false, (int)$this->context->shop->id),

than in your cms tpl you should have somwhere:

<ul class="bullet list-group">
				{foreach from=$cms_pages item=cmspages}
					<li>
						<a class="list-group-item" href="{$link->getCMSLink($cmspages.id_cms, $cmspages.link_rewrite)|escape:'html':'UTF-8'}">{$cmspages.meta_title|escape:'html':'UTF-8'}</a>
					</li>
				{/foreach}
			</ul>

add a condition to only get the active pages

<ul class="bullet list-group">
				{foreach from=$cms_pages item=cmspages}
					{if $cmspages.active == 1} <!-- CONDITION ADDED HERE -->
					<li>
						<a class="list-group-item" href="{$link->getCMSLink($cmspages.id_cms, $cmspages.link_rewrite)|escape:'html':'UTF-8'}">{$cmspages.meta_title|escape:'html':'UTF-8'}</a>
					</li>
					{/if} <!-- END of CONDITION -->
				{/foreach}
			</ul>

now the overrid will ask prestashop to get every page, active and unactive, the tpl will filter the active pages and the unactive will be used as a description

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...