Skip Navigation

[Resolved] Conditional to add class is not working

This support ticket is created 7 years, 6 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
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 8 replies, has 2 voices.

Last updated by Beda 7 years, 6 months ago.

Assisted by: Beda.

Author
Posts
#516086

I am trying to: use a custom field (checkbox) to add a class to an element.

I have added a field to a custom post type. If it is checked, I want the post title on that page to be hidden. The field is a checkbox called "Hide Post Title". It outputs a 1 if checked, 0 if unchecked.

I have created a View that contains a conditional. The conditional should look to see if the checkbox is checked (=1). If it is checked, the conditional should add a class to the h2 post title tag "articleHideTitle" which will be set to display:none with CSS. I cannot get this to work. The class is not being added to the h2.

[wpv-layout-start]
	[wpv-items-found]
	<!-- wpv-loop-start -->
	<wpv-loop>
      <hr style="margin: 10px 0 50px 0;" />
      <h2 [wpv-conditional if="( $(wpcf-hide-article-titles) eq '1' )"]class="hideArticleTitle"[/wpv-conditional]>[wpv-post-title]</h2>
      <div class="articleBody">[wpv-post-body view_template="None"]</div>
	</wpv-loop>
	<!-- wpv-loop-end -->
	[/wpv-items-found]
	[wpv-no-items-found]
		<strong>[wpml-string context="wpv-views"]No items found[/wpml-string]</strong>
	[/wpv-no-items-found]
[wpv-layout-end]
#516172

This is the conditional to use:

[wpv-conditional if="( $(wpcf-show-title) eq '1' )"]
  [wpv-post-title] because  [types field='show-title'][/types]
[/wpv-conditional]

[wpv-conditional if="( $(wpcf-show-title) eq '0' )"]
  no title shown because  [types field='show-title'][/types]
[/wpv-conditional]

It assumes a Checkbox Field, which saves "1" if checked, "0" if not checked.
Every HTML you want to wrap around the Title should live within the Conditional that shows the Title.

#516332
screengrab.png

Thanks for your help but I still cannot get this to work.

Here is what I have now: (you had "show title" equal 1, but mine is actually "hide title" so I swapped the 1 and 0)

[wpv-conditional if="( $(wpcf-hide-article-titles) eq '0' )"]
  [wpv-post-title] because  [types field='hide-article-titles'][/types]
[/wpv-conditional]
 
[wpv-conditional if="( $(wpcf-hide-article-titles) eq '1' )"]
  no title shown because  [types field='hide-article-titles'][/types]
[/wpv-conditional]

But both posts (whether checked or unchecked) show this: (title) because 0

I have double checked to make sure that my post field is called hide-article-titles, and have double checked a post with the field checked and a post with the field unchecked. I have also resaved each post. Here is a screen shot of the field settings in case you see anything wrong.

#516440

This means those fields do hold "0" in the database and are not checked.

Can I login to your site, and see that live?
I will need to create Dummy Posts, Views, Fields and edit the code in the View you will pass me the link to.

A Backup is highly reccomended.

If you cannot afford this, please send me a Site's Snapshot or create a Test Site for this purpose.
https://toolset.com/faq/provide-supporters-copy-site/

Thank you for your patience.

#517263

There seems to be no layout with this name:
Section Specific Contributor Page

Anyway I added the View to a new test page here:
hidden link
It uses this Layout:
hidden link

Now, it would work, but something is completely wrong there.
You passed me a View for "Articles" not for "Journals".

The Checkbox is saved in Journals Posts, so of course, this will never work on a View listing Articles.
But you can see the correct syntax in my edits in the View you mentioned above:
hidden link

#517518

Thank you for your assistance.

I can see now why it wasn't working.
I put the checkbox on the 'Journal' post type because I wanted to hide titles on a layout for Journals (Section Specific Contributor Layout – sorry I gave you the wrong name).
But the piece of the layout that contains the title is a View showing Articles. So I need to put the checkbox on Articles instead.

That seems like it will work, but only if I use '0' (zero). Is there is a way to do this without the zero (save nothing to the database instead of a zero)? If I understand correctly, my article posts will not contain a zero until they are resaved. I have about 100 of these posts. (The majority of the posts will have the box unchecked; only a few will have "1")
Or, do you know a way to resave all of the posts so they write zeros to the database? I tried a bulk edit without changing any settings, but it did not work.

#517725

You can edit your Conditional to check if it stores 1 or nothing (empty).

If you already have all Fields saved, with the native default option to NOT store 0 in case it is not checked, then that is the best approach.

Just change your condition to check for 1 or nothing.

This way you do not need to change the settings in the Field and also not update all posts.

#518054

Okay. If I want to check for nothing, do I just use eq '' instead of eq '0' ?

#518525