ExpressionEngine: display a navigation menu if an entry is assigned a specific category.
As an avid ExpressionEngine user, I like to dynamically generate as much as I can. This also includes the dynamic generation of navigation menus, naturally. However, there are times when I will use an entry as a static navigation menu by assigned it a specific URL TITLE (for example: whats_new or righthand_menu). Now, in the case of categories, how does one display this right-hand menu ONLY on entries of a weblog that are assigned a specific category? Here is how:
{!--Display right-hand menu ONLY if the entry has been assigned to the AUTOMOBILES category--}
{exp:query sql="select count(w.url_title) AS post_count from exp_weblog_titles w, exp_category_posts cp, exp_categories c where w.entry_id = cp.entry_id and cp.cat_id = c.cat_id and c.cat_url_title='automobiles_category' and w.url_title = '{segment_3}' limit 1"}
{/exp:query}
{!-- Display AUTOMOBILES right-hand menu --}
{field_id_2}
{/exp:query}
So, what exactly does this do?
The first query retrieves the post count of any entries (in this case, the URL TITLE of the entry is in the third segment of the URL) that are assigned to a specific category (we are looking for 1 here; in fact, the LIMIT 1 line keeps the query from searching for anything greater, as our relevant choices are either 1 or 0). The count is then applied to a PHP variable called assignedCatVar.
If the assignedCatVar is = 1, then the next query grabs the contents of the right-hand menu entry and displays it appropriately on the page.
Simple and effective.