Site logo

Archived topic

{{post_terms.taxonomy}} custom ORDER

13 replies · Started by Eduardo Garcia on September 18, 2021

Viewing posts 1–14 of 14

Hello, we have a taxonomy on the web that is "dates" that is equivalent to the months of the year, currently it shows them in reverse order, and we would like to change it, is it possible in elements?

This is the code we have installed:
<h1>
{{post_title}}
</h1>
<h2>
{{custom_field.subtitulo_h2}}
</h2>
<h5>
{{post_terms.category}} | {{post_terms.fechas}} | {{post_terms.paises}}
</h5>

And so it shows:
https://ofertassingles.com/aventura-andaluza/

The problem is that when showing the dates it does it from December to September and we want to change the order if possible.

Thanks.

Hi Eduardo,

Unfortunately, that's not possible with the date template tag.

That tag uses the WordPress Core function get_the_term_list() and this listing sorts terms by name (alphabetical). That's why D (December) goes before S(September).

That's likely sorted as April > August > December > February > January > June > July > March > May > November > October > September.

If you want to change the sorting, you may have to modify the HTML of the page hero a bit and sort things using CSS.

Example HTML:

<h1>
{{post_title}}
</h1>
<h2>
{{custom_field.subtitulo_h2}}
</h2>
<h5>
<span class="hero-terms categoria">{{post_terms.category}}</span> | <span class="hero-terms fechas">{{post_terms.fechas}}</span> | <span class="hero-terms paises">{{post_terms.paises}}</span>
</h5>

You then sort things by using this CSS:

.page-hero h5 .hero-terms {
display: inline-flex;
flex-direction: row;
}

.page-hero h5 .fechas a[href*="diciembre"]{ order: 12; }
.page-hero h5 .fechas a[href*="nobiembre"]{ order: 11; }
.page-hero h5 .fechas a[href*="octubre"]{ order: 10; }
.page-hero h5 .fechas a[href*="septiembre"]{ order: 9; }
.page-hero h5 .fechas a[href*="agosto"]{ order: 8; }
.page-hero h5 .fechas a[href*="agosto"]{ order: 7; }
.page-hero h5 .fechas a[href*="julio"]{ order: 6; }
.page-hero h5 .fechas a[href*="junio"]{ order: 5; }
.page-hero h5 .fechas a[href*="abril"]{ order: 4; }
.page-hero h5 .fechas a[href*="marzo"]{ order: 3; }
.page-hero h5 .fechas a[href*="febrero"]{ order: 2; }
.page-hero h5 .fechas a[href*="enero"]{ order: 1; }

Perfect, now order them correctly, but how could I separate each of these taxonomies by commas, all the commas appear at the beginning and the text all together, can you help me?

https://ofertassingles.com/singles-a-marruecos/

Try adding this PHP snippet to remove the default separator for the category only.

add_filter( 'generate_category_list_output', function($output){
    $categories_list = get_the_category_list();
	$output = sprintf('<span class="cat-links">%3$s<span class="screen-reader-text">%1$s </span>%2$s</span> ',
					esc_html_x( 'Categories', 'Used before category names.', 'generatepress' ),
					$categories_list,
					apply_filters( 'generate_inside_post_meta_item_output', '', 'categories' )
				);

    return $output;
});

You then add this CSS to add the commas back:

.page-hero h5 .fechas a:not(a[href*="diciembre"]):after{
content:", ";
margin-right: 5px;
}

It works perfect, it would only remain that it does not show the commas that come out in a row after category | ,,,,

on this line - get_the_category_list(); - can you change it to get_the_category_list(' ');? This is to remove the old separator.

Good morning, first of all thank you for your attention

It just doesn't work properly.

I have added an image that corresponds to two urls

Tanzania + Morocco

Tanzania → https://ofertassingles.com/gran-aventura-tanzania/
Morocco → https://ofertassingles.com/singles-a-marorca/

We see that in the image above there is one "," before Singles in September, and another at the end of Singles in November.

In the image below they come out ",,," before Singles in September and at the end after December is correct.

Is it possible to remove all the starting and ending commas?

Thanks again

We include it in the functions.php of child theme

https://ofertassingles.com/wp-content/uploads/2021/09/code-gp.jpg

Ah I forgot post tag terms use a different filter. My bad.

Let's try this instead:

add_filter( 'generate_page_hero_terms_separator', function() {
    return '';
} );

And then wrap the tag terms on its own wrapper HTML so we re-add the comma to it as well as the new filter I've provided will remove the , for all terms.

Yes, that's right. That's expected.

We then add the , back using CSS.

try this.

.page-hero .paises a:not(:last-child):after,
.page-hero .categoria a:not(:last-child):after {
    content: ", ";
    margin-right: 5px;
}

Great, it works perfectly

Thank you very much for your attention and wisdom Elvin, you have been a great help to me.

Have a happy day !!!

No problem. Glad to be of any help. Stay safe! :)

This archived topic is closed to new replies.