Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
Search: Articles on sections or category landing pages
I was wondering if there was a better way to get proper urls in search results. At the moment, I’m using the code below (simplified), but as I have over 10 articles whose urls should point to a section, category or sub-category landing pages, I feel that it is not sustainable.
<txp:if_article_id id="2001">
<div class="line">
<h3 class="entry-title"><a href="<txp:site_url />about/">About</a></h3>
</div>
<txp:else />
<txp:if_article_id id="2310">
<div class="line">
<h3 class="entry-title"><a href="<txp:site_url />projects/toolkit-of-care/">Toolkit of Care</a></h3>
</div>
<txp:else />
<txp:if_article_id id="2243">
<div class="line">
<h3 class="entry-title"><a href="<txp:site_url />projects/respublika/seminars/">R! Seminars</a></h3>
</div>
<txp:else />
...
<txp:else />
<div class="line">
<h3 class="entry-title"><txp:permlink><txp:title /></txp:permlink></h3>
</div>
...
</txp:if_article_id>
</txp:if_article_id>
</txp:if_article_id>
Last edited by colak (2025-01-05 06:22:08)
Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.
Offline
Re: Search: Articles on sections or category landing pages
Happy New Year Yiannis … and all at the forum :-)
Yes, that looks like a potential maintenance nightmare. If your landing pages are sticky articles, maybe you could first detect their sticky status (see this snippet), and then use if_article_section and if_article_category to output the href according to certain patterns?
You could probably reduce some of the code repetition by setting a variable with the href-link in your if_… conditionals and then outputting the code for the search results output at the end.
TXP Builders – finely-crafted code, design and txp
Offline
Re: Search: Articles on sections or category landing pages
Thanks so much Julian.
Happy new year to all!!!!
I know that not all articles are sticky as I use 3 different methods. I’ll have to experiment to see what happens when I make them sticky.
Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.
Offline
Re: Search: Articles on sections or category landing pages
I changed all articles in the projects section except 1 to sticky and all works fine except the search.
This is my (simplified) construct which I think that it should work. The search results say that 1 article was found (as I added some gibberish to limit what is returned) but the link is not shown.
<txp:if_article_id id="2001"> <txp:hide>THis is a non sticky article</txp:hide>
<p><a href="<txp:site_url />about/"><txp:site_url />about/</a></p>
<txp:else />
<txp:if_article_section name="projects">
<txp:if_custom_field name="status" value="5">
<txp:if_article_category category="<txp:category>" number="1"><txp:variable name="articlecategory1"><txp:category1 /></txp:variable></txp:if_article_category>
<txp:if_article_category category="<txp:category>" number="2"><txp:variable name="articlecategory2"><txp:category2 /></txp:variable></txp:if_article_category>
<txp:if_variable name="articlecategory2">
<p><a href="<txp:site_url />projects/<txp:variable name="articlecategory1" />/<txp:variable name="articlecategory2" />/"></a></p>
<txp:if_article_category
<txp:else />
<txp:if_variable name="articlecategory2" not>
<txp:if_article_category name='<txp:variable name="articlecategory1" />' number="1">
<p class="small"><a href="<txp:site_url />projects/<txp:variable name="articlecategory1" />/"></a></p>
</txp:if_article_category>
</txp:if_variable>
</txp:if_variable>
<txp:else />
<p><txp:permlink><txp:permlink /></txp:permlink></p>
</txp:if_custom_field>
</txp:if_article_section>
</txp:if_article_id>
ps. I do have <txp:article pgonly="1" searchall="0" searchsticky="1" />
in the template.:)
Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.
Offline
Re: Search: Articles on sections or category landing pages
You have an unclosed txp:if_article_category
in there (maybe just a copy-paste omission) and I’m also not sure if the category="<txp:category />"
part is necessary, as you won’t be in a category context in the search results. Some of the a
tags lack the actual link text so may output correctly in the source but not show on screen.
But I wonder if you can simplify the entire “if two categories, show this, if just one, then this” construction but making use of the mothership placeholder in a wraptag
attribute to simplify the output, e.g. output each categoryN tag with wraptag="<+>/"
so that it only outputs if it exists. Something like this:
projects/<txp:category1 wraptag="<+>/" /><txp:category2 wraptag="<+>/" />
which should give you projects/
or projects/category1-name/
or projects/category1-name/category2-name/
depending on the article.
All together, that could be:
<txp:variable name="href" value="" />
<txp:if_article_id id="2001">
<txp:hide>This is a non-sticky article</txp:hide>
<txp:variable name="href">about/</txp:variable>
<txp:else />
<txp:if_article_section name="projects">
<txp:if_custom_field name="status" value="5">
<txp:variable name="href">projects/<txp:category1 wraptag="<+>/" /><txp:category2 wraptag="<+>/" /></txp:variable>
<txp:else />
<txp:variable name="href">projects/<txp:article_url_title /></txp:variable>
</txp:if_custom_field>
</txp:if_article_section>
</txp:if_article_id>
<p><a href="<txp:site_url /><txp:variable name="href" />">/<txp:variable name="href" /></a></p>
TXP Builders – finely-crafted code, design and txp
Offline
Re: Search: Articles on sections or category landing pages
Still struggling with this, and reverted to snippets I understand:)
<txp:if_article_id id="2001">
<h3 class="entry-title"><a href="<txp:site_url />about/">About</a></h3>
<txp:else />
<txp:if_article_section name="projects">
<txp:if_custom_field name="status" value="5">
<txp:if_custom_field name="category1"><txp:variable name="articlecategory1"><txp:custom_field name="category1" /></txp:variable></txp:if_custom_field>
<txp:if_custom_field name="category2"><txp:variable name="articlecategory2"><txp:custom_field name="category2" /></txp:variable></txp:if_custom_field>
<h3 class="entry-title"><a href="<txp:site_url />projects/<txp:if_variable name="articlecategory1"><txp:variable name="articlecategory1" />/</txp:if_variable><txp:if_variable name="articlecategory2"><txp:variable name="articlecategory2" />/</txp:if_variable>"><txp:title /></a></h3>
</txp:if_custom_field>
</txp:if_article_section>
<txp:else />
<h3 class="entry-title"><txp:permlink><txp:title /></txp:permlink></h3>
</txp:if_article_id>
The bove, should work but for some reason it does not:( My only suspicion is that the quotations for the href may be messing up the construction.
Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.
Offline
Re: Search: Articles on sections or category landing pages
Try shifting the </txp:if_article_section>
two lines further down to after the second h3
. Currently you have two txp:else statements for if_article_id.
TXP Builders – finely-crafted code, design and txp
Offline
Re: Search: Articles on sections or category landing pages
jakob wrote #338695:
Try shifting the
</txp:if_article_section>
two lines further down to after the secondh3
. Currently you have two txp:else statements for if_article_id.
Still the same result.
You searched for aaaab. 1 article found.
which is returned without a url.
Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.
Offline
Re: Search: Articles on sections or category landing pages
The beauty of txp is that it allows us to try different different things:)
<txp:if_article_id id="2001">
<h3 class="entry-title"><a href="<txp:site_url />about/">About</a></h3>
<txp:else />
<txp:hide>is sticky, has one category and resides in its landing page</txp:hide>
<txp:if_article_id="2234,2237,2243,2248,2255">
<h3 class="entry-title"><a href="<txp:site_url />projects/<txp:category1 />/"><txp:title /></a></h3>
<txp:else />
<txp:hide>is sticky, has two categories and resides in the landing page of the second category using the contraction below</txp:hide>
<txp:if_article_id="2231,2232,2233,2287,2293,2310">
<h3 class="entry-title"><a href="<txp:site_url />projects/<txp:category1 />/<txp:category2 />/"><txp:title /></a></h3>
<txp:else />
<txp:hide>Articles in sections without any category</txp:hide>
<h3 class="entry-title"><txp:permlink><txp:title /></txp:permlink></h3>
</txp:if_article_id>
</txp:if_article_id>
</txp:if_article_id>
Unfortunately that above does not work either:(
Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.
Offline
Re: Search: Articles on sections or category landing pages
Hmm. Long shot: do you have the section set to be searchable and to appear on the front page?
If you look at the dev demo fairly soon after I posted this (i.e. before it gets reset), you should see a working search setup using more or less this setup. The demo doesn’t seem to properly resolve clean urls, so the links return an error when you click on them, but if you roll over the titles, you should see them as correct.
I set up landing pages for the “Projects” section and for “Hope”, “Alright”, and “Hope is alright” category combinations . Each of them is a sticky article assigned to the projects section and corresponding category or categories. I also set up project alpha, beta, gamma articles as live articles assigned to the respective categories and projects section. And there is a general “about” article too. Try searching for “project”, “hope”, for “alright”, for “alpha”, “beta”, “gamma” and “about” and you should get relevant search results.
I’ll post some screenshots shortly should the demo site have been reset. Here they are:
yiannis_search-results_form-code
yiannis_search-results_section-setup
yiannis_search-results_articles
yiannis_search-results_about
yiannis_search-results_project
yiannis_search-results_hope-is-alright
yiannis_search-results_alright
yiannis_search-results_hope
The only other thing I did was add searchsticky="1"
to the txp:article tag in the search_display
form.
TXP Builders – finely-crafted code, design and txp
Offline
Re: Search: Articles on sections or category landing pages
colak wrote #338694:
Still struggling with this, and reverted to snippets I understand:)
jakob wrote #338695:
Try shifting the
</txp:if_article_section>
two lines further down to after the secondh3
. Currently you have two txp:else statements for if_article_id.
If you still want to try your earlier variant, my advice was slightly wrong. Both your </txp:if_custom_field>
and </txp:if_article_section>
must go in the second-to-last line, because your final h3 is for the regular “live” project article. Something like this:
<txp:if_article_id id="2001">
<h3 class="entry-title"><a href="<txp:site_url />about/">About</a></h3>
<txp:else />
<txp:if_article_section name="projects">
<txp:if_custom_field name="status" value="5">
<txp:if_custom_field name="category1"><txp:variable name="articlecategory1"><txp:custom_field name="category1" /></txp:variable></txp:if_custom_field>
<txp:if_custom_field name="category2"><txp:variable name="articlecategory2"><txp:custom_field name="category2" /></txp:variable></txp:if_custom_field>
<h3 class="entry-title"><a href="<txp:site_url />projects/<txp:if_variable name="articlecategory1"><txp:variable name="articlecategory1" />/</txp:if_variable><txp:if_variable name="articlecategory2"><txp:variable name="articlecategory2" />/</txp:if_variable>"><txp:title /></a></h3>
<txp:else />
<h3 class="entry-title"><txp:permlink><txp:title /></txp:permlink></h3>
</txp:if_custom_field>
</txp:if_article_section>
</txp:if_article_id>
That said, in my above test, I had no problem simply using <txp:category1 />
and <txp:category2 />
without needing to access those values through the custom_field workaround.
TXP Builders – finely-crafted code, design and txp
Offline
Re: Search: Articles on sections or category landing pages
I really do not know why it does not work! Ive tried all the above but to no avail. I’ll give it a break and move to something else as it may clear my brain:)
Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.
Offline