使用WordPress函数get_post_custom_keys()检索帖子的元字段名称
此get_post_custom
函数是 WordPress 中的一项功能,用于检索帖子的元字段名称。如果没有元字段,则不会返回任何内容(null)
推荐:[最新版]WordPress SEO插件Rank Math Pro
get_post_custom_keys()基本语法
描述
WordPress PHP get_post_custom_keys()函数检索帖子的元字段名称
用法
$custom_keys = get_post_custom_keys( $post_id );
$post_id
(int,可选)– 帖子 ID。默认为全局 ID$post
推荐:[最新版]YITH WooCommerce Social Login社交登录插件
get_post_custom_keys()函数
检索帖子的元字段名称。如果没有元字段,则不会返回任何内容(null)如果检索到,则为键的数组(源文件可参考这里)
function get_post_custom_keys( $post_id = 0 ) {
$custom = get_post_custom( $post_id );
if ( ! is_array( $custom ) ) {
return;
}
$keys = array_keys( $custom );
if ( $keys ) {
return $keys;
}
}
推荐:[最新版]Hero Menu免费下载响应式WordPress Mega Menu超级菜单插件
如何使用get_post_custom_keys()
检索并显示特定帖子的自定义字段键
// Set the post ID
$post_id = 42;
// Get the custom field keys
$custom_keys = get_post_custom_keys($post_id);
// Display the custom field keys
if ($custom_keys) {
foreach ($custom_keys as $key) {
if (substr($key, 0, 1) != '_') {
echo $key . "<br>";
}
}
}
在循环内检索当前帖子的自定义字段键
// Get the custom field keys
$custom_keys = get_post_custom_keys();
// Display the custom field keys
if ($custom_keys) {
foreach ($custom_keys as $key) {
if (substr($key, 0, 1) != '_') {
echo $key . "<br>";
}
}
}
获取自定义字段键并排除特定键
// Get the custom field keys
$custom_keys = get_post_custom_keys();
// Exclude specific keys
$exclude_keys = array('key_to_exclude', 'another_key_to_exclude');
// Display the custom field keys
if ($custom_keys) {
foreach ($custom_keys as $key) {
if (!in_array($key, $exclude_keys) && substr($key, 0, 1) != '_') {
echo $key . "<br>";
}
}
}
获取自定义字段键及其值
// Get the custom field keys
$custom_keys = get_post_custom_keys();
// Display the custom field keys and their values
if ($custom_keys) {
foreach ($custom_keys as $key) {
if (substr($key, 0, 1) != '_') {
$values = get_post_custom_values($key);
echo $key . " => " . implode(', ', $values) . "<br>";
}
}
}
计算并显示帖子的自定义字段数量
// Get the custom field keys
$custom_keys = get_post_custom_keys();
// Count the custom fields
$count = 0;
if ($custom_keys) {
foreach ($custom_keys as $key) {
if (substr($key, 0, 1) != '_') {
$count++;
}
}
}
// Display the number of custom fields
echo "Number of custom fields: " . $count;
推荐:[最新版]WP Speed of Light Pro插件WordPress速度优化插件
Claude、Netflix、Midjourney、ChatGPT Plus、PS、Disney、Youtube、Office 365、多邻国Plus账号购买,ChatGPT API购买,优惠码XDBK,用户购买的时候输入优惠码可以打95折