Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2017-03-08 08:04:32

alicson
Member
Registered: 2004-05-26
Posts: 465
Website

Categorized list of Live article titles

I’m trying to output a list of category titles (with listings of contained articles), however I only want those to show that actually contain at least one article—and only live articles rather than those that are marked hidden (or draft).

Can I achieve this with built-in txp tags (or with smd_if or other)?

(Currently calling it with the following:

<txp:category_list section="journal, words" parent='<txp:category />' exclude='<txp:category />' break="li" wraptag="ul">
<txp:category title="1" link="1" section="journal, words" />
<txp:article_custom category='<txp:category />' wraptag="ul" limit="999">
<li><txp:permlink><txp:title /></txp:permlink></li>
</txp:article_custom>
</txp:category_list>

The category links also aren’t working..)


textpattern.org :: find and share Textpattern resources
docs.textpattern.io :: Textpattern user documentation

Offline

#2 2017-03-08 09:13:32

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,250
Website GitHub

Re: Categorized list of Live article titles

If I’ve understood correctly and you’re willing to forego the <li> tags, you could turn this on its head like this:

<txp:article_custom limit="9999" section="journal, words" sort="category1, Posted" status="live">

<txp:if_different>
   <txp:category1 title="1" link="1" wraptag="h3" />
</txp:if_different>

<div class="entry">
   <txp:permlink><txp:title /></txp:permlink>
</div>

</txp:article_custom>

That just gets all articles in the given sections, orders them by category1 and only adds the category title if it differs from the last one seen. You can tweak it to suit, plug URL vars into the article_custom tag if you like, and use CSS to make it look like a list.

Adding the <li> tags makes things tricky because you need to open and close the <ul>s properly, which <txp:if_different> isn’t brilliant at, and requires sorcery [Edit: jakob’s post shows how to do it].

The downside to this approach is that you can’t sort by category title only by category name (if your cat titles are similar to the names, it won’t be a problem), and you’re limited to treating category 1 and 2 separately. That means cat2 entries will always appear after cat1, should you decide to display those as well.

There may be refinements to this technique, or maybe you can roll some if_different magic into your original idea to get closer to what you want.

Last edited by Bloke (2017-03-08 09:24:29)


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#3 2017-03-08 09:17:21

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,578
Website

Re: Categorized list of Live article titles

You can do it with a wrapper category_list tag and a separate article_custom tag for each category as you have it but you get a lot of queries as a result. I’m thinking you should be able to do this more efficiently this using if_different. Live is standard for article_custom, so you don’t need to worry about draft or hidden, and as it only lists actual articles, empty categories don’t show.

The principle is straightforward if you have <p> tags for the list and h3 tags for the heading:

<txp:article_custom category='<txp:category_list parent="my-parent-cat" children="1" break="," exclude="my-parent-cat" />' wraptag="" break="" sort="category1 asc, posted desc">
  <txp:if_different>
    <h3><txp:category title="1" link="1" /></h3>
  </txp:if_different>
    <p><txp:permlink><txp:title /></txp:permlink></p>
</txp:article_custom>

It’s when you need to deal with closing or nesting container html tags, such as your <ul>…</ul> list, that it becomes more complex because if you use the variant above you end up with a stray closing tag at the beginning. You can get round this with a trick:

<txp:article_custom category='<txp:category_list parent="my-parent-cat" children="1" break="," exclude="my-parent-cat" />' wraptag="" break="" sort="category1 asc, posted desc">

<txp:variable name="cat_heading"><txp:if_different><txp:category title="1" link="1" /></txp:if_different></txp:if_variable>

<txp:if_variable name="cat_heading" value=""><txp:else />
  <txp:if_first_article><txp:else />
     </ul>
  </txp:if_first_article>
      <h3><txp:variable name="cat_heading" /></h3>
      <ul>
</txp:if_variable>
        <li><txp:permlink><txp:title /></txp:permlink></li>
<txp:if_last_article>
      </ul>
</txp:if_last_article>

</txp:article_custom>

That gives you two queries, one for the category_list tag-in-tag usage and one for the article_custom tag.

Re your category links: I don’t think the section attribute contributes to the link on a txp:category tag (where should it point to with comma-separated options?) but if all your articles are in the same section as the list you are outputting with above code, you can use the attribute this_section="1" to influence where the category link points to. Otherwise, you may need to construct them manually to point to where you want them to. You mentioned elsewhere you’re using gbp_permanent_links: if so, you may need a rule on the target page to handle what happens on those category pages.

EDIT: Stef was quicker, but I added something.


TXP Builders – finely-crafted code, design and txp

Offline

#4 2017-03-08 10:12:53

etc
Developer
Registered: 2010-11-11
Posts: 5,028
Website GitHub

