Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2008-11-23 16:31:18

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

Exclude current article from related articles in a list page

Apologies for the fugly Post Title. Essentially I have a ‘corporate’ site that only ever displays 1 article at a time. Thus, visit section widget1 (site.com/widget1) and you see the most recent (or possibly earliest, haven’t decided yet) article from that section, and so on for each section. In a sidebar I want a list of links to other articles in that section (plus a few related links from other sections, but that’s irrelevant to my problem). No matter what I do, the currently displayed article is also listed in the sidebar. I must be missing something.

It’s fine if I then go and visit a normal article in that section (e.g. site.com/widget1/plastic-doohicky) because the article is “known” to TXP, so related_articles can see it, as can any code I’d care to fashion myself. But on the very first page of each section I’m stumped (I know I have the duplicate URL problem once in every section using this setup, but I can live with it).

Does anyone have any ideas how I might skip the currently displayed article? Any cunning trickery I can use?

So far:

  • <txp:related_articles /> / <txp:chh_related_articles /> cannot be run on an article list page
  • <txp:recent_articles /> includes the current article
  • <txp:article_custom /> ditto
  • <txp:smd_query /> has got me most of the way there BUT I don’t know what to compare against to tell it to ignore the article being shown — since $thisarticle is empty in an article list (even an article list of 1 article!) and there is no ID or anything article-specific in $pretext I can get my claws on
  • Setting the desired “1st article” as sticky and using a conditional check to display an article_custom with status="sticky" works a treat… but related_articles and recent_articles won’t list stickies so when I’m on other pages in the section I can’t ‘get back’ to that first article from my related list :-(
  • I’d rather not hard-code .htaccess rules for every section to forward them to a named article just so I can use the related_articles tag because it’s a lot of hassle to maintain for non-techs

In my page template for the side panel I want something like:

<txp:if_article_list>
  // Find out which article has been displayed and exclude it from the list
<txp:else />
  <txp:related_articles blah blah />
</txp:if_article_list>

Thing is, TXP knows (somehow) which article it has displayed from the section but I don’t know how it knows and I want to know! :-)

Thanks in advance for any tips (I’m running 4.0.7rc1)

Last edited by Bloke (2008-11-23 16:33:47)


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

#2 2008-11-23 17:06:00

jstubbs
Member
From: Hong Kong
Registered: 2004-12-13
Posts: 2,395
Website

Re: Exclude current article from related articles in a list page

The best way of getting around this for me has been hardcoding an if_article_id like so:

<txp:if_article_id id="33">
	Something here...
</txp:if_article_id>

Its not perfect and I am sure that it can be improved with 4.07…let me think about this.

Offline

#3 2008-11-23 17:58:13

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

Re: Exclude current article from related articles in a list page

Thanks Jonathan. I can’t figure out how I can use that though… would you mind showing me how you’d approach this?

