Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2011-07-07 19:08:17

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

[howto] show articles from category and augment with articles from section

I don’t think that this can be achieved out of the box.

Once in an article, show 4 random related articles (same section+category) and show their linked titles. Then fill the remaining <li>s with articles by the same section ideally omitting articles from the same category as the article in view – so as to prevent duplicate links.

The code bellow is where I am at the moment. Is there a way to exclude articles from current article_category in the article_custom list? (I know that exclude is not an attribute of the tag – but nevertheless are there any suggestions/alternative methods using other tags?)

<txp:variable name="relatedtext" value='<txp:related_articles limit="1" />' />
<txp:if_variable name="relatedtext" value="">
<txp:article_custom limit="10" section='<txp:section />' wraptag="ul" break="li" sort="rand()" class="related_articles">
<txp:permlink><txp:title /></txp:permlink>
</txp:article_custom>
<txp:else />
<ul class="related_articles">
<txp:related_articles limit="4" sort="rand()" wraptag="" break="li" section='<txp:article_section />'/>
<txp:article_custom limit="6" section='<txp:section />' wraptag="" break="li" sort="rand()">
<txp:permlink><txp:title /></txp:permlink>
</txp:article_custom>
</ul>
</txp:if_variable>

Does anyone have an idea?


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

Offline

#2 2011-07-07 19:50:32

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

Re: [howto] show articles from category and augment with articles from section

thinking aloud here. Maybe you can do this in two steps.

1. Make a variable consisting of just the article ids of the related articles separated with break=",".
Make a second variable consisting of 10 random article ids of articles in the desired section, again separated with break=",". You’ll have two variables that look like 3,7,4,14 and 45,23,3,21,33,4,9,17,28,40 – by way of example. Use some sly php (to be googled, or perhaps to be found here) to remove any IDs from the first variable that are present in the second variable, e.g. in the above example 3 and 4. Then select 6 random numbers of those still remaining in the second variable.

2. use txp:article_custom with the id attribute, e.g. id='<txp:variable name="rel_articles" />,<txp:variable name="section_articles" />' to output your link list.

There’s a bit of php array comparison to be done there, but maybe that’s a start.


TXP Builders – finely-crafted code, design and txp

Offline

#3 2011-07-08 07:32:40

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

Re: [howto] show articles from category and augment with articles from section

Hi jakob,

What you are suggesting sounds interesting but I’m not sure as to how to implement it.

Here’s an unsuccessful attempt without the php

<txp:variable name="relatedtexts" value='<txp:related_articles limit="10" break=",">
<txp:article_id />
</txp:related_articles>' />
<txp:variable name="moretexts" value='<txp:article_custom limit="10" section='<txp:section />' break="," sort="rand()">
<txp:article_id />
</txp:article_custom>' />
<txp:php></txp:php>
<txp:article_custom id='<txp:variable name="relatedtexts" />,<txp:variable name="moretexts" />'  wraptag="ul" break="li" limit="10" class="related_articles">
<txp:permlink><txp:title /></txp:permlink>
</txp:article_custom>

According to the textbook “when a list is supplied, this does not imply a sort order.” which is a pity as – if the above code worked – the 1st 10 articles would have been displayed. At the moment, nothing is displayed anyway:)


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

Offline

#4 2011-07-08 10:01:08

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

Re: [howto] show articles from category and augment with articles from section

Pressed for time now, but try using two single quotes around the second nested tag txp:section.
For the second part Jeff made a suggestion in this thread.

<txp:variable 
     name="relatedtexts" 
     value='<txp:related_articles limit="4" break=","><txp:article_id /></txp:related_articles>'
     />
<txp:variable 
     name="moretexts" 
     value='<txp:article_custom limit="10" section=''<txp:section />'' break="," sort="rand()"><txp:article_id /></txp:article_custom>'
     />

<!--- debugging: verify output of variables -->
<p>relatedtext IDs: <txp:variable name="relatedtexts" /></p>
<p>moretext IDs: <txp:variable name="moretexts" /></p>

<!-- php goes here -->

<txp:article_custom 
     wraptag="ul" break="li" limit="10" class="related_articles" 
     id='<txp:variable name="relatedtexts" />,<txp:variable name="moretexts" />'  
     sort='field('ID',<txp:variable name="relatedtexts" />,<txp:variable name="moretexts" />)'
     >
   <txp:permlink><txp:title /></txp:permlink>
</txp:article_custom>

For the php look up php array_diff, array_rand, possibly also array_unique and array_merge. Jukka’s post linked to above shows how to make an array out a comma-separated list, and how to make a comma-separated list of the manipulated array.


TXP Builders – finely-crafted code, design and txp

Offline

#5 2011-07-08 10:45:16

uli
Moderator
From: Cologne
Registered: 2006-08-15
Posts: 4,304

Re: [howto] show articles from category and augment with articles from section

colak wrote:

According to the textbook “when a list is supplied, this does not imply a sort order.” which is a pity as – if the above code worked – the 1st 10 articles would have been displayed.

Does this maniquish code help?


In bad weather I never leave home without wet_plugout, smd_where_used and adi_form_links

Offline

#6 2011-07-08 12:43:42

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

Re: [howto] show articles from category and augment with articles from section

Hey Jokob, Uli

Thanks sooo much!

Ok this kind of works: There was a typo in Jeff’s post. Single quotes are not needed around the id for the sort

<txp:variable name="relatedtexts" 
   value='<txp:related_articles limit="10" break=","><txp:article_id /></txp:related_articles>'/>

<txp:variable name="moretexts" 
   value='<txp:article_custom limit="10" section=''<txp:section />'' break="," sort="rand()"><txp:article_id /></txp:article_custom>' />