Re: Categorized list of Live article titles

Unfortunately, <txp:category title="1" /> will not work as expected, and <txp:category1 title="1" /> will (I think) require a db query. So, the number of queries is (roughly) equivalent to

<txp:category_list parent='<txp:category />' exclude='<txp:category />' break="" wraptag="ul">
	<txp:variable name="catarticles"><txp:article_custom section="journal, words" category='<txp:category />' wraptag="ul" limit="999">
		<li><txp:permlink><txp:title /></txp:permlink></li>
	</txp:article_custom></txp:variable>
	<txp:if_variable name="catarticles" value=""><txp:else />
	<li><txp:category title="1" link="1" /><txp:variable name="catarticles" /></li>
	</txp:if_variable>
</txp:category_list>

Just FWIW, this will be a bit simpler in 4.7:

<txp:category_list parent='<txp:category />' exclude='<txp:category />' break="" wraptag="ul">
<txp:evaluate test="article_custom">
	<li><txp:category title="1" link="1" />
	<txp:article_custom section="journal, words" category='<txp:category />' wraptag="ul" limit="999">
		<li><txp:permlink><txp:title /></txp:permlink></li>
	</txp:article_custom></li>
</txp:evaluate>
</txp:category_list>

Offline

#5 2017-03-08 10:54:16

colak
Admin
From: Cyprus
Registered: 2004-11-20
Posts: 9,007
Website GitHub Mastodon Twitter

Re: Categorized list of Live article titles

Is this of any help?… Maybe not as it does not check for empty categories.

Last edited by colak (2017-03-08 12:13:57)


Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.

Offline

#6 2017-03-09 04:03:02

alicson
Member
Registered: 2004-05-26
Posts: 465
Website

Re: Categorized list of Live article titles

Thank you thank you ALL for ALL of this! I am slowly experimenting and learning with it..
I haven’t wrapped my head around txp:evaluate as yet.. (I think I understand txp:variable fine enough to use it); currently exploring if_different.


textpattern.org :: find and share Textpattern resources
docs.textpattern.io :: Textpattern user documentation

Offline

#7 2017-03-09 08:30:53

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,250
Website GitHub

Re: Categorized list of Live article titles

You’re welcome. btw, <txp:evaluate> hasn’t hit the shelves yet. Next release…


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#8 2017-03-09 09:41:13

alicson
Member
Registered: 2004-05-26
Posts: 465
Website

Re: Categorized list of Live article titles

FYI category archives seem to be working ! I probably didn’t get it near as efficient as possible.. however it is doing all the things yay! ALL very thanks to your help. (Wonder if I’ll be able to do the same eventually with the date archives..) The site’s still chaos, however it’s starting to function properly again (getting article and article_custom back online with that database date issue was the biggest issue, solved :).

Essentially the category archives had been this:

<txp:rss_suparchive_bycat section="journal,words" showheader="1" headerlabel="Category Slates:" showmenu="1" showcats="1" showcatcount="1" showsections="1" showsectioncount="1" showjump="1" jumplabel="# hop to top" showcommentscount="1" showzerocount="0" countprefix=" [" countsuffix="]" />

Is now this:

<div id="CategoryArchive">
<div id="CategoryArchiveHeader">Category Slates:</div>

<txp:if_category>
<txp:category_list section="journal, briefs, words" wraptag="ul" break="" class="CategoryArchiveMenu"><txp:variable name="article_count"><txp:mdn_count category='<txp:category />' /></txp:variable><txp:if_variable name="article_count" value="0"><txp:else />
<li><a href="/<txp:section />/?c=<txp:category />"><txp:category title="1" /></a></li></txp:if_variable></txp:category_list>
View articles in <a href="/archives/">all categories</a>
<txp:else />
<txp:category_list section="journal, briefs, words" wraptag="ul" break="" class="CategoryArchiveMenu"><txp:variable name="article_count"><txp:mdn_count category='<txp:category />' /></txp:variable><txp:if_variable name="article_count" value="0"><txp:else />
<li><a href="#c_<txp:category />"><txp:category title="1" /></a></li></txp:if_variable></txp:category_list>
</txp:if_category>

