Hello,
I have created user fields and wants to display them in a page thanks to a Views (and also, use them in a conditional output).
What I need here is to display the user fields related to the current user, the one who is looking at the page (knowing that many users can be connected in the same time).
I have tried this :
[types usermeta='date-de-naissance' user_current='true' style='text' format='m'][/types]
but this is not working (fields of all users are displayed !)
Regards
Pat
Hi, it sounds like you have placed this types field shortcode in a View's Loop between the wpv-loop tags. If that is a View of Users, then the field will be shown for each User in the loop. If you only want to show it once, place the types field shortcode outside the wpv-loop tags in your Loop Output editor, or in another Content Template.
If I have misunderstood, please include screenshots showing how you have placed this shortcode on your site. Thanks!
Hi Christian,
Thanks for your answer.
I tried your proposal and this does not change the issue.
Then, I placed the shortcode directly in a page, and then, the result is OK ???
The issue is that I need to use the shortcode for display, but also as a conditional output. Can I place a conditional output directly inside a page (as there is no shortcode builder available, I'm not sure this is something that is possible within Toolset !)
Here is my code in Views (Views name is portrait):
[wpv-layout-start]
[wpv-items-found]
[types usermeta='date-de-naissance' style='text' format='m' user_current='true'][/types]
<!-- wpv-loop-start -->
<wpv-loop>
</wpv-loop>
<!-- wpv-loop-end -->
[/wpv-items-found]
[wpv-no-items-found]
The code in the page :
[types usermeta='date-de-naissance' style='text' format='m' user_current='true'][/types]
[wpv-view name="portrait"]
The result of the page display is :
01 => month of the current user (right)
09 => month of the user with the lowest ID) (wrong)
And the condition I need to place :
[wpv-conditional if="([types usermeta='civilite' user_current='true'][/types]' eq 'Homme' ) AND ([types usermeta='date-de-naissance' style='text' format='m' user_current='true'][/types]' eq '01' )"]
I placed this condition in both the page and the Views, but none is working !
Regards
Pat
Inside a View of Users, to access the current logged-in User's information you must use a nested View of Users. Create a new View of Users, filtered by specific Users, using a shortcode attribute filter "users". See the attached screenshot. Use the wpv-current-user shortcode to pass the current User's ID into the nested View filter. Here is an example using your portrait View:
<wpv-loop>
[wpv-view name="nested-view-slug" users="[wpv-current-user info='id']"]
</wpv-loop>
In the Loop Output editor for the nested View of Users:
<wpv-loop>
Current User's birth month: [types usermeta="date-de-naissance" format="m"][/types]
[wpv-conditional if="( '[types usermeta='date-de-naissance' format='m'][/types]' eq '01' )"]
Current User's birth month = 01
[/wpv-conditional]
</wpv-loop>
Hi Christian,
Seems the current user info is OK now. In fact, I have just added the [wpv-view name="nested-view-slug" users="[wpv-current-user info='id']"] inside the page and it's working fine.
Now, I still have an issue with the conditional. What I want is to compare if a date is between D1D1/M1M1 and D2D2/M2M2
Year is not important. So I tried this :
[wpv-conditional if="(( '[types usermeta='date-de-naissance' format='m'][/types]' gte '07' ) AND ( '[types usermeta='date-de-naissance' format='d'][/types]' gte '23' )) AND (( '[types usermeta='date-de-naissance' format='m'][/types]' lte '08' ) AND ( '[types usermeta='date-de-naissance' format='d'][/types]' lte '23' ))"]
Current User's birth month = 08 : Lion
[/wpv-conditional]
Here, is it defined to check if the date is between July, 23rd and August, 23rd.
But it's not working as desired.
Could you help please
Regards
Pat
This conditional isn't going to work because the logic is not correct. For example, the second comparison clause is
day >= 23
but the fourth comparison clause is
day <= 08
These two are mutually exclusive, so no results will ever match because the day cannot be >= 23 and <= 8. It's not possible. So that means you have to introduce more conditionals testing the months and days together. Here's the idea:
if ( (month==7 and day >=23) or (month==8 and day<=23) )
It might be possible to do this in a Views conditional, but it's messy and hard to maintain. If I were doing this, I would consider writing a custom shortcode that compares dates using timestamps and PHP.
Hi Christian,
I'm OK with PHP but not sure that timestamps will be helpful as we are speaking of only a part of the date (if we need to consider all yearly potential occurences, that would be a mess !!!).
Any idea ?
Regards
Pat
I understand that a timestamp will require some manipulation because the years can be different. To compare them, you can convert all 3 timestamps to date strings in the same year. Then convert those back to timestamps and compare those converted timestamps. Something like this:
add_shortcode( 'is_month_day_between', 'is_month_day_between_func');
function is_month_day_between_func($atts)
{
// shortcode attributes for timestamps start time, end time, and test time
$start = $atts['start'];
$end = $atts['end'];
$test = $atts['test'];
$result = 0;
// make new timestamps using the current Year so we can compare
$modStart = strtotime(date('d F, ', $start) . date('Y'));
$modEnd = strtotime(date('d F, ', $end) . date('Y'));
$modTest = strtotime(date('d F, ', $test) . date('Y'));
// compare and return 0 or 1 if test is between start and end
if(($modStart <= $modTest) && ($modTest <= $modEnd)){
$result = 1;
}
return $result;
}
Then register the "is_month_day_between" shortcode in Toolset > Settings > Frontend content > 3rd party shortcode arguments. Now you can use the shortcode in a conditional with any combination of known timestamps and custom field date values:
[wpv-conditional if="([is_month_day_between start='1498247543' end='1440359543' test='[wpv-post-field name='wpcf-date-de-naissance']'] eq '1')"] This month and day is between start and end
[/wpv-conditional]
[wpv-conditional if="([is_month_day_between start='1498247543' end='1440359543' test='[wpv-post-field name='wpcf-yourdatefieldslug']'] eq '1')" evaluate="false"] This month and day is not between start and end
[/wpv-conditional]
Hi Christian,
Thanks for the proposal.
I have tested and it's working but only when the date (here wpcf-date-de-naissance filed) to check is before 1970 (ie : when Unix time is negative).
In the other case, nothing is displayed.
Any idea?
Regards
Pat
Okay let's figure that out. Can you turn on server logs and add some log statements? Please add these log statements to your code and share the results. I'd like to see what's going on.
add_shortcode( 'is_month_day_between', 'is_month_day_between_func');
function is_month_day_between_func($atts)
{
// shortcode attributes for timestamps start time, end time, and test time
$start = $atts['start'];
$end = $atts['end'];
$test = $atts['test'];
error_log('start, end, test: ' . $start . ', ' . $end . ', ' . $test);
$result = 0;
// make new timestamps using the current Year so we can compare
$modStart = strtotime(date('d F, ', $start) . date('Y'));
$modEnd = strtotime(date('d F, ', $end) . date('Y'));
$modTest = strtotime(date('d F, ', $test) . date('Y'));
error_log('modStart, modEnd, modTest: ' . $modStart . ', ' . $modEnd . ', ' . $modTest);
// compare and return 0 or 1 if test is between start and end
if(($modStart <= $modTest) && ($modTest <= $modEnd)){
$result = 1;
}
return $result;
}
Please also turn on debugging in your conditional and share the output onscreen:
[wpv-conditional if="([is_month_day_between start='1498247543' end='1440359543' test='[wpv-post-field name='wpcf-date-de-naissance']'] eq '1')" debug="true"]
Hi Christian,
Sorry to have been long to answer.
I have placed the desired code and when I try to display the conditional, here is the message I'm getting :
Warning: date() expects parameter 2 to be long, string given in /my_site/wp-content/themes/toolset-starter/functions.php on line 88
The line 88 is :
$modTest = strtotime(date('d F, ', $test) . date('Y'));
Regards
Pat
Okay it sounds like something is going on with the "test" attribute value in your conditional. Please copy + paste the entire conditional here for me to review. Please copy the wpv-post-field code from your conditional and paste it just before the conditional so we can debug it. Like this:
Test date de naissance: [wpv-post-field name='wpcf-date-de-naissance']
[wpv-conditional if="([is_month_day_between start='1498247543' end='1440359543' test='[wpv-post-field name='wpcf-date-de-naissance']'] eq '1')" debug="true"]
What does this show on the front-end of your site?
Then, please be sure to copy + paste the relevant server logs here. Not just the warnings, please also include the "start, end, test" lines and "modstart, modend, modtest" lines.
Hi Christian,
Thanks for your time.
SOrry, but I'm getting exactly the same result.
But, I have to highlight something :
"date-de-naissance" is not a postfield but a user field. Perhaps this could be the cause of the problem?
Let me know
Regards
Pat
Okay since it's a user field, please use this code instead:
Test current user ID: [wpv-current-user info="id"]
Test user ID: [wpv-user field="ID"]
Test date de naissance: [wpv-user field='wpcf-date-de-naissance']
Test current user date de naissance: [wpv-user field='wpcf-date-de-naissance' id='[wpv-current-user info='id']']
[wpv-conditional if="([is_month_day_between start='1498247543' end='1440359543' test='[wpv-user field='wpcf-date-de-naissance']'] eq '1')" debug="true"]
Hi Christian,
That's better interms of error not showing now.
But, concerning the function, this is stil not working fine.
It was working if the "date-de-naissance" was August, 10th 1957
With this conditional :
[wpv-conditional if="([is_month_day_between start='1498247543' end='1440359543' test='[wpv-user field='wpcf-date-de-naissance']'] eq '1')" debug="true"]
Now, if I'm using a January, 1st 2000 with this condition :
[wpv-conditional if="([is_month_day_between start='945817200' end='948495599' test='[wpv-user field='wpcf-date-de-naissance' user_current='true']'] eq '1')"]
Here, I'm using 945817200 = Tue, 21 Dec 1999
And 948495599 = Fri, 21 Jan 2000
So, normally, it should work !
Regards
Pat