Skip Navigation

[Resolved] Shortcode for showing today's date

This support ticket is created 4 years, 12 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 13 replies, has 3 voices.

Last updated by deepS 4 years, 11 months ago.

Assisted by: Waqar.

Author
Posts
#1425641

Hi,
I want to display today's date in the toolset view. How to do this?
Also, help me to find out how to sum up the values of a number field generated by the view and display it.
Please help.

#1426137

Hi,

Thank you for contacting us and I'd be happy to assist.

1. To show today's date in a Toolset view, you can register a custom shortcode:
https://toolset.com/documentation/adding-custom-code/how-to-create-a-custom-shortcode/

The following links have some useful examples with code:

https://toolset.com/forums/topic/display-current-date-in-view-or-cred-form/
hidden link

2. To get the count or sum of the values of a number field generated by the view, you can create a view with a "List with separators" format selected in the "Loop Wizard".
( screenshot: hidden link )

In the loop, you can select only the number field, so that view's output only includes the comma-separated list of the field values.
( e.g. 1,2,3... etc )

Once this view is ready, you can create a custom shortcode that gets these values from the view and returns the count/sum:


add_shortcode( 'show_count_of_field_values', 'show_count_of_field_values_func');
function show_count_of_field_values_func() {

	$args = array(
		'id' => 1234
	);

	$view_output = render_view( $args );

	$view_output_arr = explode(',', $view_output);

	if(!empty($view_output_arr)) {
		$count = 0;
		foreach ($view_output_arr as $item) {
			$count = $count + $item;
		}
		return $count;
	}
}

Please replace 1234 with the actual ID of your newly created view and the above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through active theme's "functions.php" file.

To show the count, you can use the shortcode like this:


[show_count_of_field_values]

I hope this helps and for more personalized assistance around custom code, you can consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

regards,
Waqar

#1427219
Screenshot_1.jpg

Hi Waqar,
Happy to hear from you again. Actually I want help on the 'VIEW' created by you. As you can remember you helped me with displaying Jobs available within the State. See the page hidden link
Here I am displaying the Total Vacancies (sum of No. of Vacancies in the Job Posts). I want to add up all these available in the View ignoring the pagination. Hope you understood my point.

#1427635

Thanks for writing back and for sharing further details.

I think I understand the requirement and on this "Jobs in Assam" page, you'd like to show the total number of vacancies, coming from all the posts belonging to "Assam".

To recall how this view was set up, I'll need access to the website's admin area. Can you please share temporary admin login details?

Note: Your next reply will be private and please make a complete backup copy, before sharing the access details.

#1432779

Hi,

Thank you for sharing the admin access.

To get the total number of available jobs, on each state's page ( e.g. hidden link ), you can follow these steps:

1. Please create a duplicate of your view "Location wise Jobs actual" ( ID: 19495 )

In this view make sure that the pagination is set to "No pagination" and in the "Loop Editor" section, it should include code to return the number of vacancies:


[wpv-layout-start][wpv-items-found]<!-- wpv-loop-start --><wpv-loop>[sum-rfg],</wpv-loop><!-- wpv-loop-end -->[/wpv-items-found][wpv-no-items-found]0[/wpv-no-items-found][wpv-layout-end]

2. Next, create a duplicate of your view "states-view-grandparent" ( ID: 17991 ).

In this new view, you can remove all custom CSS and JS added in their respective tabs and make sure that the "Loop Editor" only includes code to return the nested view's output:


[wpv-layout-start][wpv-items-found]<!-- wpv-loop-start --><wpv-loop>[wpv-view name="location-wise-jobs-actual-copy" ids="[wpv-view name='places-belongs-state']"],</wpv-loop><!-- wpv-loop-end -->[/wpv-items-found][wpv-no-items-found]0[/wpv-no-items-found][wpv-layout-end]

Note: Please make sure to replace "location-wise-jobs-actual-copy" with the actual slug of the view, created in step 1.

Also, check the option "Disable the wrapping DIV around the View" just below the "Loop Editor".

3. After that, you can update the custom shortcode that I shared earlier slightly so that it includes the attribute "ids" for the target state:


add_shortcode( 'show_count_of_field_values', 'show_count_of_field_values_func');
function show_count_of_field_values_func() {
 
    $args = array(
        'id' => 1234,
        'ids' => ''
    );
 
    $view_output = render_view( $args );
 
    $view_output_arr = explode(',', $view_output);
 
    if(!empty($view_output_arr)) {
        $count = 0;
        foreach ($view_output_arr as $item) {
            $count = $count + $item;
        }
        return $count;
    }
}

Note: Please replace "1234" with the actual ID of the view created in step 2.

4. You'll be able to use the final shortcode like this:


[show_count_of_field_values ids="16142"]

This will give you a total of vacancies from the "Assam" state ( ID: 16142 ) and you can replace 16142 in the shortcode to get the count for different states.

