Redirect when last segment missing - expressionengine

I have a single entry url structure like:
www.site.com/template_group/template_1/entry_id
But I want it to be:
www.site.com/template_group/template_1/entry_id/url_title.
Entry_id would be the segment feeding exp:channel:entries. How can I redirect www.site.com/template_group/template_1/entry_id to www.site.com/template_group/template_1/entry_id/url_title.
Thanks a lot!

Something like the following should work - it's untested, but it'll give you a good idea of how to go forward with it.
{if segment_4==""}
{exp:channel:entries channel="x" limit="1" dynamic="no" entry_id="{segment_3}"}
{redirect="template_group/template_1/{entry_id}/{url_title}"}
{/exp:channel:entries}
{/if}

put this at the very top of your template:
{if segment_4 == ""}
{exp:channel:entries channel="channel_goes_here" entry_id="{segment_3}"}
{redirect="{site_url}/{segment_1}/{segment_2}/{segment_3}/{url_title}
{/exp:channel:entries}
{/if}
This will check if you have set a fourth segment, if not it takes the third segment containing your entry_id, feeds it to the channel entries tag, which returns the url_title you need. With this url_title you can easily redirect to the correct page.

Good answers from janvl and madebyhippo
Quick note if you are using complex conditionals (the ones in the answers are simple ones so you will not have that problem) is that EE will parse the channel entries tags they contain, wich can slow down performance.
If you find yourself in that situation, best to avoid the problem altogether using Mark "The croxton" addons like switchee or ifelse

Related

Expression Engine- Show HTML within entries loop even if no results

New to Expression Engine. I have the following code:
{exp:channel:entries channel="blogposts" orderby="date" url_title="{segment_3}" limit="1"}
<div id="cat_head_section">
<div id="cat_title">
<div id="lower_bubble_heading" class="cat_title"><h5>{title}</h5></div>
</div>
</div>
<div id="category_main">
{if no_results}
<h2>Sorry, the post you were looking for either does not exist or has been deleted.</h2>
{/if}
{url_title}
</div>
{/exp:channel:entries}
When there are no results returned, I don't get any of the HTML returned (other than the heading), which is vital to the structure of the page. How do I solve this?
Thanks.
This answer assumes that the entry call you are making here is the main one on the page. If this call is really just for a sidebar or something then this answer may not apply.
Couple of things...
If you are building urls like EE "expects" you to you shouldn't have to specify which url segment to look for the url title. The 'url_title' parameter is generally useful when you are doing something that EE does not expect, such loading a specific entry other than the one that appears in the last segment.
If you are limiting your result to one entry, there's no need to specify an order.
Maybe the reason you think you need the 'url_title=' parameter is because you haven't taken a good look at 'require_entry='.
So we are down to this:
{exp:channel:entries channel="blogposts" limit="1" require_entry="yes"}
Now on to your question:
Think about why and how someone would end up asking for a page that you can't find.
Did the entry get deleted? Did someone retweet a mal-formed URL?
A. You may say these requests really call out for 404 response.
Serving a true 404 instead of a valid page with a "Sorry..." message ensures that the bad page will not be indexed by search engines...
To handle this you want EE's redirect tag.
{if no_results}
{redirect="404"}
{/if}
You'll want to have a genuinely helpful 404 page if you do this.
B. Alternatively you may say that when an entry can't be found you already have in mind a 'next best place' to sent the visitor -- better than your site's 404 page.
So if your visitor is looking for a blog entry that doesn't exist (say: /blog/entry/bad_page) the best place to send them would be to your blog index page (say: /blog). If you care about search ranking, then to the extent that the inbound link that is breaking had some SEO value, a 301 redirect here will help to confer that SEO value on to the page to which you are redirecting.
{if no_results}
{redirect='blog/' status_code="301"}
{/if}
The only thing that will show up if there are no results is what's in between the {if no_results} "if" statement. If you want more HTML in there, you just have to add it in there (which looks like you are duplicating it but you won't be because only one or the other will show up - results or no results).
Alternately, if you'd rather not duplicate the code you might choose to save yout HTML as a separate embedded template, and then pass the appropriate values to it as variable parameters. If no results, assign apologies to your variable values.
See http://ellislab.com/expressionengine/user-guide/templates/embedding.html

Expression Engine entry_id Parse

