by
KK » Sat Sep 03, 2016 5:39 am
Hi larin555,
Here is the promised solution.
Let us tackle first the articles template listing only posts related to a particular user.
The list-view of your article.php template should already be listing all blog posts using some variation of the following code -
- Code: Select all
<cms:pages masterpage='article.php' limit='10'>
<h2><cms:show k_page_title /></h2>
</cms:pages>
In your setup, the article template is related to the profile.php template through a region named 'article_author'.
Let us now bring that region into the code above to make it list posts of only a user with profile named 'john-doe' -
- Code: Select all
<cms:pages masterpage='article.php' limit='10' custom_field="article_author=john-doe">
<h2><cms:show k_page_title /></h2>
</cms:pages>
I don't think it'd be difficult for you to understand the modified code above.
Of course, the code above will always show posts of user 'john-doe' as we have hard-coded that name in it.
Let us make it flexible by making the code dynamically take the user-name from the URL (i.e. we should be able to supply any user-name through the URL by adding it as a querystring parameter). Here is how we do it -
- Code: Select all
<cms:set my_user="<cms:gpc 'user' method='get' />" />
<cms:if my_user >
<cms:set my_filter="article_author=<cms:show my_user />" 'global' />
</cms:if>
<cms:pages masterpage='article.php' limit='10' custom_field=my_filter>
<h2><cms:show k_page_title /></h2>
</cms:pages>
in the code above, we are checking the URL used to access the current template for a querystring parameter named 'user' and then using whatever value is specified in it to filter our listing.
To test it out, if the current URL happens to be
http://www.yoursite.com/article.phpmake it
http://www.yoursite.com/article.php?user=janeand now when you access the template listing it will show it filtered by 'jane'.
So that now gives us the ability to manipulate the URL dynamically.
Use a different user name as the parameter and the listing should show posts related only to that user e.g. the following will show a listing of only posts related to 'sherlock-holmes'
http://www.yoursite.com/article.php?use ... ock-holmesOK, with that done we can switch our attention to the profile.php template.
I need to show on the profile page of "John Doe" for example, a button saying "See John Doe articles from the blog".. Once you click the button, it needs to send you to a page where all articles from John Doe would be displayed. But only his articles.
We can do that now simply by adding the user name to the link leading to the articles listing we saw above. Example -
- Code: Select all
<cms:if k_is_page>
<cms:set my_article_template_link="<cms:link 'article.php' />" />
<a href="<cms:add_querystring my_article_template_link "user=<cms:show k_page_name />" />">See <cms:show k_page_title /> articles from the blog</a>
</cms:if>
but this button should appear only if he actually has any articles in the blog section.
One way of doing that would be to first find out the count of posts related to the user and then conditionally output the button as follows -
- Code: Select all
<cms:if k_is_page>
<cms:set my_count_of_posts="<cms:pages masterpage='article.php' custom_field="article_author=<cms:show k_page_name />" count_only='1' />" />
<cms:if my_count_of_posts >
<cms:set my_article_template_link="<cms:link 'article.php' />" />
<a href="<cms:add_querystring my_article_template_link "user=<cms:show k_page_name />" />">See <cms:show k_page_title /> articles from the blog</a>
</cms:if>
</cms:if>
I think that should cover both the use-cases you had.
Hope it helps.