admin管理员组文章数量:1130349
I'm creating a plugin to load custom posts by custom taxonomy (restaurant menu plugin).. I'm calling the main function (holds everything) by the add_action hook on 'init' and inside it I have another function that is using the new Wp_query to load these custom posts content.
I add a shortcode to this function and placed it in the gutenberg widget editor. The content is loading perfectly in the main restaurant menu page, however I can also see the echos in the gutenberg editor of the menu page....
What am I doing wrong?
function benito_generate_menu_posts() {
$args2 = array(
'taxonomy' => 'item_type',
'orderby' => 'name',
'order' => 'ASC'
);
$types = get_categories($args2);
function load_post_with_tax($tax_type) {
$args = array(
'post_type' => 'menu',
'tax_query' => array(
array(
'taxonomy' => 'item_type',
'field' => 'term_id',
'terms' => $tax_type,
)
)
);
$query1 = new WP_query ( $args );
while($query1->have_posts()) : $query1->the_post();
echo '<div class="menu-thumb">';
the_post_thumbnail( );
echo '</div>';
echo '<div class="menu-title">';
the_title( );
echo '</div>';
echo '<div class="menu-content">';
the_content( );
echo '</div>';
endwhile;
wp_reset_postdata(); // reset the query
}
foreach($types as $type) {
load_post_with_tax($type);
}
}
add_shortcode( 'get_menu_items' , 'benito_generate_menu_posts' );
I'm creating a plugin to load custom posts by custom taxonomy (restaurant menu plugin).. I'm calling the main function (holds everything) by the add_action hook on 'init' and inside it I have another function that is using the new Wp_query to load these custom posts content.
I add a shortcode to this function and placed it in the gutenberg widget editor. The content is loading perfectly in the main restaurant menu page, however I can also see the echos in the gutenberg editor of the menu page....
What am I doing wrong?
function benito_generate_menu_posts() {
$args2 = array(
'taxonomy' => 'item_type',
'orderby' => 'name',
'order' => 'ASC'
);
$types = get_categories($args2);
function load_post_with_tax($tax_type) {
$args = array(
'post_type' => 'menu',
'tax_query' => array(
array(
'taxonomy' => 'item_type',
'field' => 'term_id',
'terms' => $tax_type,
)
)
);
$query1 = new WP_query ( $args );
while($query1->have_posts()) : $query1->the_post();
echo '<div class="menu-thumb">';
the_post_thumbnail( );
echo '</div>';
echo '<div class="menu-title">';
the_title( );
echo '</div>';
echo '<div class="menu-content">';
the_content( );
echo '</div>';
endwhile;
wp_reset_postdata(); // reset the query
}
foreach($types as $type) {
load_post_with_tax($type);
}
}
add_shortcode( 'get_menu_items' , 'benito_generate_menu_posts' );
Share
Improve this question
edited Jan 11, 2019 at 16:40
Silvester Vella
asked Jan 11, 2019 at 16:25
Silvester VellaSilvester Vella
155 bronze badges
7
|
Show 2 more comments
1 Answer
Reset to default 1As Milo noticed, shortcodes should return content instead of echoing it. So change
while($query1->have_posts()) : $query1->the_post();
echo '<div class="menu-thumb">';
the_post_thumbnail( );
echo '</div>';
echo '<div class="menu-title">';
the_title( );
echo '</div>';
echo '<div class="menu-content">';
the_content( );
echo '</div>';
endwhile;
to
while($query1->have_posts()) : $query1->the_post();
$div = '<div class="menu-thumb">';
$div .= get_the_post_thumbnail( );
$div .= '</div>';
$div .= '<div class="menu-title">';
$div .= get_the_title( );
$div .= '</div>';
$div .= '<div class="menu-content">';
$div .= get_the_content( );
$div .= '</div>';
return $div;
endwhile;
(replace echo with return and also anywhere there is a the_something() replace it with get_the_something() so that at the very end, it returns the whole chunk of output.)
I'm creating a plugin to load custom posts by custom taxonomy (restaurant menu plugin).. I'm calling the main function (holds everything) by the add_action hook on 'init' and inside it I have another function that is using the new Wp_query to load these custom posts content.
I add a shortcode to this function and placed it in the gutenberg widget editor. The content is loading perfectly in the main restaurant menu page, however I can also see the echos in the gutenberg editor of the menu page....
What am I doing wrong?
function benito_generate_menu_posts() {
$args2 = array(
'taxonomy' => 'item_type',
'orderby' => 'name',
'order' => 'ASC'
);
$types = get_categories($args2);
function load_post_with_tax($tax_type) {
$args = array(
'post_type' => 'menu',
'tax_query' => array(
array(
'taxonomy' => 'item_type',
'field' => 'term_id',
'terms' => $tax_type,
)
)
);
$query1 = new WP_query ( $args );
while($query1->have_posts()) : $query1->the_post();
echo '<div class="menu-thumb">';
the_post_thumbnail( );
echo '</div>';
echo '<div class="menu-title">';
the_title( );
echo '</div>';
echo '<div class="menu-content">';
the_content( );
echo '</div>';
endwhile;
wp_reset_postdata(); // reset the query
}
foreach($types as $type) {
load_post_with_tax($type);
}
}
add_shortcode( 'get_menu_items' , 'benito_generate_menu_posts' );
I'm creating a plugin to load custom posts by custom taxonomy (restaurant menu plugin).. I'm calling the main function (holds everything) by the add_action hook on 'init' and inside it I have another function that is using the new Wp_query to load these custom posts content.
I add a shortcode to this function and placed it in the gutenberg widget editor. The content is loading perfectly in the main restaurant menu page, however I can also see the echos in the gutenberg editor of the menu page....
What am I doing wrong?
function benito_generate_menu_posts() {
$args2 = array(
'taxonomy' => 'item_type',
'orderby' => 'name',
'order' => 'ASC'
);
$types = get_categories($args2);
function load_post_with_tax($tax_type) {
$args = array(
'post_type' => 'menu',
'tax_query' => array(
array(
'taxonomy' => 'item_type',
'field' => 'term_id',
'terms' => $tax_type,
)
)
);
$query1 = new WP_query ( $args );
while($query1->have_posts()) : $query1->the_post();
echo '<div class="menu-thumb">';
the_post_thumbnail( );
echo '</div>';
echo '<div class="menu-title">';
the_title( );
echo '</div>';
echo '<div class="menu-content">';
the_content( );
echo '</div>';
endwhile;
wp_reset_postdata(); // reset the query
}
foreach($types as $type) {
load_post_with_tax($type);
}
}
add_shortcode( 'get_menu_items' , 'benito_generate_menu_posts' );
Share
Improve this question
edited Jan 11, 2019 at 16:40
Silvester Vella
asked Jan 11, 2019 at 16:25
Silvester VellaSilvester Vella
155 bronze badges
7
-
Does your shortcode
returnit’s content? Shortcodes can’t directly produce output. – Milo Commented Jan 11, 2019 at 16:28 -
mmm no it has a foreach loop at the end with contains a function call in itself..
foreach($types as $type) { load_post_with_tax($type); }– Silvester Vella Commented Jan 11, 2019 at 16:36 -
and what does
load_post_with_taxdo? – Milo Commented Jan 11, 2019 at 16:38 - edit my post with the code :) – Silvester Vella Commented Jan 11, 2019 at 16:40
- See my first comment. – Milo Commented Jan 11, 2019 at 16:41
1 Answer
Reset to default 1As Milo noticed, shortcodes should return content instead of echoing it. So change
while($query1->have_posts()) : $query1->the_post();
echo '<div class="menu-thumb">';
the_post_thumbnail( );
echo '</div>';
echo '<div class="menu-title">';
the_title( );
echo '</div>';
echo '<div class="menu-content">';
the_content( );
echo '</div>';
endwhile;
to
while($query1->have_posts()) : $query1->the_post();
$div = '<div class="menu-thumb">';
$div .= get_the_post_thumbnail( );
$div .= '</div>';
$div .= '<div class="menu-title">';
$div .= get_the_title( );
$div .= '</div>';
$div .= '<div class="menu-content">';
$div .= get_the_content( );
$div .= '</div>';
return $div;
endwhile;
(replace echo with return and also anywhere there is a the_something() replace it with get_the_something() so that at the very end, it returns the whole chunk of output.)
本文标签: Plugin echos text from shortcode function in gutenberg page editor
版权声明:本文标题:Plugin echos text from shortcode function in gutenberg page editor 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749020497a2304302.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


returnit’s content? Shortcodes can’t directly produce output. – Milo Commented Jan 11, 2019 at 16:28foreach($types as $type) { load_post_with_tax($type); }– Silvester Vella Commented Jan 11, 2019 at 16:36load_post_with_taxdo? – Milo Commented Jan 11, 2019 at 16:38