Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2012-01-24 22:34:50

johnstephens
Plugin Author
From: Woodbridge, VA
Registered: 2008-06-01
Posts: 1,000
Website

Check for previous and next articles in an article_custom list

Is it possible to check for previous and next articles in the article form of a list generated by article_custom?

I’m using article_custom to display list of related articles based on a custom field. I want to use jQuery to show only one item at a time, and toggle through the articles with Previous and Next buttons. I thought I could define a variable using Txp’s link_to_prev and link_to_next tags, but those seems to be pulling data from the individual article page and not from the related article in context.

I’m using this code to list related articles:

<txp:article_custom
    form="_related-articles"
    is_index=""
    sort="posted asc"
    subsection='<txp:custom_field name="related"/>'
    wraptag=""/>

Here’s the code I’m using in the related articles form:

<!-- My article tags are here -->

<txp:if_custom_field name="is_index" value="">
<txp:variable name="has_next"><txp:link_to_next/></txp:variable>
<txp:variable name="has_prev"><txp:link_to_prev/></txp:variable>

<txp:if_variable name="has_prev" value="">
<txp:else/>
<txp:adi_calc name="article-count" subtract="1"/>
<button id="gotoStep<txp:variable name="article-count"/>">Previous</button>
<txp:adi_calc name="article-count" add="1"/>
</txp:if_variable>

<txp:if_variable name="has_next" value="">
<txp:else/>
<txp:adi_calc name="article-count" add="1"/>
<button id="gotoStep<txp:variable name="article-count"/>">Next</button>
<txp:adi_calc name="article-count" subtract="1"/>
</txp:if_variable>

The button ids are tied to JS functions to show and hide. The script works, but the buttons are showing up based on the previous and next articles compared to the page, not in reference to the article in scope.

Offline

#2 2012-01-24 23:00:12

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

Re: Check for previous and next articles in an article_custom list

johnstephens wrote:

Is it possible to check for previous and next articles in the article form of a list generated by article_custom?

You could try <txp:page_url type="prev_id" /> and <txp:page_url type="next_id" /> in a variable/if_variable construction.

Source is this topic, especially this post by Gocom. Quite interesting what evolved from a simple subject.


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

Offline

#3 2012-01-25 03:15:49

johnstephens
Plugin Author
From: Woodbridge, VA
Registered: 2008-06-01
Posts: 1,000
Website

Re: Check for previous and next articles in an article_custom list

Thanks, Uli! Unfortunately, this gives me the same results as using <txp:link_to_next/> and <txp:link_to_prev/>: Since it’s in the related links form on an individual article page, it uses the articles adjacent to the individual article page and populates all the articles called by the article_custom tag witht he same data.

Offline

#4 2012-01-25 06:07:15

wet
Developer Emeritus
From: Schoerfling, Austria
Registered: 2005-06-06
Posts: 3,376
Website GitHub Mastodon

Re: Check for previous and next articles in an article_custom list

johnstephens wrote:

Is it possible to check for previous and next articles in the article form of a list generated by article_custom?

Not without resorting to some custom PHP/SQL.

Offline

#5 2012-01-25 06:55:12

johnstephens
Plugin Author
From: Woodbridge, VA
Registered: 2008-06-01
Posts: 1,000
Website

Re: Check for previous and next articles in an article_custom list

wet wrote:

johnstephens wrote:

“Is it possible to check for previous and next articles in the article form of a list generated by article_custom?”

Not without resorting to some custom PHP/SQL.

Thanks for the heads up! My PHP chops are better than they used to be, so that doesn’t strike terror into my heart. But I’m not sure where to start.

Offline

#6 2012-01-25 07:28:58

wet
Developer Emeritus
From: Schoerfling, Austria
Registered: 2005-06-06
Posts: 3,376
Website GitHub Mastodon

Re: Check for previous and next articles in an article_custom list

I think getNeighbour() would be a nice starting point. NB: I’ve started to generalize on its use cases in the current development branch.

Offline

#7 2012-03-21 20:21:31