If recent_articles took a form attribute I could perhaps use that technique inside my ‘related’ form to detect if the current article it was trying to display was one from a predefined list and ignore it if so. But it doesn’t take a form :-(

related_articles does take a form now (yay!) but it won’t run on a list page so that’s out of the question.

I’ve also tried adding a ‘fake’ article call using pgonly="1" to see if that populated $thisarticle with the article TXP has chosen to display. Sadly that didn’t work either.

As I alluded to above, my smd_query is the closest so far in that it does exactly what I want in terms of functionality on all pages except when I’m at the site.com/section level. I can see a link in my sidebar to the same article I’m viewing. If I click that article in the list, the page redisplays virtually identically, except now — because I’m viewing an individual article (site.com/section/my-article) — related_articles works and excludes the current article, as I want.

The sticky approach also works well, because the article is missing from the related list when viewing site.com/section (hoorah) but when I click another article in the same section from my related list, the sticky article is not listed.

I could perhaps simulate <txp:related_articles /> with an smd_query and code it up myself so it did the same thing as related articles but used status='4' OR status='5' in the query. Just seems a shame to duplicate (and hard code) an entire tag’s functionality so I can display a sticky in a list! Plus I don’t know what impact setting the first article n every section to sticky has on other parts of the system (e.g. searching, though I think there’s a showsticky attribute in the article tag so I might be ok).

*ponder* I can’t help thinking I’m missing something obvious here…

Last edited by Bloke (2008-11-23 18:00:47)


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

#4 2008-11-23 18:12:19

jstubbs
Member
From: Hong Kong
Registered: 2004-12-13
Posts: 2,395
Website

Re: Exclude current article from related articles in a list page

I think sticky articles must be the way to go. Something like:

<txp:if_article_list>
  // Find out which article has been displayed and exclude it from the list
<txp:article status="sticky" limit="1" />
<txp:article limit="10" />
<txp:else />
  <txp:related_articles blah blah />
</txp:if_article_list> 

Is does not really solve the problem, but if the main article is only to be seen on the section page, then maybe a variation of this could work. Or maybe use custom fields?

This is a tough one to solve..

Last edited by jstubbs (2008-11-23 18:12:49)

Offline

#5 2008-11-23 18:21:13

els
Moderator
From: The Netherlands
Registered: 2004-06-06
Posts: 7,458

Re: Exclude current article from related articles in a list page

Bloke wrote:

I’ve also tried adding a ‘fake’ article call using pgonly="1" to see if that populated $thisarticle with the article TXP has chosen to display. Sadly that didn’t work either.

Are you sure about that? I was just about to suggest using pgonly="1". Hmm… would something like this work?

Your article tag for the full article:

<txp:article form="full-article" limit="1" sort="posted asc" />

Another article form ‘article_id’:

<txp:article_id />

In the sidebar:

<txp:variable name="article_id" value='<txp:article form="article_id" limit="1" sort="posted asc" />' />
<!-- limit and sort attribute values must be identical to those in the full article tag -->

<txp:article limit="10">
	<txp:if_article_id id='<txp:variable name="article_id" />'>
	<txp:else />
		// your code for the article list
	</txp:if_article_id>
</txp:article>

Offline

#6 2008-11-23 18:41:30

mrdale
Member
From: Walla Walla
Registered: 2004-11-19
Posts: 2,215
Website

Re: Exclude current article from related articles in a list page

Bloke>

Another approach would be to use rvm_if_this_article

So I assume you’re building a related links nav that uses an article form. you can just plop this conditional into the form.

<txp:rvm_if_this_article>
nothing or
<li class="hidden"><txp:permlink/></li>
<txp:else />
<li><txp:permlink/></li>
</txp:rvm_if_this_article>

I routinely use this to highlight the active article in a side nav.

Last edited by mrdale (2008-11-23 18:44:42)

Offline

#7 2008-11-23 18:56:47

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

Re: Exclude current article from related articles in a list page

Els wrote:

would something like this work? <snip txp:variable voodoo>

You
Are
Totally
My
Heroine

:-)

Genius bit of cheating with the new tags there, love it. I threw your txp:variable assignment at the top of the form, which dutifully assigned the article id that TXP was about to use, to the article_id variable.

Then later — in my smd_query that fakes related_articles on article list pages — I simply inserted the value from that txpvar in a ID != '?article_id' clause and it worked flawlessly (well, I had to make a small mod to smd_query to allow it read txp:variables natively, because otherwise it involved some more skulduggery I wasn’t prepared to put up with… this new version of smd_query will be out next week).

Totally awesome, thanks Els!

Happy days… happy days…

mrdale

Thanks for the tip. Forgot to say I looked at that plugin as well but couldn’t get it to work on a site.com/section page.

EDIT: It works within the form of an article/article_custom list, but my related articles sidebar is not part of that form. It is generated outside it. I think a lot of my problem stems from that fact and perhaps I should rewrite the entire structure so that the sidebars can be part of the default form, but my CSS isn’t good enough to put the centre column first and float the two sidebars; its much easier to render the sidebars first and slot the central column in the gap. Lazy / ignorant me…

Last edited by Bloke (2008-11-23 19:10:28)


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 2008-11-23 19:00:00

mrdale
Member
From: Walla Walla
Registered: 2004-11-19
Posts: 2,215
Website

Re: Exclude current article from related articles in a list page

Damn! I wanted to be a heroine… :)

So my example actually depends on an individual article context. RTFP next time.

Offline

#9 2008-11-23 19:04:15

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

Re: Exclude current article from related articles in a list page

Well, kind of. It probably does work if I’d laid my page out properly *shrug*. See Edit to the post above.

Last edited by Bloke (2008-11-23 19:09:43)


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

#10 2008-11-23 19:44:01

els
Moderator
From: The Netherlands
Registered: 2004-06-06
Posts: 7,458

Re: Exclude current article from related articles in a list page

Bloke wrote:

… Heroine

I’m flattered :)

perhaps I should rewrite the entire structure so that the sidebars can be part of the default form, but my CSS isn’t good enough to put the centre column first and float the two sidebars; its much easier to render the sidebars first and slot the central column in the gap. Lazy / ignorant me…

This change of your page structure wouldn’t be necessary I think if you could avoid to use the section page and only serve the individual article, which can be done with redirects in .htaccess or using php location redirect (as long as you know which article to redirect to). In individual article context the other solutions would all have worked ;)
It would also prevent duplicate URLs.

Offline

Board footer

Powered by FluxBB