我有一个多站点安装:
1:主站点-domain.com
2:其他网站-domain.com/some-url
已修复:到目前为止一切顺利。我的脚本会在标题下方自动显示内容,而不是在我的简码插入位置显示内容。
function subsites_list_sites() {
$subsites = get_sites();
if ( ! empty ( $subsites ) ) {
echo '<div class="subsites-container">';
foreach( $subsites as $subsite ) {
$subsite_id = get_object_vars( $subsite )["blog_id"];
$subsite_name = get_blog_details( $subsite_id )->blogname;
$subsite_link = get_blog_details( $subsite_id )->siteurl;
echo '<div class="subtite-item site-' . $subsite_id . '">
<a class="thumb" href="' . $subsite_link . '">Here i want the featured image/thumbnail to display</a>
<a class="title-link" href="' . $subsite_link . '"><h3 class="title">' . $subsite_name . '</h3></a>
<a class="excerpt" href="' . $subsite_link . '"><p>Here i want excerpt<p></a>
<a class="btn-link" href="' . $subsite_link . '">GO TO WEBSITE</a>
</div>';
}
echo '</div>';
}
}
add_shortcode( 'subsites_list_sites_sc', 'subsites_list_sites' );
答案 0 :(得分:1)
Shortcodes应该返回html而不是在shortcode函数中打印出来。
尝试这样:
function subsites_list_sites() {
$subsites = get_sites();
$returnHtml = '';
if ( ! empty ( $subsites ) ) {
$returnHtml .= '<div class="subsites-container">';
foreach( $subsites as $subsite ) {
$subsite_id = get_object_vars( $subsite )["blog_id"];
$subsite_name = get_blog_details( $subsite_id )->blogname;
$subsite_link = get_blog_details( $subsite_id )->siteurl;
$returnHtml .= '<div class="subtite-item site-' . $subsite_id . '">
<a class="thumb" href="' . $subsite_link . '">Here i want the featured image/thumbnail to display</a>
<a class="title-link" href="' . $subsite_link . '"><h3 class="title">' . $subsite_name . '</h3></a>
<a class="excerpt" href="' . $subsite_link . '"><p>Here i want excerpt<p></a>
<a class="btn-link" href="' . $subsite_link . '">GO TO WEBSITE</a>
</div>';
}
$returnHtml .= '</div>';
}
return $returnHtml;
}
输出
插入了shortcode处理函数的返回值 代替shortcode宏放入帖子内容输出中。记得 使用return而不是echo-任何回显的东西都将输出到 浏览器,但它不会出现在页面上的正确位置。 https://codex.wordpress.org/Shortcode_API#Output