Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2025-01-07 08:40:40

peterj
Member
From: Melbourne, Australia
Registered: 2005-06-02
Posts: 102

txp:link_to_next (& prev) context attribute

Hi,
I have a client who would like the pagination links on individual articles constrained to category1. I thought this wasn’t possible and was about to recommend other tacks – eg showing related posts. Then in the docs I spotted a context attribute introduced at 4.7.2 that I managed to miss:

“… Useful jointly with the permlink tag in individual article forms when one needs to restrain navigation to posts with the current article category1.”

It looked like it might be very handy but I can’t make sense of the description of this attribute in the docs, or the inline example.

I was hoping it could do something like this:

<txp:link_to_next context="category1">&nbsp;Next &#187;</txp:link_to_next>

When I try this, nothing different happens, so assuming I’m misunderstanding the attribute?

Offline

#2 2025-01-07 19:27:55

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

Re: txp:link_to_next (& prev) context attribute

Filtering is not really the purpose of context, the latter just handles URL parameters in standard txp permlinks. Instead, you can try appending category filter to <txp:article /> in relevant pages:

<txp:if_individual_article>
    <txp:article category='<txp:category1 />' />
</txp:if_individual_article>

Then links to next/prev article should respect this filter. For fine-tuning, it is possible to add match="Category1" too. But it might be a 4.9 thing.

Offline

#3 2025-01-08 07:34:28

peterj
Member
From: Melbourne, Australia
Registered: 2005-06-02
Posts: 102

Re: txp:link_to_next (& prev) context attribute

Hi,
thanks. Unfortunately that gave me “Unknown tag attribute: category”. I ended up trying a different tack, using article_custom to find the previous and next links. Seems to work so far.

<!-- if this individual article has a populated category1 -->
<txp:if_individual_article>
	<txp:if_article_category number="1">

		<txp:variable name='page_id' value='<txp:article_id />'/>
		<txp:variable name='stop_next_round' value='no' />

		<!-- Using stm_article_order for article custom sorting -->
		<txp:article_custom limit='999' category='<txp:category1 />' sort='position asc'>

			<txp:if_variable name='stop_next_round' value='yes'>
				<txp:hide>A match was found on last round. Collect next_id if there is one and 'exit'/txp:hide>
				<txp:variable name='next_id' value='<txp:article_id />' />
				<txp:variable name="stop_next_round" value="do nothing" />
			</txp:if_variable>

			<txp:if_variable name='stop_next_round' value='no'>
				<txp:if_variable name='page_id' value='<txp:article_id />'>
					<txp:hide>This article matches so gather next_id on next round then 'exit'</txp:hide>
					<txp:variable name="stop_next_round" value="yes" />
				<txp:else />
					<txp:hide>Not a match so store this id as prev_id for next round</txp:hide>
					<txp:variable name='prev_id' value='<txp:article_id />' />
				</txp:if_variable>
			</txp:if_variable>

		</txp:article_custom>

		<txp:if_variable name="prev_id">
			<txp:permlink id='<txp:variable name="prev_id" />'>&#171; Previous&nbsp;</txp:permlink>
		</txp:if_variable>
		<txp:if_variable name="next_id">
			<txp:permlink id='<txp:variable name="next_id" />'>&nbsp;Next &#187;</txp:permlink>
		</txp:if_variable>

	</txp:if_article_category>
</txp:if_individual_article>

Offline

#4 2025-01-08 08:33:07

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

Re: txp:link_to_next (& prev) context attribute

I’m not sure if the issue is with the use of quotes. Try:

<!-- if this individual article has a populated category1 -->
<txp:if_individual_article>
	<txp:if_article_category number="1">

		<txp:variable name="page_id" value="<txp:article_id />" />
		<txp:variable name='stop_next_round' value="no" />

		<!-- Using stm_article_order for article custom sorting -->
		<txp:article_custom limit="999" category="<txp:category1 />" sort="position asc">

			<txp:if_variable name="stop_next_round" value="yes">
				<txp:hide>A match was found on last round. Collect next_id if there is one and 'exit'/txp:hide>
				<txp:variable name="next_id" value="<txp:article_id />" />
				<txp:variable name="stop_next_round" value="do nothing" />
			</txp:if_variable>

			<txp:if_variable name="stop_next_round" value="no">
				<txp:if_variable name="page_id" value="<txp:article_id />">
					<txp:hide>This article matches so gather next_id on next round then 'exit'</txp:hide>
					<txp:variable name="stop_next_round" value="yes" />
				<txp:else />
					<txp:hide>Not a match so store this id as prev_id for next round</txp:hide>
					<txp:variable name="prev_id" value="<txp:article_id />" />
				</txp:if_variable>
			</txp:if_variable>

		</txp:article_custom>

		<txp:if_variable name="prev_id">
			<txp:permlink id='<txp:variable name="prev_id" />'>&#171; Previous&nbsp;</txp:permlink>
		</txp:if_variable>
		<txp:if_variable name="next_id">
			<txp:permlink id='<txp:variable name="next_id" />'>&nbsp;Next &#187;</txp:permlink>
		</txp:if_variable>

	</txp:if_article_category>
</txp:if_individual_article>

Edit: Also, As far as I know, there is no position attribute to article_custom.

Last edited by colak (2025-01-08 08:36:04)


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

Offline

#5 2025-01-08 15:56:38

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

Re: txp:link_to_next (& prev) context attribute

peterj wrote #338711:

Unfortunately that gave me “Unknown tag attribute: category”.

Then it’s definitively a 4.9 thing. You can cheat, adding

<txp:php>
    global $pretext, $thisarticle;
    $pretext['c'] = $thisarticle['category1'];
</txp:php>

before (individual) <txp:article /> tag, and

<txp:link_to_prev context="id" /> / <txp:link_to_next context="id" />

after (or within) it.

I ended up trying a different tack, using article_custom to find the previous and next links. Seems to work so far.

That’s fine and more robust than a hack, just pity to loop over all articles to retrieve the neighbours.

Offline

#6 Yesterday 06:59:50

peterj
Member
From: Melbourne, Australia
Registered: 2005-06-02
Posts: 102

Re: txp:link_to_next (& prev) context attribute

Thanks both,

Etc, I’ll try that on another site soon. This is a small site (on 4.8.8) so not much looping to do.

Colak, the sort=“position asc” value comes from the stm_article_order plugin, I fogot to remove it or mention. It allows the client to adjust article ordering within sections. Quite old but still works.

Offline

Board footer

Powered by FluxBB