Hey Everyone I am having a huge problem :
I have this Line :
{exp:entries:ids_assigned_to_me tag="idont" channel="proiecte" field="clienti"}
Which outputs me some entry ids
and i can put it There at the entry_id:
{exp:channel:entries channel="proiecte" entry_id=" HERE " }
{content}
{/exp:channel:entries}
I tryied and search for hours over forums and stuff, but variable, snippets and embeding and stuff doesen't seemed to work out. Any Ideas ? about Inward Parse or something ?
Also tried php but didn't worked out :((
You're using the entries add-on and by the looks of it you're using the single tag method. It's not clear from the documentation, but I think as you're specifying a single tag, it's outputting the IDs, but if you use it as a tag pair (as other examples in the documentation show) and this piece of documentation hints at: "If no entries are assigned the logged in member, the no_results conditional is returned.".
So first try
{exp:entries:ids_assigned_to_me tag="idont" channel="proiecte" field="clienti"}
{content}
{/exp:entries:ids_assigned_to_me}
...all by itself without any {exp:channel:entries} tags. No idea what "tag" parameter is doing in there.
If that doesn't work and that method doesn't support a tag pair, then you'll need to do as an embed across 2 templates:
1st template:
{embed=template-group/second-template entry_ids="{exp:entries:ids_assigned_to_me tag="idont" channel="proiecte" field="clienti"}"}
2nd template:
{exp:channel:entries channel="proiecte" entry_id="{embed:entry_ids}"}
{content}
{/exp:channel:entries}

EE - How do I make the current segment active using Category Tags?

I am using the channel:categories tag to pull in categories for a section navigation. This works fine, but I also would like to tag the current active category as the active item in the navigation for styling purposes:
a class="active"
I tried two approaches neither of which works.
Attempt 1 (per the documentation):
<a {if active} class="active"{/if} href="{path='internal-staff-center/{category_url_title}'}">{category_name}</a>
Attempt 2:
<a {if segment_2 == "{category_url_title}"}class="active"{/if} href="{path='internal-staff-center/{category_url_title}'}">{category_name}</a>
I would appreciate some input from another viewpoint out there.
Thanks.
The {path} variable within Channel Categories will output a path using your category trigger word or the C(n) URL segment (C2, C15, etc). So it's doubtful that {segment_2} is actually your category_url_title ... it's more likely {segment_3}.
What is an example URL structure of these pages?
{if segment_X == category_url_title} should work so long as the comparison is correct.
Try
{if "{segment_2}" == "{category_url_title}"} class="current"{/if}

Set a custom template variable in expressionengine

I need to output the category for an entry a few times in an entry's template.
So I want to get the output from the following and assign to a variable so I can reuse within the template:
{exp:channel:entries channel="product" limit="1" status="open"}
{categories}{category_name}{/categories}
{/exp:channel:entries}"
How do it do that?
Now, you could enable the template to allow PHP, then you could write something like this:
{exp:channel:entries channel="product" limit="1" status="open"}
{categories}
<?php $category = '{category_name}'; ?>
{/categories}
{/exp:channel:entries}
Then you have the {category_name} stored in the php-variable "category". Later you can reuse it as you wish, like echoing it:
<?php echo $category; ?>
You can even compare it to other EE-tags:
{exp:channel:entries channel="product" limit="1" status="open"}
{if <?php $echo($category) ?> == title}
This title have got the same value as the category!
{/if}
{/exp:channel:entries}
Croxton's Stash: http://devot-ee.com/add-ons/stash does very nearly the same thing NSM Transplant (mentioned by Derek, above) does, and is free. One of these addons would definitely be the easiest way to do what you're trying to do.
EE has no built-in way to save data from within a tag loop and reuse it elsewhere in the template, outside of that tag loop.
One solution would be to use NSM Transplant to do exactly what you're looking to do.
Another would be to wrap your whole entry page in your channel:entries tag, so you can just use the categories loop wherever you need it, then use embeds for anything that can't be nested inside channel:entries.

Pagination on Single-Entry view in ExpressionEngine

I am building a website with ExpressionEngine as the CMS. There is a "Work" section that displays all the projects, and a detail view that shows each project individually. It is at this point, on the single-entry view that I would like to have "prev" and "next" pagination. In my tests I have made it work when the URL is "somedomain.com/index.php/work/detail/" but it does not work when the specific entry is part of the URL: "somedomain.com/index.php/work/detail/some_project/"
I've tried putting the pagination code outside of the {exp:weblog:entries} tag as well as within it, but to no avail. This is what my pagination code looks like:
{paginate}
<ul>
{if previous_page}
<li>< previous</li>
{/if}
{if next_page}
<li>next ></li>
{/if}
</ul>
{/paginate}
You are using pagination for lists of entries, what you need is the next / previous entry tags:
http://expressionengine.com/user_guide/modules/channel/entry_linking.html
Hmm. I'm not sure what the issue is here, as I've never used the {pagination} tag in that way. After checking out the docs, I see that the example code for prev/next links inside of the {pagination} tag is wrapped inside of an {exp:comment:entries} loop instead of the normal {exp:channel:entries} loop.
The docs aren't very clear about the scope of this particular feature of the {pagination} tag. You might want to double check that, in your {exp:channel:entries} loop, you haven't included pagination as a value in the disable parameter.
You could also check out the page in EE's user guide about Next/Prev Linking, which details the use of {exp:channel:next_entry} and {exp:channel:prev_entry} tags in place of the {pagination} tag that you've been using. I've used these tags without a hitch, so I definitely recommend trying them if you can't get your method to work.
Best of luck!

Resources