Get value from option tree wordpress - wordpress

I want to be use some sliders in my wordpress theme. I want to select them in my theme option. I am using the code below.
<?php
$slider_select = get_option_tree( 'slider_select', '', 'true' );
?>
<?php get_template_part('$slider_select'); ?>
But, it is not working. I want the get_template_part code worked. Any suggestion?

replace
get_template_part('$slider_select');
with
get_template_part($slider_select);

You can use this method for create a slider in option tree.
create settings array like this.
array(
'id' => 'my_slider',
'label' => 'Images',
'desc' => '',
'std' => '',
'type' => 'list-item',
'section' => 'general',
'class' => '',
'choices' => array(),
'settings' => array(
array(
'id' => 'slider_image',
'label' => 'Image',
'desc' => '',
'std' => '',
'type' => 'upload',
'class' => '',
'choices' => array()
),
array(
'id' => 'slider_link',
'label' => 'Link to Post',
'desc' => 'Enter the posts url.',
'std' => '',
'type' => 'text',
'class' => '',
'choices' => array()
),
array(
'id' => 'slider_description',
'label' => 'Description',
'desc' => 'This text is used to add fancy captions in the slider.',
'std' => '',
'type' => 'textarea',
'class' => '',
'choices' => array()
)
)
)
Add into page using loop
$my_slider = ot_get_option( 'my_slider' );
foreach($my_slider as $slider){
echo '<img src="'.$slider["slider_image"].'">';
echo $slider["slider_link"];
echo $slider["slider_description"];
}

Related

Wordpress - Trying to display date from custom form two different ways

I am trying to create an events page in Wordpress. I am using the Advanced Custom Fields plugin to generate my forms. I would like to style the month and year differently from the day in the listing section. However, I don't want to make the user have to input the same date twice in the back-end just for this. Is there a way to display the content from the one form in two different calibrations on the front-end? For example, can I display it once as month and year then once again as just the day? Is there any other workaround? Any advice would be appreciated.
Here is my custom form code:
if( function_exists('acf_add_local_field_group') ):
acf_add_local_field_group(array(
'key' => 'group_5fec83c0a2f7b',
'title' => 'Event Particulars',
'fields' => array(
array(
'key' => 'field_5fec83ca0dc8b',
'label' => 'Date of event',
'name' => 'date_of_event',
'type' => 'date_picker',
'instructions' => '',
'required' => 0,
'conditional_logic' => array(
array(
array(
'field' => 'field_5fec84a00dc8c',
'operator' => '!=empty',
),
),
),
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'display_format' => 'Y F j',
'return_format' => 'Y F j',
'first_day' => 1,
),
array(
'key' => 'field_5fec84a00dc8c',
'label' => '',
'name' => '',
'type' => 'text',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'maxlength' => '',
),
),
'location' => array(
array(
array(
'param' => 'page_type',
'operator' => '==',
'value' => 'front_page',
),
),
),
'menu_order' => 0,
'position' => 'normal',
'style' => 'default',
'label_placement' => 'top',
'instruction_placement' => 'label',
'hide_on_screen' => '',
'active' => true,
'description' => '',
));
endif;
You can load the value of the date field, create a date object and then format that date object as you prefer:
// Load field value.
$date_string = get_field('date_of_event');
// Create DateTime object from value.
// Here you must use the format that you specified as the "return format" of the date field in ACF.
$date = DateTime::createFromFormat('Y F j', $date_string);
// Now you can use the date object to print the date in different formats:
echo $date->format('M Y');
echo $date->format('j M Y');
Have a look at the "Modifiy value" section of the docs:
https://www.advancedcustomfields.com/resources/date-picker/

Wordpress add Shortcut link to Enable/Disable plugin in admin bar

I would like to add a link to enable and disable a plugin in the admin bar.
Below the code, I wanted to know if it is the correct method.
thank you
function custom_admin_bar_link( $admin_bar ) {
$admin_bar->add_menu( array(
'id' => 'wp-custom-link',
'title' => 'Custom Item',
'href' => '',
'meta' => array(
'title' => __('Custom'),
),
));
$admin_bar->add_menu( array(
'id' => 'disable-link',
'parent'=> 'wp-custom-link',
'title' => 'Disable Amazon',
'href' => 'https://mydisablelink.com/',
'meta' => array(
'title' => __('Disable Amazon'),
),
));
$admin_bar->add_menu( array(
'id' => 'unable-link',
'parent'=> 'wp-custom-link',
'title' => 'Enable Amazon',
'href' => 'https://mydisablelink.com/',
'meta' => array(
'title' => __('Enable Amazon'),
),
));
}
add_action( 'admin_bar_menu', 'custom_admin_bar_link', 100 );

How to add custom fields in ACF programmatically?

