SO网友:Paul G.
这有很多。。。我觉得我在为你做你的工作,但这就是。
自动生成一个表,以显示中返回的主题的每个版本的下载按钮themes_api()
要求
您必须在适当的时间运行此操作-至少在WordPress之后init
:
只需使用所需的主题slug调用函数:
function display_version_downloads_table_for_theme( $slug ) {
if ( !function_exists( \'themes_api\' ) ) {
echo "You\'re calling this function display_version_downloads_table_for_theme() too soon.";
return;
}
$result = themes_api( \'theme_information\', [
\'slug\' => $slug,
\'fields\' => [
\'versions\' => true,
\'sections\' => false,
\'screenshot_url\' => false,
\'tags\' => false,
]
] );
$versions = ( is_object( $result ) && !empty( $result->versions ) && is_array( $result->versions ) ) ? $result->versions : [];
foreach ( $versions as $version => $link ) {
if ( empty( $version ) ) {
unset( $versions[ $version ] );
}
}
if ( empty( $versions ) ) {
echo \'Themes API request either failed, or there were no versions available for the requested theme\';
}
else {
?>
<script type="text/javascript">
function runDownload( button ) {
window.open( button.getAttribute( \'data-href\' ), \'_blank\' );
}
</script>
<table>
<thead>
<tr>
<th>Version</th>
<th>Download</th>
</tr>
</thead>
<tbody>
<?php foreach ( $versions as $version => $href ) : ?>
<tr>
<td><?= $version ?></td>
<td>
<button onclick="runDownload( this )" data-href="<?= esc_url( $href ) ?>">
Download
</button>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php }
}
display_version_downloads_table_for_theme( \'twentyfifteen\' );