Closed. This question is off-topic. It is not currently accepting answers.admin管理员组文章数量:1317898
Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.
Closed 4 years ago.
Improve this questionI'm new to wordpress and I'm trying to accomplish the following goal:
I have a custom type albums seeded by a plugin and I would like to display this data in a list.
I installed the Code Snippets Plugin and was able to create a following working shortcode:
function display_albums_shortcode() {
$query = new WP_Query(
array(
'post_type' => 'albums'
));
$output = '<section>';
if ($query->have_posts()):
while ( $query->have_posts() ):
$album = $query->the_post();
$output .= '<div><figure>' . get_the_title($album) . ' </figure></div>';
endwhile;
endif;
$output .= '</section>';
wp_reset_postdata();
return $output;
}
add_shortcode('display_albums', 'display_albums_shortcode');
However, I would like to make it modularize it a bit. I would like to have an isolated function (code snippet) called render_album_list_item that takes the album as an argument and returns the HTML:
function render_album_list_item($album) {
$output = '<div><figure>' . get_the_title($album) . ' </figure></div>';
return $output;
}
Subsequently, I would like to call render_album_list_item inside of the first code snippet, something like:
function display_albums_shortcode() {
$query = new WP_Query(
array(
'post_type' => 'albums'
));
$output = '<section>';
if ($query->have_posts()):
while ( $query->have_posts() ):
$album = $query->the_post();
$output .= render_album_list_item($album);
endwhile;
endif;
$output .= '</section>';
wp_reset_postdata();
return $output;
}
add_shortcode('display_albums', 'display_albums_shortcode');
Unfortunately, I cannot make the snippet above work and haven't found anything in the documentation.
Also: is this the only way how to display the data - by manually creating a HTML string? Coming from a ReactJS world, this approach is pretty nasty. Any ideas, advice would be appreciated!
Thanks!
Closed. This question is off-topic. It is not currently accepting answers.Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.
Closed 4 years ago.
Improve this questionI'm new to wordpress and I'm trying to accomplish the following goal:
I have a custom type albums seeded by a plugin and I would like to display this data in a list.
I installed the Code Snippets Plugin and was able to create a following working shortcode:
function display_albums_shortcode() {
$query = new WP_Query(
array(
'post_type' => 'albums'
));
$output = '<section>';
if ($query->have_posts()):
while ( $query->have_posts() ):
$album = $query->the_post();
$output .= '<div><figure>' . get_the_title($album) . ' </figure></div>';
endwhile;
endif;
$output .= '</section>';
wp_reset_postdata();
return $output;
}
add_shortcode('display_albums', 'display_albums_shortcode');
However, I would like to make it modularize it a bit. I would like to have an isolated function (code snippet) called render_album_list_item that takes the album as an argument and returns the HTML:
function render_album_list_item($album) {
$output = '<div><figure>' . get_the_title($album) . ' </figure></div>';
return $output;
}
Subsequently, I would like to call render_album_list_item inside of the first code snippet, something like:
function display_albums_shortcode() {
$query = new WP_Query(
array(
'post_type' => 'albums'
));
$output = '<section>';
if ($query->have_posts()):
while ( $query->have_posts() ):
$album = $query->the_post();
$output .= render_album_list_item($album);
endwhile;
endif;
$output .= '</section>';
wp_reset_postdata();
return $output;
}
add_shortcode('display_albums', 'display_albums_shortcode');
Unfortunately, I cannot make the snippet above work and haven't found anything in the documentation.
Also: is this the only way how to display the data - by manually creating a HTML string? Coming from a ReactJS world, this approach is pretty nasty. Any ideas, advice would be appreciated!
Thanks!
Share Improve this question asked Oct 27, 2020 at 14:29 matt93matt93 1111 bronze badge 3 |1 Answer
Reset to default 0I managed to get it work by creating one code snippet:
function render_album_list_item($album) {
$output = '<div><figure>' . get_the_title($album) . ' </figure></div>';
return $output;
}
function display_albums_shortcode() {
$query = new WP_Query(
array(
'post_type' => 'albums'
));
$output = '<section>';
if ($query->have_posts()):
while ( $query->have_posts() ):
$album = $query->the_post();
$output .= render_album_list_item($album);
endwhile;
endif;
$output .= '</section>';
wp_reset_postdata();
return $output;
}
add_shortcode('display_albums', 'display_albums_shortcode');
Still, is this the best way to achieve this goal?
本文标签: shortcodeNested Code Snippets
版权声明:本文标题:shortcode - Nested Code Snippets 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742034285a2417044.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
the_post
does not return the post,$foo = $query->the_post()
resolves tonull
/undefined, not a post object – Tom J Nowell ♦ Commented Oct 27, 2020 at 14:54<?php /** Plugin name: 1 line plugin */
) – Tom J Nowell ♦ Commented Oct 27, 2020 at 14:58