Jump to content

Module or field to display articles in categories


kaz
 Share

Recommended Posts

Which field / module is suitable for creating articles in different categories? I tried it with ListerPro and a radio select field, but that doesn't work.

Before I invest any more time, I would like to ask here. Which module / field is suitable for displaying articles frontend in categories? It may gladly be Pro modules.

Link to comment
Share on other sites

Your question is very vague. Are you talking about a blog with some kind of tagging feature? Is the challenge the backend implementation or the frontend? Or both? What "did not work" with your approach of using ListerPro / radio select. The more context you give us the better we can help.

For tagging the TextTags module can be a great choice. It's imho a bit cumbersome to setup but once you got it setup its great for such use cases: https://processwire.com/blog/posts/pw-3.0.177/

Link to comment
Share on other sites

I add articles in ListerPro. The count is displayed as child pages under the page. That works quite well so far. Now I need categories. ListerPro does not support categories, it's a tool to sort data in the backend. I hope that I'm right ..?
Without categories, I read the data from the template name:

foreach($pages->find('template=sample, sort=sort') as $item)

I added a FieldtypeToggle field to the template to check out two categories. My idea was that the content should of both be assigned under the categorie-name headline.

I think my search is wrong, $pages->find('template=sample) of course finds all data, regardless of the FieldtypeToggle selection. I read the Select Options Fieldtype (FieldtypeOptions), but I can't get any further. Either ‘0’ or ‘1’ is displayed, not both, and they don't end up under the right headline either. I do not have the complete code anymore, because I have tried new ones for days.

Because I obviously need two identical blocks with this procedure, the if query, I therefore need two identical body codes with all fields, with exception of the different headline. That alone is not ideal:

<?php if($page->MyFieldtypeOptionsField === 0) {
echo "<h3></h3>
<div class='uk-grid-medium uk-margin-medium-top' uk-grid>";
foreach($pages->get('template=sample')->find('MyFieldtypeOptionsField=0') as $item) {

I'm sorry, lots of text for a simple question. I think I don't use the ideal module. Maybe there are modules with category (or something like that) functions? Trying to solve it with FieldtypeOptions was surely not my best idea?

Link to comment
Share on other sites

hi,
here is the way i do what i think you're describing, easy for me and easy for the client too
maybe it could be useful and sort of adaptable for your structure
i put all this in a code block, easier to comment 🙂

/*
as it is a multilingual website, i need an easy way to display the tags in various languages
it would work for a one language wensite and it make easy for the client to add a new category
without going anywhere else than in the pages tree
- a page with a template blog_cats without file
- chhildren of this page with a blog_cat template (no file needed either)

- for the blog article pages a page reference field - pages multiple array, every article can belong to several categories
on top of the blog page (with url segments allowed)
*/

// first i retreive the categories in the user language
// 1122, i'm sure you guessed, being the blog_cats page
$cats = $pages->find('parent=1122, template=blog_cat');

$cats_name = array();
foreach ($cats as $c) {
    $cats_name[] = $c->name;
}

// then i check if there is an url segment 
$zcat = isset($input->urlSegment1) ? $input->urlSegment1 : '';
// if not
if ( $zcat == '') {
    $arts = $pages->find('template=blog_article, sort=-date_date, limit=6');
    $total = $pages->count('template=blog_article, sort=-date_date, limit=6');
}
// if yes
else {
    // yes, being paranoid i first check the url segment is in the cats array above, else... 404
    // well, if it were my website o would go for a die() but clients usually need to be a bit more diplomatic :)
    if ( ! in_array($zcat, $cats_name) ) throw new Wire404Exception();
    $arts = $pages->find('template=blog_article, artcats%='.$zcat.', sort=-date_date, limit=6');
    $total = $pages->count('template=blog_article, artcats%='.$zcat.', sort=-date_date, limit=6');
}

of course, easy peasy to set a raw of butttons/links to select a category on top of the blog page using the $cats array retreived above (i like when things are easy :D)

<div class="top_cat_wrapper">
    <a href="<?= $page->url; ?>" class="blog_cat_butt<?php if($zcat == '') echo ' selected'; ?>">*</a>
    <?php foreach($cats as $bc): ?>

    <a href="<?= $page->url.$bc->name; ?>" class="blog_cat_butt<?php if($zcat != '' && $zcat == $bc->name) echo ' selected'; ?>"><?= $bc->title; ?></a>
    <?php endforeach; ?>

</div>

in case it could be useful

have a nice day

  • Like 1
Link to comment
Share on other sites

I think my field settings are not correct?

I have selected this (Details):

image01.png.c3e678044249f3e37f288b66894f18e4.png

If I have two articles and I create an entry with the new Page referencefield, e.g. Sydney, it looks like this:

image04.png.097fc37badb2c7f356166672da2c61ee.png

The names of the pages are also displayed. I don't want that, I only want that Sydney (my added categories) is displayed. Is this because of the field settings?

and sorry, I don't see a way to scale the image size here

Link to comment
Share on other sites

@virtualgadjo Hi, it's not easy to get the field to work. I have adjusted the settings of the field. Unfortunately, the complete page list is still included in the select field (when selecting in the editor). Are further settings necessary so that only what I have added myself is displayed?

Link to comment
Share on other sites

Hi,

there is another setting you may have look to, in the input tab of the field, the "parent" fieldset contains a lot of options letting you choose which pages you want in the list, selected either by their parent or their template
digging a little you should easily end with only the pages you need in the list
and, actually, most of the time, when i say digging, it will only be selecting a parent or a template but i occasionally have used the custom php code to exclude some pages or add some hard to select depending of the structure of the website (probably basically my fault... 🙂 )

have a nice day

Link to comment
Share on other sites

I think the field is not what I really need. I don't need a reference to pages what the field is actually for. I only need the heading of categories that I create myself. This would work with a select field if there is a selection of headlines. Using the page reference field, the list of pages which I don't want is not what I need.
Because my deadline has already expired, I used the standard text field, which is visible via an if query. The sorting is done by the editor in the page tree. This isn't perfect for every client, but it does what I need. It would have been nice if sorting with a select option had been possible, but now it is as it is.

Thanks to all of you!

Link to comment
Share on other sites

4 minutes ago, kaz said:

Using the page reference field, the list of pages which I don't want is not what I need.

@kaz, pages in PW are just a storage mechanism. They are called pages historically, but are often used as nodes, data items etc. Using pages as values for selects is a recommended way to go in PW. So @virtualgadjo's advice is a great one and a best practice. So do not hesitate and use it.

See the 1st sentence of the official docs here : "While we usually recommend using the Page Fieldtype for selectable options..."

P.S. Please star ProcessWire on github!

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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...