Using ExpressionEngine's {category_name} on a permalink page?
I need help with a simple issue I'm having on a site built with ExpressionEngine 2.
I have a category which links to a "view" page using {title_permalink='product/view'}
. On this page, I want to create a link to take the users "back" to the category (e.g. Back to Toys). How do I create this link?
For example, I would've thought this code would work:
{exp:channel:category_heading channel="project"}
<p class="pfloatRight"><a href="#"> Back to {category_name}</a></p>
{/exp:channel:c开发者_C百科ategory_heading}
But it doesn't as ExpressionEngine doesn't know which category the entry is in. I tried enabling related_categories_mode but it didn't help.
Any ideas? I know this is a simple fix, I'm just not used to working with categories.
If you don't mind outputting all the categories an entry is assigned to, you can offer a link "back to a category" from your product view permalink page.
Put the following code within your exp:channel:entries
tag loop:
<p class="pfloatRight">
Back to
{categories backspace="2"}
<a href="{path="product/index"}">{category_name}</a>,
{/categories}
</p>
Which would output something like:
<p class="pfloatRight">
Back to <a href="#">Category Name</a>, <a href="#">Category Name</a>
</p>
You'll notice I placed the "Back to" text outside the {categories}
variable pair so it doesn't get repeated and used the backspace parameter to remove the comma from the last category.
The apparent downside is that if there's more than one category assigned to an entry, it may be confusing for the user to remember which category they navigated from.
I'd argue that most people are accustomed to using their browser's Back button rather than any on-page links, so trying to determine the actual category they came from may provide little return on investment.
However, even if these "Back to Category" links aren't necessarily useful to users, they do provide SEO benefits for people who may land on a product page from a search result and want to see more items in the same category.
If you only have one category assigned to the product, you can do this within your channel:entries
loop:
{categories limit="1"}
<p class="pfloatRight"><a href="{path="product/index"}">Back to {category_name}</a></p>
{/categories}
If you're using multiple categories per-product, then you'd have to use an add-on (or custom code) that stores the URL history for you (like this one) and retrieve the last-visited page that way.
Your code looks like it should work. What's the URL you're trying to execute this code from?
精彩评论