<div id="CategoryList">
<txp:if_category>
<h3 class="Ccat"><txp:category title="1" /> (<txp:mdn_count category='<txp:category />' />)</h3>
<txp:article_custom category='<txp:category />' wraptag="ul" limit="999" sort="section, posted">
<txp:variable name="article_count2"><txp:mdn_count category='<txp:category />' /></txp:variable><txp:if_variable name="article_count2" value="1"><txp:else /><txp:if_different><li><div class="Csec">in <a href="/<txp:section />/?c=<txp:category />"><txp:section /></a></div></li></txp:if_different></txp:if_variable>
<li class="C-art"><txp:permlink><txp:title /></txp:permlink><txp:if_comments> <span class="CmntCnt">[<txp:comments_count />]</span></txp:if_comments></li></txp:article_custom>
</div><!-- end #CatList -->
<txp:else />
<txp:category_list parent='<txp:category />' exclude='<txp:category />' break="" wraptag="ul"><txp:variable name="catarticles"><txp:article_custom section="journal, briefs, words" category='<txp:category />' wraptag="ul" limit="999" sort="section, posted">
<txp:variable name="article_count2"><txp:mdn_count category='<txp:category />' /></txp:variable><txp:if_variable name="article_count2" value="1"><txp:else /><txp:if_different><li><div class="Csec">in <a href="/<txp:section />/?c=<txp:category />"><txp:section /></a></div></li></txp:if_different></txp:if_variable>
<li class="C-art"><txp:permlink><txp:title /></txp:permlink><txp:if_comments> <span class="CmntCnt">[<txp:comments_count />]</span></txp:if_comments></li></txp:article_custom></txp:variable>

<txp:if_variable name="catarticles" value=""><txp:else />
<div id="c_<txp:category />" class="Ccat"><a href="/archives/?c=<txp:category />"><txp:category title="1" link="0" /></a>  (<txp:mdn_count category='<txp:category />' />)</div>
<txp:variable name="catarticles" />
<div class="cjump"><a href="#CategoryArchive"># hop to top</a></div>
</txp:if_variable>
</txp:category_list>
</div><!-- end #CatList -->
</txp:if_category>

</div><!-- end #CategoryArchive -->

textpattern.org :: find and share Textpattern resources
docs.textpattern.io :: Textpattern user documentation

Offline

#9 2017-03-09 09:44:43

alicson
Member
Registered: 2004-05-26
Posts: 465
Website

Re: Categorized list of Live article titles

Bloke wrote #304512:

You’re welcome. btw, <txp:evaluate> hasn’t hit the shelves yet. Next release…

:) Thanks for clarification; I had understood that, I just don’t understand what it will do. I can certainly wait for the release and subsequent documentation/explanations though.. I’m sure it makes perfect sense to others even from the example given, I just haven’t wrapped my head around it yet. (I’m sure it is/will be awesome :) ::excitement::


textpattern.org :: find and share Textpattern resources
docs.textpattern.io :: Textpattern user documentation

Offline

#10 2017-03-09 13:17:24

etc
Developer
Registered: 2010-11-11
Posts: 5,028
Website GitHub

Re: Categorized list of Live article titles

If you use only Category1, to minimize the number of db queries in Stef/Julian solution, the titles can be cached when retrieving the category list:

<txp:variable name="cats"><txp:category_list children="1" wraptag="" break="," sort="title">
  <txp:category /><txp:variable name="dummy" value='<txp:category title="1" />' />
</txp:category_list></txp:variable>

<txp:article_custom category='<txp:variable name="cats" />' match="Category1" sort="Category1, posted desc" limit="999">

<txp:variable name="cat_heading"><txp:if_different><txp:category1 title="1" link="1" /></txp:if_different></txp:if_variable>
<txp:if_variable name="cat_heading" value=""><txp:else />
  <txp:if_first_article><txp:else />
     </ul>
  </txp:if_first_article>
      <h3><txp:variable name="cat_heading" /></h3>
      <ul>
</txp:if_variable>
        <li><txp:permlink><txp:title /></txp:permlink></li>
<txp:if_last_article>
      </ul>
</txp:if_last_article>

</txp:article_custom>

As Stef said, this will only work if category names match their titles. Otherwise, you can use etc_sort plugin, setting sort='<txp:etc_sort field="Category1" order=''<txp:variable name="cats" />'' />, posted desc' in <txp:article_custom />.

Edit: whatever solution, given the number of articles, you should store this list in aks_cache or other caching plugin.

Offline

#11 2017-03-09 15:29:32

michaelkpate
Moderator
From: Avon Park, FL
Registered: 2004-02-24
Posts: 1,379
Website GitHub Mastodon

Re: Categorized list of Live article titles

alicson wrote #304515:

(Wonder if I’ll be able to do the same eventually with the date archives..)

What are you thinking of? I tried using the Wayback Machine but couldn’t find a working example.

Offline

#12 2017-03-09 15:40:06

colak
Admin
From: Cyprus
Registered: 2004-11-20
Posts: 9,007
Website GitHub Mastodon Twitter

Re: Categorized list of Live article titles

Hi… Here’s Michael’s how to to help you start:)


Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.

Offline

Board footer

Powered by FluxBB