Search code examples
expressionengine

Expression Engine: How do I display the category name of an entry?


I am using SafeCracker to create some entries on a site and when the form is submitted it comes up in a preview template.

I need to be able to display the selected category for the entry.

I am using the following code to display the results in the preview template:

{exp:safecracker channel="jobs" id="contact" return="jobs/preview/ENTRY_ID" entry_id="{segment_3}"}

<p>Job Type: {job_type}<br />
  Category: {exp:channel:category_heading}{category_name}{/exp:channel:category_heading}<br />  
  Location: {job_location}</p>
  <p>Description:<br />
  {job_description}
  </p>
  <p>Apply by: {how_to_apply} at: {apply_value}</p>
  <p>Company: {company}</p>
  <p>Description: <br />
  {company_description}</p>
{/exp:safecracker}

As it is, the Category: value is blank. What is the correct way to do this?

Thanks!


Solution

  • Use: {categories}{if selected}{category_name}{/if}{/categories}.

    Failing that, you could use the query module:

    {exp:query sql="
        SELECT c.cat_name
        FROM exp_categories c, exp_category_posts cp
        WHERE cp.entry_id = {segment_3}
        AND c.cat_id = cp.cat_id
    " backspace="2"}{cat_name}, {/exp:query}