THE BLUE DRAGON
Member
From: Israel
Registered: 2007-11-16
Posts: 638
Website

Re: Check for previous and next articles in an article_custom list

Hi I’m also looking for this, did you made it and can share how please?

My idea is to store the current article-id in a txp:variable
and list all the articles IDs in a comma separated list using article_custom,
then somehow get the next&prev IDs from the list with the variable we created before
and then again use article_custom to output the 2 articles (prev&next) and display as we want.
But I don’t know PHP and how to get the next & prev values from that “array”, so I will really like your help please.

EDIT: I made it doing this:

<txp:article>
	// Current article ID
	<txp:variable name="current_id" value='<txp:article_id />' />
~
	// Create a variable with all the list of articles IDs separated by a comma
	<txp:variable name="all_ids"><txp:article_custom section="my-section" category="my-category" customfieldname="my-value" sort="Title asc" limit="999"><txp:article_id /><txp:if_last_article><txp:else />,</txp:if_last_article></txp:article_custom></txp:variable>
~
	// PHP search in array using the variables current_id & all_ids we created
	<txp:php>
		global $variable;
		$the_ids = $variable['all_ids'];
		$id_list = explode(',', $the_ids);
		$this_id = array_search($variable['current_id'], $id_list);
		$cnext_id = $id_list[$this_id + 1];
		$cprev_id = $id_list[$this_id - 1];
		if(in_array($cnext_id,$id_list) == FALSE){$cnext_id = $id_list[0];}
		if(in_array($cprev_id,$id_list) == FALSE){$cprev_id = end($id_list);}
~
		$variable['custom_prev_article_id'] = $cprev_id;
		$variable['custom_next_article_id'] = $cnext_id;
	</txp:php>
~
	// Prev link
	<txp:article_custom id='<txp:variable name="custom_prev_article_id" />'>
		<txp:permlink>prev</txp:permlink>
	</txp:article_custom>
~
	// Next link
	<txp:article_custom id='<txp:variable name="custom_next_article_id" />'>
		<txp:permlink>next</txp:permlink>
	</txp:article_custom>
</txp:article>

Last edited by THE BLUE DRAGON (2012-03-23 19:51:33)

Offline

#8 2012-03-22 20:38:45

johnstephens
Plugin Author
From: Woodbridge, VA
Registered: 2008-06-01
Posts: 1,000
Website

Re: Check for previous and next articles in an article_custom list

I did not find a way to get Textpattern to tell me adjacent article info. My solution was to use adi_calc to generate a unique ID for each article as it was listed:

<txp:variable name="article_count" value="0"/>
<txp:article_custom
    form="_related-articles"
    sort="posted asc"
    subsection='<txp:custom_field name="related"/>'
    status="live"
    wraptag=""/>

Form: _related-articles

<txp:adi_calc name="article_count" add="1"/>
<div class='related-article' id='step-<txp:variable name="article_count"/>'>
    <!-- My article content -->
</div> <!-- end of step-<txp:variable name="article_count"/> -->

Then use a similar article_custom to list the navigation links to each article’s ID-based anchor:

<txp:variable name="article_count" value="0"/>
<ul id='related-pager'>
<txp:article_custom
    break="li"
    sort="posted asc"
    subsection='<txp:custom_field name="related"/>'
    status="live"
    wraptag=""><txp:adi_calc name="article_count" add="1"/><a class='related-nav gotoStep<txp:variable name="article_count"/>' href='#step-<txp:variable name="article_count"/>'><txp:variable name="article_count"/></a></txp:article_custom>
</ul>

Finally, I used jQuery to assign ‘prev’ and ‘next’ classes to the navigation links adjacent to the active article’s link:

    $('.related-article').hide();
    $('.gotoStep1').addClass('active');
    $('#related-pager .gotoStep2').addClass('next');
    $('#step-1').show();
    /* Pagination actions */
    $('.gotoStep1').click(function() {
        $('.related-nav').removeClass('active');
        $('.related-nav').removeClass('prev');
        $('.related-nav').removeClass('next');
        $($this).addClass('active');
        $('#related-pager .gotoStep2').addClass('next');
        $('.document-subsection').hide();
        $('#step-1').show();
    });
    // Repeat .gotoStep# function for each step,
    // designating the appropriate .gotoStep#
    // to be marked as adjacent and active.

