RainLab blog - OctoberCMS - Show posts from specific category - php

I've found the following code in Posts.php - I think I essentially need to duplicate this and turn it into a public function limiting the category to a sepcific ID. Although that seems overkill? Can I query the current function on the front end?
Here's the posts.php code:
protected function listPosts()
{
$category = $this->category ? $this->category->id : null;
/*
* List all the posts, eager load their categories
*/
$isPublished = !$this->checkEditor();
$posts = BlogPost::with('categories')->listFrontEnd([
'page' => $this->property('pageNumber'),
'sort' => $this->property('sortOrder'),
'perPage' => $this->property('postsPerPage'),
'search' => trim(input('search')),
'category' => $category,
'published' => $isPublished,
'exceptPost' => $this->property('exceptPost'),
]);
/*
* Add a "url" helper attribute for linking to each post and category
*/
$posts->each(function($post) {
$post->setUrl($this->postPage, $this->controller);
$post->categories->each(function($category) {
$category->setUrl($this->categoryPage, $this->controller);
});
});
return $posts;
}
on the front end is this:
{% for post in posts %}
<li>
<h3>{{ post.title }}</h3>
</li>
{% else %}
<li class="no-data">{{ noPostsMessage }}</li>
{% endfor %}
Could someone point me in the right direction?
Cheers,

Not sure what you want to achieve. You just want to show posts from specific category only?
Set the parameter "category filter" of your blogPosts component to the slug of specific category
[blogPosts newsList]
pageNumber = "{{ :page }}"
categoryFilter = "news"
postsPerPage = 100
noPostsMessage = "No posts found"
sortOrder = "published_at desc"
categoryPage = "blog/category"
postPage = "blog/post"
==
{% component 'newsList' %}
this will show posts only from "news" category

Related

OctoberCMS: Why is the slug value missing in "post.url" of the blog plugin

I am using the [blogPosts] component of the blog plugin. I have a for loop to display all blog posts. Everything is fine, but the value of {{ post.url }} is missing the slug value. This is how I have used the component:
url = "/blog/:page?"
layout = "default"
[blogPosts]
pageNumber = "{{ :page }}"
postsPerPage = 10
noPostsMessage = "No posts found"
sortOrder = "published_at desc"
categoryPage = 404
postPage = "post"
==
{% set posts = blogPosts.posts %}
{% for post in posts %}
<h1>{{ post.title }}</h1>
<p>{{ post.summary|raw }}</p>
Read more
{% endfor %}
In the above examples, all post links are referring to blog/post. I expect the slug of each post to be in its url, but it's missing. Why?
Sometimes it may be an issue with the blog page URL.
if you are using [blogPosts] and in markup, you need to generate a link of blog page using {{ post.url }} you need to make proper url of the blog page
title = "Blog Post Page"
url = "/blog/post/:slug"
layout = "default"
is_hidden = 0
==
<?PHP
// ... other code
Here important stuff is url = "/blog/post/:slug" and main important thing is :slug parameter
URL has to have :slug parameter to replace with actual post slug.
If you also need to add post category slug in URL then you also need to use :category parameter in URL for ex: url = "/blog/post/:category/:slug"
these :slug and :category is hardcoded in making a link of the post so you need to use exactly them not other names. check below screenshot of post model from rainlab.post plugin.
if any doubt please comment.

Dynamic links in menu (Symfony 3)

