In my articles i m using a button to another article with similar topic. Don’t really understand how to do it with dynamic page, links are different on each page. can someone guide me?
Hay Olya,
Let me see if I understand. You have a collection of articles, each article may have one or more related articles. In the item page of an article you want to have one or more links to related articles. Is that a good representation of the challenge?
There are different ways to approach this, depending on some factors
-
Is the number of related articles for an article limited in some way? A single related article? up to 3 related articles?
-
How do you enter a new article to the system? Are you using the Content Manager (the database excel like view)?
I am going to choose the simple case - up to 3 related articles and using the Content Manager.
In the articles collection, we are going to add 3 new fields - related article 1 to 3. We then enter values to those fields - the related articles id or title.
-
Choosing the id field is the safest option. You can see the id field if you click on Visible Fields and select to view the id field. It appears like a guid (a sequence of chars and letters) that uniquely identifies each article. Using it, you enter the id field into the related article fields.
-
Choosing the title field, you enter the article titles in the related article fields. Note that in this case you have to be careful to enter the exact same value in each related field, and that if you modify the title value you have to update also all the references to it in the related article fields.
Next, in the article item page, you set three text elements as the links, each as hidden on load. lets denote those text elements as relatedArticle1 to 3. With a bit of code we can setup the links to the related articles. The code looks like the following
import wixData from 'wix-data';
$w.onReady(() => {
$w('#dynamicDataset1').onReady(() => {
let item = $w('#dynamicDataset1').getCurrentItem();
// repeat the following code for related article 2 and 3.
wixData.query('articles')
.eq('_id', item.relatedArticle1)
.find()
.then(res => {
if (res.length > 0) {
let relatedUrl = res.items[0].articleUrl;
let relatedTitle = res.items[0].title;
$w('#relatedArticle1').html =
`<p><a href="${relatedUrl}">${relatedTitle}</a></p>`;
$w('#relatedArticle1').show();
}
});
});
});
Notes
-
The code above assumes using the id. To use title instead, replace the field name in the .eq line with title.
-
The code assumes the id of the dynamic dataset is dynamicDataset1. You can change this id by selecting the dataset and opening the property panel.
-
The code assumes the dynamic page url field for articles is named articleUrl. In you case the field name is probably different - to find the field name, in content manager, select the url column and click field properties. You are looking for the field key.
Yes, that s what I am after. Thank you I ll try, sounds scary though
Can I ask why the links must be hidden on load?
Just to prevent flicker until we check for the related item and get the title and url.
Why there s no a possibility to add link in database, so we can add it in a cell? Just want to understand if it s not possible or just something missing
It is coming…
Is it possible to add “dynamic links” now a days?
Hi Tecno,
Can you please explain the scenario? what do you want to achieve?
Roi.