-1

I created a new Elementor Widget using the documentation provided here:

https://developers.elementor.com/creating-a-new-widget/

The widget works fine from the demo for static content. But what I'd like to do is load content from a custom post type, and I can't find any documentation explaining how to do that. I have a product-menu CPT, and used ACF for the fields. How do I load product-menu, and then have that displayed on the page when I drag and drop the widget?

From the example, the below functions I'm thinking are the ones for loading CPT content. Can someone give me a basic example of connecting CPT to the widget, then have that display on the page? I can figure out all options afterwards. Thanks!

protected function _register_controls() {
    $this->start_controls_section(
        'section_content',
        [
            'label' => __('Content', 'vs-elementor-elements'),
        ]
    );

    $this->add_control(
        'title',
        [
            'label' => __('Title', 'vs-elementor-elements'),
            'type' => Controls_Manager::TEXT,
            'default' => __('Title', 'vs-elementor-elements'),
        ]
    );

    $this->add_control(
        'description',
        [
            'label' => __('Description', 'vs-elementor-elements'),
            'type' => Controls_Manager::TEXTAREA,
            'default' => __('Description', 'vs-elementor-elements'),
        ]
    );

    $this->add_control(
        'content',
        [
            'label' => __('Content', 'vs-elementor-elements'),
            'type' => Controls_Manager::WYSIWYG,
            'default' => __('Content', 'vs-elementor-elements'),
        ]
    );


    $this->end_controls_section();
}

protected function render() {
    $settings = $this->get_settings_for_display();

    $this->add_inline_editing_attributes('title', 'none');
    $this->add_inline_editing_attributes('description', 'basic');
    $this->add_inline_editing_attributes('content', 'advanced');
    ?>
    <h2 <?php echo $this->get_render_attribute_string('title'); ?>><?php echo $settings['title']; ?></h2>

    <div <?php echo $this->get_render_attribute_string('description'); ?>><?php echo $settings['description']; ?></div>

    <div <?php echo $this->get_render_attribute_string('content'); ?>><?php echo $settings['content']; ?></div>
    <?php
}

protected function _content_template() {
    ?>
    <#
    view.addInlineEditingAttributes('title', 'none');
    view.addInlineEditingAttributes('description', 'basic');
    view.addInlineEditingAttributes('content', 'advanced');
    #>
    <h2 {{{ view.getRenderAttributeString('title') }}}>{{{ settings.title }}}</h2>

    <div {{{ view.getRenderAttributeString('description') }}}>{{{ settings.description }}}</div>

    <div {{{ view.getRenderAttributeString('content') }}}>{{{ settings.content }}}</div>
    <?php
}
0

It is possible to load custom post type content by adding the following inside render():

$args = array(
    'post_type' => 'menu', // Change to desired post type
    'posts_per_page' => 99
);

$query = new \WP_Query($args);

if ($query->have_posts()) :
    while ($query->have_posts()) : $query->the_post();

        $this->data[] = array(
            'title' => get_the_title()
        );

        echo '<h2>' . get_the_title() . '</h2>';

    endwhile;

    wp_reset_postdata();
endif;

var_dump($this->data);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.