Then, use CSS to hide all the links except the previous, active, and next. This might not be useful at all, but it seems like you could do something similar. I hope this helps!

Last edited by johnstephens (2012-03-22 20:39:04)

Offline

#9 2012-03-23 20:03:00

THE BLUE DRAGON
Member
From: Israel
Registered: 2007-11-16
Posts: 638
Website

Re: Check for previous and next articles in an article_custom list

Thanks John, but I think I was wrong and didn’t understand what your goal was.
I have edited my post above with the code I came up with that works, but not so sure that this is what you was looking for, sorry if I have posted in the wrong topic.

Last edited by THE BLUE DRAGON (2012-03-23 20:03:45)

Offline

#10 2012-04-29 17:17:19

m.young
Member
Registered: 2012-02-22
Posts: 10

Re: Check for previous and next articles in an article_custom list

Hi Blue Dragon,
Just wanted to say a BIG thank you for your array code (below) posted on this Forum. I was having endless problems trying to create a “Next” & “Previous” function on my “Bible Cartoons” website. Then I stumbled across your array solution which works beautifully on my website. Now my viewers can look at one of my cartoons, & also use the “Next” & “Previous” buttons (which are powered by an adapted version of your array) to navigate the cartoon collection.
Thank you so much for sharing this solutuion – you have saved the day for me!

—————————————————————————————————————————————————-

THE BLUE DRAGON wrote:

EDIT: I made it doing this:

<txp:article>
	// Current article ID
	<txp:variable name="current_id" value='<txp:article_id />' />
~
	// Create a variable with all the list of articles IDs separated by a comma
	<txp:variable name="all_ids"><txp:article_custom section="my-section" category="my-category" customfieldname="my-value" sort="Title asc" limit="999"><txp:article_id /><txp:if_last_article><txp:else />,</txp:if_last_article></txp:article_custom></txp:variable>
~
	// PHP search in array using the variables current_id & all_ids we created
	<txp:php>
		global $variable;
		$the_ids = $variable['all_ids'];
		$id_list = explode(',', $the_ids);
		$this_id = array_search($variable['current_id'], $id_list);
		$cnext_id = $id_list[$this_id + 1];
		$cprev_id = $id_list[$this_id - 1];
		if(in_array($cnext_id,$id_list) == FALSE){$cnext_id = $id_list[0];}
		if(in_array($cprev_id,$id_list) == FALSE){$cprev_id = end($id_list);}
~
		$variable['custom_prev_article_id'] = $cprev_id;
		$variable['custom_next_article_id'] = $cnext_id;
	</txp:php>
~
	// Prev link
	<txp:article_custom id='<txp:variable name="custom_prev_article_id" />'>
		<txp:permlink>prev</txp:permlink>
	</txp:article_custom>
~
	// Next link
	<txp:article_custom id='<txp:variable name="custom_next_article_id" />'>
		<txp:permlink>next</txp:permlink>
	</txp:article_custom>
</txp:article>

Offline

#11 2012-04-29 22:09:08

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

Re: Check for previous and next articles in an article_custom list

You could probably try this:

<txp:etc_query html='
  <txp:article_custom ... your search query here...>
  <a href=''<txp:permlink />'' id=''link-to-<txp:article_id />''><txp:title /></a>
  </txp:article_custom>'
  query="a[@id='link-to-{?thisid}']">
<p>{preceding-sibling::a[position()=1]} {?} {following-sibling::a[position()=1]}</p>
</txp:etc_query>

with etc_query plugin. Be sure to use v0.61 or above.

Last edited by etc (2012-05-22 17:01:28)

Offline

Board footer

Powered by FluxBB