<!-- php to compare and omit duplicate IDs which come form the article_custom tag goes here -->

<txp:article_custom wraptag="ul" break="li" limit="10" class="related_articles" 
   id='<txp:variable name="relatedtexts"/>,<txp:variable name="moretexts" />' 
   sort='field(ID,<txp:variable name="relatedtexts" />,<txp:variable name="moretexts" />)'>
      <txp:permlink><txp:title /></txp:permlink>
</txp:article_custom>

All is needed now is the php to compare if any id appears twice


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

Offline

#7 2011-07-08 22:29:20

uli
Moderator
From: Cologne
Registered: 2006-08-15
Posts: 4,304

Re: [howto] show articles from category and augment with articles from section

colak wrote:

All is needed now is the php to compare if any id appears twice

In case I understood a small handful: Wouldn’t it be possible to instead filter the relatedtexts from the moretexts by surrounding <txp:article_id /> with if_article_id id="your related text IDs", like so:

<txp:variable name="moretexts" value='<txp:article_custom limit="10" section=''<txp:section />'' break="," sort="rand()"><txp:if_article_id id='<txp:variable name="relatedtexts" />'><txp:else /><txp:article_id /></txp:if_article_id></txp:article_custom>' />

Untested, just for the fun of solving weekend riddles, tired ;)


In bad weather I never leave home without wet_plugout, smd_where_used and adi_form_links

Offline

#8 2011-07-09 10:26:38

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

Re: [howto] show articles from category and augment with articles from section

Hey Uli,

I’d be surprised if it does work but I will try it this evening:)


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

Offline

#9 2011-07-10 07:43:50

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

Re: [howto] show articles from category and augment with articles from section

Just to let you know that I tested your suggestion but it did not work.


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

Offline

#10 2011-07-10 10:35:39

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

Re: [howto] show articles from category and augment with articles from section

solved: and the final code is:

<txp:variable name="relatedtexts" 
	value='<txp:related_articles limit="10" break=",">
		<txp:article_id />
	</txp:related_articles>'/>
<txp:variable name="moretexts" 
	value='<txp:article_custom 
		limit="10" section=''<txp:section />'' 
		break="," sort="rand()">
	<txp:article_id />
	</txp:article_custom>' />

<txp:if_variable name="relatedtexts" value="">
<txp:article_custom limit="10" section='<txp:section />' break="li" wraptag="ul" sort="rand()" class="related_articles">
<txp:permlink><txp:title /></txp:permlink>
</txp:article_custom>
<txp:else />

<txp:article_custom 
	wraptag="ul" 
	break="li" 
	limit="10" 
	class="related_articles" 
	id='<txp:rah_repeat offset="1" 
	break="," duplicates="1" 
	sort="regular asc" 
		value=''<txp:variable name="relatedtexts"/>,<txp:variable name="moretexts" />''>
		<txp:rah_repeat_value />
	</txp:rah_repeat>' 
		sort='field(ID,<txp:variable name="relatedtexts" />,<txp:variable name="moretexts" />)'>
   <txp:permlink><txp:title /></txp:permlink>
</txp:article_custom>
</txp:if_variable>

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

Offline

#11 2011-07-10 10:36:29

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

Re: [howto] show articles from category and augment with articles from section

uli, your suggestion looked good to me, except that you might not get exactly 10 results. Can’t see off-hand why it doesn’t work.

Yiannis, try this:

<txp:variable name="relatedtexts" 
     value='<txp:related_articles limit="4" break=","><txp:article_id /></txp:related_articles>' />
<txp:variable name="moretexts" 
     value='<txp:article_custom limit="14" section=''<txp:section />'' break="," sort="rand()"><txp:article_id /></txp:article_custom>' />

<txp:php>
// make arrays out of the comma-separated variables

   $related_texts = explode(',',variable(array('name' => 'relatedtexts')));
   $more_texts = explode(',',variable(array('name' => 'moretexts')));

// if there are related_texts (= cleaned array contains something)

   if ( array_filter($related_texts) ) {
     // remove any duplicates from more texts
     $more_diff = array_diff($more_texts,$related_texts);
     // combine both arrays one after the other
     $further_reading = array_merge($related_texts,$more_diff);
   } else {
     // just use the more texts
     $further_reading = $more_texts;
   }

// shorten array to ten items

   $further_reading = array_slice($further_reading, 0, 10);

// create a new comma-separated variable from the array

   variable(array('name' => 'furtherreading', 'value' => implode(',',$further_reading) ));
</txp:php>

<txp:article_custom wraptag="ul" break="li" class="related_articles" 
     id='<txp:variable name="furtherreading" />'  
     sort='field(ID,<txp:variable name="furtherreading" />)' >
   <txp:permlink><txp:title /></txp:permlink>
</txp:article_custom>

That should work (if not I’ve made a typo somewhere as it works in php in a non-txp environment). It looks more than it is due to the comments and breaking it down into individual steps – some of the steps can be combined into single steps.

The above should always give you ten results with the first 0-4 being related articles, which is why you need to get 4 ‘related texts’ and 14 random ‘more texts’ at the beginning. You don’t need the limit="10" at the end because the array is shortened to ten items in the php beforehand.

Last edited by jakob (2011-07-10 10:47:28)


TXP Builders – finely-crafted code, design and txp

Offline

#12 2011-07-10 11:25:40

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

Re: [howto] show articles from category and augment with articles from section

Hi Jakob,

the idea of 4+6 was an idea of necessity which doesn’t even make any sense:)

Using rah_repeat and no raw php seems to have solved the problem.


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