Skip Navigation

[Resolved] Content template for taxonomy items

This support ticket is created 5 years, 11 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 6 replies, has 2 voices.

Last updated by Daniel 5 years, 11 months ago.

Assisted by: Nigel.

Author
Posts
#1180007
Desktop screenshot (8).png
Idiomas ‹ ESmedo   Empresa de Traducciones y Servicios Web — WordPress.png

Hi,

on this site I have created a taxonomy called "Idiomas" (languages) and I added terms (languages) with a description for each.

When you click View each item, you go to a taxonomy item page where there is a loop with the custom posts associated to that taxonomy term (see screenshot).

I'd like to create a Content template with the Divi builder and customize the way each "Idioma" item looks like. For example, I don't want the sidebar, and I'd like to show the description for the item and a list of all the custom posts related to that taxonomy item... It seems with Toolset you can only customize the loop assigned to each Archive.

Can you please help? I hope I made myself clear

#1180123

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Daniel

The page in your front-end screenshot with the url site.com/idioma/albanes/ is the idioma taxonomy archive for the term albanes, meaning that WordPress generates a list of posts which have the albanes term assigned.

Your theme will handle displaying this list much like it would the list of blog posts, using an archive.php template.

You can create a custom design for this archive at Toolset > WordPress Archives and creating a new archive for the idioma taxonomy.

The custom design will simply replace the content area of the page (where the theme itself would display the list of posts).

At the end of the settings page for the custom archive you will see a Theme Options section. Under "Disable Elements" you can disable the sidebar, but it doesn't really work. The sidebar doesn't get added, but the space where the sidebar would appear remains. We have an internal ticket to remove this setting, because Divi itself doesn't seem to have an option to display archives full width.

It would only be possible to display a full-width archive if you created a child theme and made a copy of the archive.php file and name it taxonomy-idioma.php and directly edit the PHP so that there was no sidebar added and that the main content occupied the full width.

Now, regarding customising the archive itself, you'll see it is much like creating a View, and you would use Divi to design the template for each iteration of the loop.

If you want to display additional information, such as the taxonomy description, at the top of the page before the list of matching posts, you would have to insert that content in the Loop Editor of the custom archive, immediately after the opening wpv-layout-start shortcode (so that it will appear only once, and will appear even if there are no matching posts).

Now the Views shortcodes used for displaying taxonomy term info (e.g. wpv-taxonomy-description) don't work in this context, they only work in the output section of a View to display taxonomy terms. We have an internal ticket to expand the scope where they work, but in the meantime you can try using this custom shortcode (you'll need to add the code to your site, which you can do at Toolset > Settings > Custom Code):

/**
 * Register shortcode to output current taxonomy
 * for use on taxonomy archive page outside or inside loop
 * att['output'] string taxonomy | name (default) | slug | term_id | description | count | field (requres att['field'])
 * att['field'] string key
 */
add_shortcode('taxonomy', function ($atts = [], $content = null) {
 
    // provide defaults
    $atts = shortcode_atts(
        array(
            'output' => 'name',
            'field' => '',
        ),
        $atts
    );
    extract($atts);
 
    $return = '';
 
    global $wp_query;
    $obj = $wp_query->get_queried_object();
 
    if ($output != 'field') {
 
        $return = $obj->$output;
    } else {
        if (isset($field)) {
            $return = get_term_meta($obj->term_id, $field, true);
        }
    }
 
    return $return;
});

And here are examples of how you can use it (in the context described above):

[taxonomy output='taxonomy'] -- outputs the taxonomy slug
[taxonomy] -- outputs the term name (default)
[taxonomy output='count'] -- outputs the count of results
[taxonomy output='description'] - outputs the term description
[taxonomy output='field' field='wpcf-colour'] -- outputs a 'colour' taxonomy custom field
#1180166

I am afraid this is too much coding for me :(( I tried putting the description before the loop but it did not worked. I only get this far:

hidden link

#1180699

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Daniel

I'll need access to your site to be able to check what you have done.

I will mark your next reply as private so that I can get log-in credentials from you—you may want to create a temporary admin user for me to use that you can later delete. And be sure to have a current backup of your site, even though I don't intend to make anything but trivial changes.

#1184277

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

I've done the basics on your site to set this up.

First, I added the code to register the custom shortcode "taxonomy" at Toolset > Settings > Custom code.

Next, the correct template you need to copy from the parent theme is index.php and rename it to taxonomy.php, which means it will be used on all taxonomy archive pages. (Delete the other file you created.)

I edited this file and removed the get_sidebar() call near the end, so no sidebar is added.

But the theme still leaves space for the sidebar, even when not present. Divi provide instructions on how to modify the CSS for this here: hidden link

I added the required CSS to the custom CSS of the Idiomas custom archive.

I also added a minimal amount of CSS to create a 2 column layout for the page, in the same custom CSS section.

I then edited the markup in the Loop Editor so that there is a wrapper div (class "col-container"), a left column for entering content that does not repeat with each post (class "col-left"), and a right column for where the post results are displayed (class "col-right").

In the left section I output the taxonomy term using my custom taxonomy shortcode. I described above other examples of how to use it (https://toolset.com/forums/topic/content-template-for-taxonomy-items-2/#post-1180123), to output term meta for example.

Hopefully if you look at what I have done you can see how it works and then expand upon it as you need, as well as do something similar for other taxonomy archives as required.

#1184308

PERFECT NIGEL!!

You are great. That's exactly what I needed.

Thanks a lot for your help!!

#1184309

My issue is resolved now. Thank you!