Zadejte hledaný výraz...
Jakub Glos
Webtrh.cz
Vývoj webových stránek na WordPressu a proklientský přístup pro freelancery
Třídenní infromacemi nabitý prezenční + online kurz v Praze od Webtrhu pouze za 2 871 Kč
Více informací

Zobrazení 8 článků na stránku – (Laravel / Octobercms)

Tomina10
verified
rating uzivatele
(10 hodnocení)
7. 1. 2022 11:34:11
Ahoj všem,
prosím zde přítomné, kteří by mi byli ochotný pomoci...
Skrze pluigin Rainlab Blog, se snažím zobrazit na stránku 8 příspěvků, na další dalších 8 příspěvků...
jenže je problém, protože ať jsem na kterékoliv stránce, 8 příspěvků mi to zobrazí, jenže si to přednačte dalších více jak 20 tisíc dalších článků na dalších stránkách, a na další zas... atd.
Byl by tu někdo ochotný na to kouknout?
Rád dám i na kafe... :D
use Lang;
use Redirect;
use BackendAuth;
use CmsClassesPage;
use CmsClassesComponentBase;
use OctoberRainDatabaseModel;
use OctoberRainDatabaseCollection;
use RainLabBlogModelsPost as BlogPost;
use RainLabBlogModelsCategory as BlogCategory;
use RainLabBlogModelsSettings as BlogSettings;
class Posts extends ComponentBase
{
/**
* A collection of posts to display
*
* @var Collection
*/
public $posts;
/**
* Parameter to use for the page number
*
* @var string
*/
public $pageParam;
/**
* If the post list should be filtered by a category, the model to use
*
* @var Model
*/
public $category;
/**
* Message to display when there are no messages
*
* @var string
*/
public $noPostsMessage;
/**
* Reference to the page name for linking to posts
*
* @var string
*/
public $postPage;
/**
* Reference to the page name for linking to categories
*
* @var string
*/
public $categoryPage;
/**
* If the post list should be ordered by another attribute
*
* @var string
*/
public $sortOrder;
public function componentDetails()
{
return ;
}
public function defineProperties()
{
return ,
'categoryFilter' => ,
'postsPerPage' => +$',
'validationMessage' => 'rainlab.blog::lang.settings.posts_per_page_validation',
'default' => '10',
],
'noPostsMessage' => ,
'sortOrder' => ,
'categoryPage' => ,
'postPage' => ,
'exceptPost' => +$',
'validationMessage' => 'rainlab.blog::lang.settings.posts_except_post_validation',
'default' => '',
'group' => 'rainlab.blog::lang.settings.group_exceptions',
],
'exceptCategories' => +$',
'validationMessage' => 'rainlab.blog::lang.settings.posts_except_categories_validation',
'default' => '',
'group' => 'rainlab.blog::lang.settings.group_exceptions',
],
];
}
public function getCategoryPageOptions()
{
return Page::sortBy('baseFileName')->lists('baseFileName', 'baseFileName');
}
public function getPostPageOptions()
{
return Page::sortBy('baseFileName')->lists('baseFileName', 'baseFileName');
}
public function getSortOrderOptions()
{
$options = BlogPost::$allowedSortingOptions;
foreach ($options as $key => $value) {
$options = Lang::get($value);
}
return $options;
}
public function onRun()
{
$this->prepareVars();
$this->category = $this->page = $this->loadCategory();
$this->posts = $this->page = $this->listPosts();
/*
* If the page number is not valid, redirect
*/
if ($pageNumberParam = $this->paramName('pageNumber')) {
$currentPage = $this->property('pageNumber');
if ($currentPage > ($lastPage = $this->posts->lastPage()) && $currentPage > 1) {
return Redirect::to($this->currentPageUrl());
}
}
}
protected function prepareVars()
{
$this->pageParam = $this->page = $this->paramName('pageNumber');
$this->noPostsMessage = $this->page = $this->property('noPostsMessage');
/*
* Page links
*/
$this->postPage = $this->page = $this->property('postPage');
$this->categoryPage = $this->page = $this->property('categoryPage');
}
protected function listPosts()
{
$category = $this->category ? $this->category->id : null;
$categorySlug = $this->category ? $this->category->slug : null;
/*
* List all the posts, eager load their categories
*/
$isPublished = !$this->checkEditor();
$posts = BlogPost::with()->listFrontEnd();
/*
* Add a "url" helper attribute for linking to each post and category
*/
$posts->each(function($post) use ($categorySlug) {
$post->setUrl($this->postPage, $this->controller, );
$post->categories->each(function($category) {
$category->setUrl($this->categoryPage, $this->controller);
});
});
return $posts;
}
protected function loadCategory()
{
if (!$slug = $this->property('categoryFilter')) {
return null;
}
$category = new BlogCategory;
$category = $category->isClassExtendedWith('RainLab.Translate.Behaviors.TranslatableModel')
? $category->transWhere('slug', $slug)
: $category->where('slug', $slug);
$category = $category->first();
return $category ?: null;
}
protected function checkEditor()
{
$backendUser = BackendAuth::getUser();
return $backendUser && $backendUser->hasAccess('rainlab.blog.access_posts') && BlogSettings::get('show_all_posts', true);
}
}
Zkoušel jsem https://docs.octobercms.com/1.x/services/pagination.html#paginating-model-results ale asi špatně..
7. 1. 2022 11:34:11
https://webtrh.cz/diskuse/zobrazeni-8-clanku-na-stranku-laravel-octobercms/#reply1496326
Pavel Lorenc
verified
rating uzivatele
(1 hodnocení)
7. 1. 2022 22:55:07
Přímo v komponentě máš možnost omezit kolik článků ti to bude načítat na stránce... Omezuješ to tedy až v dané komponentě - ne v třídě Posts. A už vůbec tomu nepomůžeš, když všech 20K článků nejprve načteš a pak je rozdělíš přes pagination.
Zkus se podívat přímo do Administrace v nastavení komponenty "Seznam příspěvků" - viz printscreen, tam to uvidíš rovnou https://ibb.co/cc3vzgS
Pavel
7. 1. 2022 22:55:07
https://webtrh.cz/diskuse/zobrazeni-8-clanku-na-stranku-laravel-octobercms/#reply1496325
NichollsS
verified
rating uzivatele
11. 1. 2022 07:01:24
October CMS has a simple extensible core. The core provides basic CMS functions and a lot of functionality can be added with plugins from the Marketplace. The plugins Marketplace includes a lot of product categories, which include blogging, e-commerce, contact forms, and many others.
11. 1. 2022 07:01:24
https://webtrh.cz/diskuse/zobrazeni-8-clanku-na-stranku-laravel-octobercms/#reply1496324
Pro odpověď se přihlašte.
Přihlásit