I hope this helps and please let me know if you need any further assistance around this.

regards and Happy New Year,
Waqar

#1433971

Hi,
First of all, I wish you a very happy new year. I have acted as per your instructions but could not generate the desired results. I have created two new views "Location wise Jobs actual Duplicate" and "states-view-grandparent-duplicate" and also the custom code.
I have inserted the suggested shortcode in two views alternatively in two views "Location wise Jobs actual" and "states-view-grandparent" in the loop editor section but they are showing some other figures (see screenshot_2). Even I tried with other State IDs too.
Another thing that I need to mention that I want to place the shortcode in such a way where I don't have to change the State ID every time but in a universal way so that I change the View ID once in the like
hidden link
and it's done. Hope you got my point.

#1436017

Hi,

Thank you for waiting.

I've checked your views and added the following changes:

1. I remove the extra line breaks and spaces from both your new views and checked the option "Disable the wrapping DIV around the View" in the view "Location wise Jobs actual Duplicate".
( sorry I missed this step in my last reply for this view )

2. With the recent changes, I've updated the code for the shortcode slightly, so it becomes:


add_shortcode( 'show_count_of_field_values', 'show_count_of_field_values_func');
function show_count_of_field_values_func($atts) {

	$a = shortcode_atts( array(
		'id' => '21046',
		'ids' => ''
	), $atts );

	$args = array(
		'id' => $a['id'],
		'ids' => $a['ids']
	);

	$view_output = render_view( $args );

	$view_output_arr = explode(',', $view_output);

	if(!empty($view_output_arr)) {
		$count = 0;
		foreach ($view_output_arr as $item) {
			$count = $count + $item;
		}
		return $count;
	}
}

3. I noticed that you're using this shortcode to show the total number of jobs for a state inside the view "Location wise Jobs actual", like this:


[show_count_of_field_values ids="16158"]

To make it dynamically get the parent state's ID, I've updated it to use the attribute value "state":
( ref: https://toolset.com/documentation/user-guides/views/views-shortcodes/#wpv-attribute )


[show_count_of_field_values ids="[wpv-attribute name='state']"]

4. In the view "states-view-grandparent", where you're calling this child view, I've updated the view's shortcode, to pass current state's ID in the state attribute:
( ref: https://toolset.com/documentation/user-guides/views/passing-arguments-to-views/ )


[wpv-view name="location-wise-jobs-actual" ids="[wpv-view name='places-belongs-state']" state="[wpv-post-id]"]

As a result, the used state's job count is now showing correctly.

regards,
Waqar

#1439303
Screenshot_2.png

Hi Waqar,
As usual you are outstanding. I must appreciate your patience and hard work. The issue has been almost resolved. Only a few things left.
Point 1: I have to insert the shortcode in 'Location wise jobs actual' view while placing it in the 'State view grandparent' doesn't reflect the actual figure. Well that's not an issue. I have shifted the entire paragraph including codes to 'Location wise jobs actual' view. Now the problem is whenever I change pages the code [wpv-post-title] used for pulling the state name, fetches the page title. (see in screenshot_2).

Point 2: I don't know whether it is due to the current issue or not, I think you are the best person to be addressed as you have helped me creating the views. Recently I realized that the views are generating blank page when there is no job in a particular state. I have tried it with 'Sikkim (code: 16163)' in hidden link

Please help. Thank you.

#1442645

Nigel
Supporter

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

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

Hi Deep

Waqar is off today, and as he has already helped you a long way with these issues I'll let him reply to you when he's back tomorrow.

Thanks for your patience.

#1443843

Thanks for your reply.
Waqar please help me to resolve the issue at the earliest.

#1444249

Thank you for waiting and glad that the shortcode to count the number of jobs is working.

Point 1:

To overcome this, you can pass on the current state's title in the view's shortcode, same was the state's ID:

In view "states-view-grandparent", the child view's shortcode will include the "title" attribute with the state's title:


[wpv-view name="location-wise-jobs-actual" ids="[wpv-view name='places-belongs-state']" state="[wpv-post-id]" title="[wpv-post-title]"]

And in the view "Location wise Jobs actual", at both places where you need to show the title of that state, you can fetch that value using:


[wpv-attribute name="title"]

Point 2:

The output of the child view is hidden when no jobs are found, because of the custom script added in the view "states-view-grandparent".
( screenshot: hidden link )

It hides the state's wrapping container if no job wrapping containers are found within (which means there are no jobs), but I understand that in this new setup, this script is no longer needed.

regards,
Waqar

#1444257

Are you working at the moment?

#1444333

Yes, I'm working.

You're welcome to mark this ticket as resolved if both points are taken care of and for a new question or concerns, please start a new ticket.

regards,
Waqar

#1444357

My issue is resolved now. Thank you!