Reputation: 98
i have created a elementor control in this I have displayed all posts list but while am display the post title in protected function render()
then it shows post ID not a post name, I want to display post name and also want to get permalink of that post in <a href="<?php the_permalink(); ?>">Read More</a>
$options = array();
$posts = get_posts( array(
'post_type' => 'digital_card'
) );
foreach ( $posts as $key => $post ) {
$options[$post->ID] = get_the_title($post->ID);
}
$this->add_control(
'post_name',
[
'label' => __( 'Select Posts', 'plugin-domain' ),
'label_block' => ('bool'),
'type' => \Elementor\Controls_Manager::SELECT,
'multiple' => true,
'options' => $options,
]
);
protected function render() {
$settings = $this->get_settings();
$show_title = $settings['post_name'];
?>
<?php echo $show_title; ?>
<a href="<?php the_permalink(); ?>">Read More</a>
<?php
}
}
Upvotes: 0
Views: 1733
Reputation: 383
So you want to get all ID
s and post_title
s of all custom post_type
'digital_card'?! You don't need $key
and get_the_title()
to fetch the values for the options of control type Controls_Manager::SELECT2
. You can set 'label_block' => true
if you want the selection field in full width at the editor panel.
Part within protected function _register_controls()
:
$options = [];
$posts = get_posts( [
'post_type' => 'digital_card'
] );
foreach ( $posts as $post ) {
$options[ $post->ID ] = $post->post_title;
}
$this->add_control(
'posts',
[
'label' => __( 'Select Posts', 'your-plugin-domain' ),
'type' => \Elementor\Controls_Manager::SELECT2,
'label_block' => true,
'multiple' => true,
'options' => $options,
]
);
So now the ID
s of the selected posts will be saved in the control data posts
.
To display the post_title
s and a link to the related post, you fetch the values by the ID
of the posts, which are stored in $settings['posts']
as an array.
Part within protected function render()
:
$settings = $this->get_settings();
$posts = $settings[ 'posts' ];
foreach( $posts as $post ) {
echo get_the_title( $post );
?>
<a href="<?php echo get_permalink( $post ); ?>">Read More</a>
<?php
}
Upvotes: 1