Skip Navigation

[Resolved] Use value of shortcode inside a view

This support ticket is created 5 years, 3 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 10 replies, has 2 voices.

Last updated by jesperK 5 years, 3 months ago.

Assisted by: Nigel.

Author
Posts
#1372349
Skærmbillede 2019-10-29 14.47.31.png

I need a variable title on a view. f.ex. 'blogg' see (attachment)

I have a view with a query filter value as a shortcode like this:
[wpv-view name="view-name" wpvcategory="xxxx"]

I would like to use conditional statements to show titles:
[wpv-layout-start]
[wpv-items-found]
<!-- wpv-loop-start -->
<section class="wpv-loop js-wpv-loop front-section cf">
[wpv-conditional if="( '[wpvcategory]' eq 'Blogg' )"]
<h3 class="blog-title">Blogg</h3>
[/wpv-conditional]
.... more

#1372391

Nigel
Supporter

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

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

Hi Jesper

You can access the values of shortcode attributes passed to a View using the wpv-attribute shortcode, see: https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-attribute

There is a little more description here: https://toolset.com/documentation/user-guides/passing-arguments-to-views/#accessing-the-arguments

#1372401

Cool. That helped me a lot 🙂

which attribute can I use in 'wpv-attribute'? Content Selection?

#1372405

Nigel
Supporter

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

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

The wpv-attribute shortcode is used to get the value of any attribute you specify which was included in the wpv-view shortcode.

So if you call a View with the shortcode

[wpv-view name="my-view" fruit="banana"]

then within the View you can refer to the value of the fruit attribute (in this case, "banana") using the wpv-attribute shortcode, i.e.

[wpv-attribute name="fruit"]

will output "fruit".

The attribute can be whatever you want. In your question you asked about using the built-in wpvcategory attribute, which you can.

To use it inside a conditional statement, that would look something like

[wpv-conditional if="( '[wpv-attribute name='wpvcategory']' eq 'Blogg' )"]

Does that cover what you mean?

#1372431

Yeah it works as it should. I was just wondering if I can display the post type as well with it?

#1372911

Nigel
Supporter

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

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

There is a wpv-post-title shortcode to output the post title: https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-post-type

#1372971

Hi Nigel

Thanks for your response.

The output is empty? Here is the start of my view:

[wpv-layout-start]
[wpv-items-found]
<!-- wpv-loop-start -->
<section id="front-section-two-portfolio" class="front-section cf">
[wpv-conditional if="( '[wpv-post-type show="single"]' ne '' )"]
<h3 class="blog-title">[wpv-post-type show="single"]</h3>
[/wpv-conditional]
...more

#1373007

Nigel
Supporter

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

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

The problem with that conditional statement is that the show="single" double quotes break the if="..." double quotes, breaking the entire shortcode. You need to use single quotes for show='single'.

But I'm not sure about the purpose of your condition, because you are saying "if the post type label is not nothing then show it", but of course the post type label exists if you have a View iterating over posts. (If you are querying taxonomies or users then it will fail.)

#1373009

I want to show the name of the post type (f.ex. portfolio) as a headline. I guess what the conditional statment is not used because all post type by defintion have a name 🙂

But this does not show the headline:
[wpv-layout-start]
[wpv-items-found]
<!-- wpv-loop-start -->
<section id="front-section-two-portfolio" class="front-section cf">
<h3 class="blog-title">[wpv-post-type show="single"]</h3>
...more

The value of [wpv-post-type show="single"] is empty it should be f.ex. 'Portfolio'

#1373071

Nigel
Supporter

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

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

OK, sorry, I see that you are using the shortcode within the Loop Editor but outside of the loop itself.

So it doesn't have the context of one of the posts from the query to know the post type.

If you want to create a heading with the post type, i.e. it should appear only once and not for every iteration of the loop, then I suggest you do something like this:

<wpv-loop>
  [wpv-item index=1]
  <h3>[wpv-post-type show="single"]</h3>
  ...your other loop stuff...
  [wpv-item index=other]
  ...your other loop stuff...
</wpv-loop>

That way the heading is only shown for the first iteration of the loop.

#1373101

My issue is resolved now. Thank you!