I want to programmatically add a tab with repeater inside but I can't seem to find a solution, I've googled all available resources but still not working.
I already tried using acf_add_local_field_group and acf_add_local_field but still no luck.
Well I can create a tab using acf_add_local_field but when I tried to add a child which in this case a repeater OR even a text field it still doesn't work.
Here's my code to create a tab and its child but the child doesn't work.
acf_add_local_field(array(
'key' => 'field_1',
'label' => 'Sub Title',
'name' => 'sub_title',
'type' => '',
'parent' => 'field_5bd14c9349930',
'fields' => array (
array(
'key' => 'field_2',
'label' => 'This is a test',
'name' => 'my_test',
'type' => 'text',
)
)
));
You should use acf_add_local_field_group to construct the whole field group.
Here's the proper code for adding a group and a custom tab with single repeater field inside:
if( function_exists('acf_add_local_field_group') ):
acf_add_local_field_group(array (
'key' => 'group_1',
'title' => 'My Group',
'fields' => array (
array (
'key' => 'field_unique_key',
'label' => 'First Tab',
'name' => '',
'type' => 'tab',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
'placement' => 'top',
'endpoint' => 0,
),
array (
'key' => 'field_unique_key',
'label' => 'Simple Repeater',
'name' => 'simple_repeater',
'type' => 'repeater',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
'collapsed' => '',
'min' => 0,
'max' => 10,
'layout' => 'table',
'button_label' => 'Add row',
'sub_fields' => array ( // Here you can add as many subfields for this repeater as you want
array (
'key' => 'field_unique_key',
'label' => 'Link',
'name' => 'link',
'type' => 'link', // example link type
'instructions' => 'Link name and URL',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
'return_format' => 'array',
),
),
),
),
'location' => array (
array (
array (
'param' => 'post_type',
'operator' => '==',
'value' => 'post',
),
),
),
'menu_order' => 0,
'position' => 'normal',
'style' => 'default',
'label_placement' => 'top',
'instruction_placement' => 'label',
'hide_on_screen' => '',
));
endif;

How can i use Option Tree custom post type checkbox in wordpress

array(
'id' => 'custom_post_type_checkbox',
'label' => __( 'Custom Post Type Select', 'theme-text-domain' ),
'desc' => sprintf( __( 'Check box' ),
'std' => '',
'type' => 'custom-post-type-checkbox',
'section' => '',
'rows' => '',
'post_type' => 'newyork-offices',
'taxonomy' => '',
'min_max_step'=> '',
'class' => '',
'condition' => '',
'operator' => 'and'
),
This is custom post type check box and i got all posts in my page template. so How can i use it my page template ?
What is the loop function for this ?

get acf fields from php file

I put this code into my functions.php
include_once('advanced-custom-fields/acf.php');
include_once('acf-options-page/acf-options-page.php');
include_once('acf-repeater/acf-repeater.php');
define( 'ACF_LITE', true );
if(function_exists("register_field_group"))
{
register_field_group(array (
'id' => 'acf_options',
'title' => 'Options',
'fields' => array (
array (
'key' => 'field_525d1b6d49043',
'label' => 'lable',
'name' => 'homepage',
'type' => 'repeater',
'instructions' => 'Select which categories you want to display on the homepage.',
'sub_fields' => array (
array (
'key' => 'field_525d1b8a49044',
'label' => 'Category',
'name' => 'firsttext',
'type' => 'text',
'column_width' => '',
'field_type' => 'text',
'return_format' => 'id',
),
array (
'key' => 'field_525d2473de72c',
'label' => 'Number of Posts',
'name' => 'number-of-posts',
'type' => 'text',
'column_width' => '',
'default_value' => 4,
'placeholder' => '',
'prepend' => '',
'append' => '',
'min' => 2,
'max' => '',
'step' => 2,
),
),
'row_min' => 1,
'row_limit' => 1,
'layout' => 'row',
'button_label' => 'Add a category',
),
),
'location' => array (
array (
array (
'param' => 'options_page',
'operator' => '==',
'value' => 'acf-options',
'order_no' => 0,
'group_no' => 0,
),
),
),
'options' => array (
'position' => 'normal',
'layout' => 'no_box',
'hide_on_screen' => array (
),
),
'menu_order' => 0,
));
}
Then I enter values into fields in admin console
How I cat get this value from page file?
I try get_field('acf_options) and I try get_field('homepage), but this return null
Im not sure why you would want to put this in your function.php file instead of just do this this through the plugin.
But if you want to retrieve those field values, you should do this: get_field( 'homepage', 'option' );
instead of:
get_field( 'homepage' ) or get_field( 'acf_options' );
My best advice would be to look on the ACF website for information. These docs help out a lot!

Resources