内容营销
WordPress:如何使用简码列出子页面
我们已经为我们的几个网站重建了网站层次结构 WordPress 客户,我们尝试做的事情之一就是有效地组织信息。为此,我们通常希望创建一个母版页并包含一个自动列出其下方页面的菜单。子页面或子页面的列表。
不幸的是,WordPress 中没有固有的功能或特性来执行此操作,因此我们开发了一个短代码来添加到客户端的站点。以下是如何使用短代码及其在 WordPress 帖子或页面中填充的所有变量:
[listchildpages ifempty="No child pages found" order="ASC" orderby="title" ulclass="custom-ul-class" liclass="custom-li-class" aclass="custom-a-class" displayimage="yes" align="aligncenter"]
用途细目:
ifempty="No child pages found"
:如果没有可用的子页面,将显示此文本。order="ASC"
:这会按升序对子页面列表进行排序。orderby="title"
:这将按标题对子页面进行排序。ulclass="custom-ul-class"
:将 CSS 类“custom-ul-class”应用于<ul>
列表的元素。liclass="custom-li-class"
:将 CSS 类“custom-li-class”应用于每个<li>
列表中的元素。aclass="custom-a-class"
:将 CSS 类“custom-a-class”应用于每个<a>
列表中的(链接)元素。displayimage="yes"
:这包括列表中每个子页面的特色图像。align="aligncenter"
:这会将特色图像居中对齐。
将此短代码直接插入到您希望显示子页面列表的 WordPress 帖子或页面的内容区域中。请记住自定义每个属性的值以适合您的 WordPress 网站的设计和结构。
另外,如果您想 简短摘录 在描述每个页面时,该插件会启用页面摘录,以便您可以在页面设置中编辑该内容。
列出子页面简码
function add_shortcode_listchildpages($atts, $content = "") {
global $post;
$string = '';
$atts = shortcode_atts(array(
'ifempty' => '<p>No Records</p>',
'order' => 'DESC',
'orderby' => 'publish_date',
'ulclass' => '',
'liclass' => '',
'aclass' => '',
'displayimage' => 'no',
'align' => 'alignleft'
), $atts, 'listchildpages');
$args = array(
'post_type' => 'page',
'posts_per_page' => -1,
'post_parent' => $post->ID,
'orderby' => $atts['orderby'],
'order' => $atts['order']
);
$parent = new WP_Query($args);
if ($parent->have_posts()) {
$string .= $content.'<ul class="'.$atts['ulclass'].'">';
while ($parent->have_posts()) : $parent->the_post();
$string .= '<li class="'.$atts['liclass'].'">';
$true = array("y", "yes", "t", "true");
$showimage = strtolower($atts['displayimage']);
if (in_array($showimage, $true)) {
if (has_post_thumbnail($post->ID)) {
$image_attributes = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'thumbnail');
$string .= '<a class="'.$atts['aclass'].'" href="'.get_permalink().'" title="'.get_the_title().'">';
$string .= '<img src="'.$image_attributes[0].'" width="'.$image_attributes[1].'" height="'.$image_attributes[2].'" alt="'.get_the_title().'" class="'.$atts['align'].'" /></a>';
}
}
$string .= '<a class="'.$atts['aclass'].'" href="'.get_permalink().'" title="'.get_the_title().'">'.get_the_title().'</a>';
if (has_excerpt($post->ID)) {
$string .= ' - '.get_the_excerpt();
}
$string .= '</li>';
endwhile;
$string .= '</ul>';
} else {
$string = $atts['ifempty'];
}
wp_reset_postdata();
return $string;
}
add_shortcode('listchildpages', 'add_shortcode_listchildpages');
该功能 add_shortcode_listchildpages
添加自定义简码
No Records
- 全局后置变量:函数首先声明全局变量
$post
,用于访问有关 WordPress 中当前帖子或页面的信息。 - 简码属性:本
shortcode_atts
函数设置短代码属性的默认值。用户可以在插入短代码时覆盖这些。属性包括:ifempty
:没有子页面时显示的消息。order
:子页面的顺序(ASC 或 DESC)。orderby
:子页面的排序标准(例如,publish_date)。ulclass
: CSS 类<ul>
元件。liclass
: CSS 类<li>
元素。aclass
: CSS 类<a>
(锚)元素。displayimage
:是否显示子页面的特色图片。align
:特色图像的对齐方式。
- 查询参数:该函数设置了一个
WP_Query
检索当前页面的所有子页面,并根据指定的属性排序。 - 生成列表:
- 如果找到子页面,该函数将构造一个 HTML 无序列表 (
<ul>
),每个子页面由一个列表项(<li>
). - 在每个列表项中,该函数根据列表检查是否显示特色图像
displayimage
属性。 - 该函数还使用以下方法创建到每个子页面的链接
<a>
标签,如果可用,添加子页面的摘录。
- 如果找到子页面,该函数将构造一个 HTML 无序列表 (
- 输出或默认消息:如果没有子页面,则该函数输出由
ifempty
属性。 - 重置发布数据:本
wp_reset_postdata
函数重置 WordPress 查询,确保全局$post
对象恢复到原始主查询的帖子。 - 简码注册:最后,
add_shortcode
函数寄存器listchildpages
作为一个新的短代码,将其链接到add_shortcode_listchildpages
功能,使其可在帖子和页面中使用。
此功能对于在父页面上动态列出子页面、增强 WordPress 站点内的导航和组织非常有用。如果您想将其添加到您的 WordPress 网站,我建议您将其添加到自定义插件中。或者……你可以下载我发布的插件。
列出子页面简码插件
我终于开始将代码推送到插件中,以使其易于安装和使用,并且 列出子页面的简码插件 今天已获得 WordPress 批准!请下载并安装它 - 如果您喜欢它,请提供评论!