how can i add post-categories list in elementor custom widget - wordpress

My control code is
i want to create control in elementor which shows all post categories. please help me how can i achieve this...
enter image description here
$this->add_control(
'show_elements',
[
'label' => __( 'Post Categoris', 'plugin-domain' ),
'type' => \Elementor\Controls_Manager::SELECT2,
'multiple' => true,
'options' => [
$category,
],
]
); ```

You can use WP get_categories() to get all categories. check below code.
$options = array();
$args = array(
'hide_empty' => false,
);
$categories = get_categories($args);
foreach ( $categories as $key => $category ) {
$options[$category->term_id] = $category->name;
}
$this->add_control(
'show_elements',
[
'label' => __( 'Post Categoris', 'plugin-domain' ),
'type' => \Elementor\Controls_Manager::SELECT2,
'multiple' => true,
'options' => $options,
]
);

Related

Update title field while listing wordpress page titles in Elementor selector

I can get wordpress page list to elementor selector. I can get the pages as a list, but the title field remains as the page id. can someone help
$options = array();
$pages = get_pages(array(
));
foreach ($pages as $key => $page) {
$options[$page->ID] = get_the_title($page->ID);
}
$repeater = new \Elementor\Repeater();
$repeater->add_control(
'selected_item_id',
[
'label' => __('Portfolio Item', 'cabei'),
'type' => \Elementor\Controls_Manager::SELECT,
'options' => $options,
]
);
$this->add_control(
'list',
[
'label' => esc_html__( 'Repeater List', 'cabei' ),
'type' => \Elementor\Controls_Manager::REPEATER,
'fields' => $repeater->get_controls(),
'title_field' => '{{{ selected_item_id }}}',
]
);
title field display page id :

Wordpress / CMB2 Framework / Display data from repeatable fields in frontend

I have a own option-page in the backend-sidebar with this repeatable fields in it. (not a metabox) I cant figure out, how to display the data from the group field in the frontend.
function ww_register_theme_options() {
$ww_prefix = '_ww_';
$ww_contacts = new_cmb2_box( array(
'id' => $ww_prefix . 'ww_option_plugin',
'title' => esc_html__( 'Ansprechpartner', 'ww-contact' ),
'icon_url' => '/wp-content/plugins/ww-contact/assets/images/icons/ww-icon-white.png',
'object_types' => array( 'options-page' ),
'option_key' => 'ww_options',
) );
$ww_group_field = $ww_contacts->add_field(array(
'id' => $ww_prefix . 'contact_repeat_group',
'type' => 'group',
'description' => __('Ansprechparter Liste', 'ww-contact'),
'repeatable' => true,
'options' => array(
'group_title' => 'Ansprechpartner {#}',
'add_button' => __('neuer Ansprechpartner', 'ww-contact'),
'remove_button' => __('Entfernen', 'ww-contact'),
'sortable' => true,
),
));
$ww_contacts->add_group_field($ww_group_field, array(
'name' => 'Vorname',
'id' => $ww_prefix . 'forname',
'type' => 'text',
));
$ww_contacts->add_group_field($ww_group_field, array(
'name' => 'Vorname',
'id' => $ww_prefix . 'surname',
'type' => 'text',
));
}
Remove the prefix from your add_group_field ids they aren't needed. Then loop through like so...
$entries = get_post_meta( $parent, 'ww_contact_repeat_group', true );
if($entries){
foreach ( (array) $entries as $key => $entry ) {
if ( isset( $entry['forname'] ) ) {
$forname = $entry['forname'];
}
}
}

WordPress - Custom Taxonomies on CPT aren't displaying correctly

I have a custom post type called 'activity' and a custom taxonomy called 'category_activity'.
On single-activity.php I am wanting to display the current taxonomy. At the moment, all taxonomies are being displayed even if they aren't added to the post.
single-activity.php:
$taxonomy = 'category_activity';
$terms = get_terms($taxonomy);
if ( $terms ) {
foreach ( $terms as $term ) { echo $term->name; }
}
Should display: 'Taxonomy Name 1'
Currently displays: 'Taxonomy Name 1 Taxonomy Name 2 Taxonomy Name 3'.
Taxonomy registration:
function cptui_register_my_taxes_category_activity() {
$labels = array(
"name" => __( 'Things Categories', '' ),
"singular_name" => __( 'Things Category', '' ),
);
$args = array(
"label" => __( 'Things Categories', '' ),
"labels" => $labels,
"public" => true,
"hierarchical" => true,
"label" => "Things Categories",
"show_ui" => true,
"show_in_menu" => true,
"show_in_nav_menus" => true,
"query_var" => true,
"rewrite" => array( 'slug' => 'things-to-do/category', 'with_front' => false, ),
"show_admin_column" => false,
"show_in_rest" => false,
"rest_base" => "",
"show_in_quick_edit" => false,
);
register_taxonomy( "category_activity", array( "activity" ), $args );
}
add_action( 'init', 'cptui_register_my_taxes_category_activity' );
Thanks for the help.
EDIT - I got this working with:
$category = wp_get_post_terms($post->ID, 'category_activity');
echo $category[0]->name;
It would be better to use wp_get_post_terms in your case :
https://codex.wordpress.org/Function_Reference/wp_get_post_terms
<?php $terms = wp_get_post_terms( $post_id, $taxonomy, $args ); ?>

Retrieving data from additional OptionTree pages

Adding OptionTree pages is simple with the code below, but does anyone know how to retrieve the stored data?
/**
* Hook to register admin pages
*/
add_action( 'init', 'register_options_pages' );
/**
* Registers all the required admin pages.
*/
function register_options_pages() {
// Only execute in admin & if OT is installed
if ( is_admin() && function_exists( 'ot_register_settings' ) ) {
// Register the pages
ot_register_settings(
array(
array(
'id' => 'custom_options',
'pages' => array(
array(
'id' => 'test_page',
'parent_slug' => 'options-general.php',
'page_title' => 'Test Page',
'menu_title' => 'Test Page',
'capability' => 'edit_theme_options',
'menu_slug' => 'test-page',
'icon_url' => null,
'position' => null,
'updated_message' => 'Test Page updated.',
'reset_message' => 'Test Page reset.',
'button_text' => 'Save Changes',
'show_buttons' => true,
'screen_icon' => 'options-general',
'contextual_help' => null,
'sections' => array(
array(
'id' => 'test_section',
'title' => __( 'Test Section', 'motif-core' )
)
),
'settings' => array(
array(
'id' => 'test_section_input',
'label' => 'Test Input',
'desc' => 'Pretty freaking awesome!',
'std' => '',
'type' => 'text',
'section' => 'test_section',
'class' => ''
)
)
)
)
)
)
);
I tried this $my_plugin_options = get_option('custom_options'); but it only shows the word 'array' on the front end?
This is how to retrieve the stored data:
$my_plugin_options = get_option('custom_options');
echo $my_plugin_options['test_section_input'];

Unable to add custom select field in woocommerce

I want to add a custom field options to checkout page. I am using the following code :
$fields['billing']['billing_options'] = array(
'label' => __('Options', 'woocommerce'),
'placeholder' => _x('', 'placeholder', 'woocommerce'),
'required' => false,
'clear' => false,
'type' => 'select',
'options' => array(
'option_a' => __('option a', 'woocommerce' ),
'option_b' => __('option b', 'woocommerce' )
)
);
I want to show the options (option_a,option_b) from database or
I want to use dynamic data and want to use for loop in the options menu
How can I use for loop inside this function ?
Just do it before, like this:
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function my_custom_checkout_fields( $fields ) {
$args = array(
'post_type' => array('options'),
'posts_per_page' => -1
);
$posts = new WP_Query($args);
$options = array();
foreach ($posts as $post) {
$options[$post->ID] => attr_esc($post->post_title);
}
$fields['billing']['billing_options'] = array(
'label' => __('Options', 'woocommerce'),
'placeholder' => _x('', 'placeholder', 'woocommerce'),
'required' => false,
'clear' => false,
'type' => 'select',
'options' => $options
);
return $fields;
}

Resources