I'm working with Symfony and I want to add menu bar to my site. I have a simple menu bar with 3 links that are static and always be the same like "Home" but I need to add new links that should be dynamic. I have my admin panel where I can add new pages (info about them is stored in database - title, content etc.) and what I want to achieve is that after adding a new page, a link to this page should appear in this menu. I have this menu in my header.html.twig file which I include to every page same as footer.
Best of what I was able to achieve at that moment:
DefaultController.php
public function headerAction ()
{
$news = $this->getDoctrine()
->getManager()
->createQueryBuilder()
->from('AppBundle:News', 'n')
->select('n')
->where('n.type = :news')
->setParameter('news', 'other')
->getQuery()
->getResult();
$parsedown = new Parsedown();
foreach($news as $key => $new){
$news[$key]->setContent($parsedown->text($new->getContent()));
}
return $this->render('default/other_menu.html.twig', array(
'news' => $news
));
}
header-menu.html.twig
{# some bootstrap #}
{# simple static links #}
{{ render(controller('AppBundle:Default:header')) }}
{# simple static links #}
{# some bootstrap #}
other_menu.html.twig
{% for new in news %}
{{ new.title }}
{% endfor %}
But this solution works only when I have one page in database. With more of them - one long link is created (page1page2page3)
Any help will be appreciated.

Phalcon & Volt: Adding hasMany()/belongsTo() conditions, and counting linked rows

I have two tables in my database, form_settings and webmaster, that are on a one-to-many relationship, and this has been defined in their Models.
FormSettings.php
class FormSettings extends \Phalcon\Mvc\Model
{
public function initialize()
{
$this->hasMany('db_table', 'webmaster', 'db_table');
}
}
Webmaster.php
class FormSettings extends \Phalcon\Mvc\Model
{
public function initialize()
{
$this->belongsTo('db_table', 'form_settings', 'db_table');
}
}
In my Controller, I perform the following find() and pass it to the view:
ControllerBase.php
class ControllerBase extends Controller
{
public function initialize()
{
$exhibitions = FormSettings::find(
array(
'form_type = "v" AND show_end_date > NOW() AND archived = "n"',
'order' => 'db_table'
)
);
$this->view->exhibitions = $exhibitions;
}
}
And I know it's correctly linking rows from my webmaster table, as I have the following code in my View, and it displays webmaster_id values:
index.volt
{% for exhibition in exhibitions %}
<li>
{{ link_to('index/browse/' ~ exhibition.db_table, exhibition.db_table) }}
<!-- testing below -->
{% for webm in exhibition.webmaster %}
{{ webm.webmaster_id }}
{% endfor %}
<!-- end testing -->
</li>
{% endfor %}
My question is three-part:
How can I only link webmaster rows that have a column extra_1 as not NULL?
How can I count() the linked webmaster rows for each db_table (which is unique in form_settings)?
How can I pass this information through to the View in my $exhibitions object so that I can echo the count() in Volt syntax?
Hey and first of all thank you for the nice question formatting.
Excuse me for using examples that use my current database structure. But you can easily update your code.
1) You can set additional parameters to the relation definition.
$this->hasMany('id', 'Models\News', 'category_id', [
'alias' => 'news',
'reusable' => true,
'params' => [
'order' => 'id DESC',
'conditions' => 'extra_1 IS NOT NULL',
]
]);
Please note the reusable above. When using it, the query runs only once per request. Considering you want to count records and iterate over them its a nice performance boost.
2 + 3) Iterating over results in volt and counting:
Controller code:
$this->view->categories = \Models\NewsCategories::find();
Volt:
{% for category in categories %}
{% if category.news|length > 0 %} // Do not print categories without articles
<h3>Category #{{ category.id }} with total of {{ category.news|length }} articles.</h3>
<ul>
{% for item in category.news %}
<li>News #{{ item.id }}</li>
{% endfor %}
</ul>
{% endif %}
{% endfor %}
The above code in my case produces the following output:
Category #4 with total of 4 articles.
News #3
News #4
News #5
News #7 Category #5 with total of 1 articles.
News #1

how to make navigation dropdown menu in Pyro CMS

I have file "header.html" in partial folder, here my code
<ul class="sf-menu" id="mainMenu">
{{ shoesmart:showMenu group="header" indent="tab"}}
<li>{{ name }}
{{ if children }}
<ul>
{{ children }}
<li>{{ name }}</li>
{{ /children }}
</ul>
{{ endif }}
</li>
{{/shoesmart:showMenu}}
</ul>
and I have file "shoesmart.php" in plugins folder, here my code :
function showMenu()
{
$return = '';
$menu_list = $this->db->select('*,CONCAT(pua.type,"/",pua.keyword) as path',false)
->from('menu mn')
->join('product_url_alias pua','mn.url_alias_id=pua.url_alias_id','LEFT')
->where('status',1)
->get()
->result_array();
foreach ($menu_list as $result) {
$return[] = array(
'name' => $result['name'],
'url' => BASE_URL.'home/'.$result['path']
);
}
return $return;
}
and my table name in database is "default_menu" that have structure:
menu_id url_alias_id name status parent_id
1 868 Men 1 0
2 869 woman 1 0
but when I refresh in browser, just show main menu, its mean that the submenu didn't show. Should I change my code ? or I also add new table for submenu in my database ?So please help me .. I have deadline project tomorrow T.T

Create a paginate using KnpPaginator

I tried to create a pagination on my website using the bundle KnpPaginator.
In my repository I create a query :
public function getProductsOrderByDateDesc($id_category = null, $max = null){
$qb = $this->createQueryBuilder('p')
->orderBy('p.created_at', 'DESC');
if($max) {
$qb->setMaxResults($max);
}
if($id_category) {
if(is_array($id_category)){
$aIdCategory = implode("','",$id_category);
$qb->andWhere('p.category IN (:ids)')
->setParameter('ids', $aIdCategory);
}else{
$qb->andWhere('p.category = :category_id')
->setParameter('category_id', $id_category);
}
}
$query = $qb->getQuery();
return $query->getArrayResult();
}
In my controller I do :
$repositoryProduct = $em->getRepository('ShopDesktopBundle:Product');
$aProducts = array();
$aProducts = $repositoryProduct->getProductsOrderByDateDesc($id);
$paginator = $this->get('knp_paginator');
$pagination = $paginator->paginate(
$aProducts,
$this->get('request')->query->get('page', 1),
3
);
return $this->render('ShopDesktopBundle:Category:category.html.twig',array(
'aProducts' => $aProducts,
'pagination' => $pagination
));
In view I only show this pagination :
<div class="navigation">
{{ knp_pagination_render(pagination) }}
</div>
The problem is that always displays all products not only the limit who for my example is 3.
For example :
I have 9 products, limit = 3, the pagination is correct "1 2 3" but for every page I see all 9 products
Help me please ! Thx in advance
Almost correct, however you have to use the pagination object instead of the 'aProducts'. In your view, use the following code:
{% for product in pagination %}
<tr {% if loop.index is odd %}class="color"{% endif %}>
<td>{{ product.id }}</td>
<td>{{ product.title }}</td>
</tr>
{% endfor %}
See more info in the documentation, under 'View': https://github.com/KnpLabs/KnpPaginatorBundle

Categories