向WordPress API请求版本字段的任何插件或主题信息时https://api.wordpress.org/themes/info/1.1/?action=theme_information&request[per_page]=12&request[slug]=twentyfifteen&request[fields][tags]=0&request[fields][sections]=0&request[fields][screenshot_url]=0&request[fields][versions]=1
我得到JSON数组响应。类似这样:"1.0":"https://downloads.wordpress.org/theme/twentyfifteen.1.0.zip", "1.1":"https://downloads.wordpress.org/theme/twentyfifteen.1.1.zip", "1.2":"https://downloads.wordpress.org/theme/twentyfifteen.1.2.zip",
这些也是动态生成的
所以我想动态地将版本号转换为简单文本,将下载链接转换为按钮href URL。另外,我想将整个部分包装成一个漂亮的表,其中有两列,一列有版本号,另一列有下载按钮。如果有人能帮我写代码,我将不胜感激。提前谢谢。
WordPress主题/插件信息API对文本和按钮的响应
1 个回复
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\' );