当Iam创建包含大量HTML的短代码时,Iam使用两个函数。一个模板函数,用于保存所有短代码。还有一个简单的函数,用于从第一个函数创建短代码。我倾向于使用此设置,因为shortcode函数需要return
内容。
首先,我们需要创建一个模板函数,其中包含HTML代码和shortcode属性。
function child_pages_shortcode_template( $atts ) {
// Attributes
// these are the attributes you can insert in the shortcode
// i.e. [show_child_pages sort_order="desc"]
// see codex for get_pages for all attributs
// these are also used as default attribute settings
$atts = shortcode_atts(array(
\'sort_order\' => \'asc\',
\'sort_column\' => \'post_title\',
\'show_excerpt\' => 1, // excerpt displays as default, set to any other number to disable
), $atts );
extract( $atts );
// get ID of current page
$current_page_id = get_the_ID();
// set the ID of the current page as child_of parameter
// that means we are only showing children of the current page
$args = array(
\'child_of\' => $current_page_id,
\'parent\' => $current_page_id, // add this to only show 1 level deep, remove to also show grand children
\'post_type\' => \'page\',
\'sort_order\' => $sort_order, //this comes from the shortcode_atts above, so you can change it with the shortcode
\'sort_column\' => $sort_column, //this comes from the shortcode_atts above, so you can change it with the shortcode
);
$child_pages = get_pages( $args );
// only start if we have some children
if ($child_pages) {
echo \'<ul>\';
foreach ($child_pages as $child_page) {
echo \'<li>\';
$child_page_title = $child_page->post_title; // get children page title
$child_page_excerpt = $child_page->post_excerpt; // get children page excerpt
$child_page_id = $child_page->ID; // get children ID to create the permalink
$child_page_permalink = get_permalink($child_page_id); // get permalink to children page
echo \'<h3><a href="\'.$child_page_permalink.\'">\'.$child_page_title.\'</a></h3>\';
// only show excerpt if $show_excerpt is 1
if ($show_excerpt == 1) {
echo \'<p>\'.$child_page_excerpt.\'</p>\';
}
echo \'</li>\';
}
echo \'</ul>\';
}
}
在此之后,我们获取第一个函数并从中创建一个短代码:
function child_pages_shortcode($atts, $content = null){
ob_start();
$content = child_pages_shortcode_template($atts);
$content = ob_get_contents();
ob_end_clean();
return $content;
}
add_shortcode(\'show_child_pages\', \'child_pages_shortcode\');
现在您可以使用
[show_child_pages show_excerpt="0"]
具有ob_start()
, ob_get_contents()
和ob_end_clean()
我们可以截取第一个函数的内容。只有使用此函数,才能处理我们的短代码。第一个函数的内容将在此处缓冲。
<小时>
Some explanations:
$atts = shortcode_atts(array(
\'sort_order\' => \'asc\',
\'sort_column\' => \'post_title\',
\'show_excerpt\' => 1, // excerpt displays as default, set to any other number to disable
), $atts );
extract( $atts );
这些都是短代码的属性。您可以看到,我们可以在这里混合属性。例如
sort_order
和
sort_column
是我们将插入到
get_pages()
查询为参数。但是
show_excerpt
只是一个自定义属性。在后面的代码中,我们可以检查是否以及输入了什么作为属性。
if ($show_excerpt == 1) {
echo \'<p>\'.$child_page_excerpt.\'</p>\';
}
这里我们只检查属性
$show_excerpt
设置为1。如果输入了任何其他数字或字符串,我们根本不显示摘录。
$args = array(
\'child_of\' => $current_page_id,
\'parent\' => $current_page_id, // add this to only show 1 level deep, remove to also show grand children
\'post_type\' => \'page\',
\'sort_order\' => $sort_order, //this comes from the shortcode_atts above, so you can change it with the shortcode
\'sort_column\' => $sort_column, //this comes from the shortcode_atts above, so you can change it with the shortcode
);
这些是get\\u pages函数的参数。具有
\'child_of\' => $current_page_id,
我们确保只列出当前页面的子级
带
\'parent\' => $current_page_id,
我们可以影响深度。如果我们也将其设置为当前页面,则不会列出任何孙子孙女。我们需要将此用作
get_pages
没有
depth
论点