对于搜索结果页面,我正在制作一个自定义模板。在这个自定义模板中,我想显示自定义元字段值。
但是,当我这样做时:
<?php if ( have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php
$pName = get_post_meta($post->ID, $productName, \'true\');
echo $pName;
);
?>
<?php endwhile; ?>
<?php else : ?>
<?php get_template_part( \'content\', \'none\' ); ?>
<?php endif;
我得到的结果不是自定义字段的值,而是:
Array
当我var\\u转储$pName时,它会显示所有正确的自定义字段内容。
问:为什么我会得到一个数组作为结果(当我告诉它这是一个带有“true”的单一结果时),我如何修复它以使它显示正确的内容?
谢谢
---更新#1---
var\\u dump($pName),设置为true:
array(12) { ["productName"]=> array(1) { [0]=> string(10) "Lemon Kush" } ["locationName"]=> array(1) { [0]=> string(57) "La Brea Caregivers THE BEST $35 CAP in the 90019 Zip Code" } ["locationAddress"]=> array(1) { [0]=> string(18) "1188 S La Brea Ave" } ["locationCity"]=> array(1) { [0]=> string(11) "Los Angeles" } ["locationState"]=> array(1) { [0]=> string(11) " California" } ["locationZip"]=> array(1) { [0]=> string(6) " 90019" } ["locationPhone"]=> array(1) { [0]=> string(15) "1(323)-549-0400" } ["meta_priceGram"]=> array(1) { [0]=> string(0) "" } ["meta_priceEighth"]=> array(1) { [0]=> string(0) "" } ["meta_priceQuarter"]=> array(1) { [0]=> string(0) "" } ["meta_priceHalfOunce"]=> array(1) { [0]=> string(0) "" } ["meta_priceOunce"]=> array(1) { [0]=> string(0) "" } } string(0) "" array(12) { ["productName"]=> array(1) { [0]=> string(10) "Lemon Kush" } ["locationName"]=> array(1) { [0]=> string(57) "La Brea Caregivers THE BEST $35 CAP in the 90019 Zip Code" } ["locationAddress"]=> array(1) { [0]=> string(18) "1188 S La Brea Ave" } ["locationCity"]=> array(1) { [0]=> string(11) "Los Angeles" } ["locationState"]=> array(1) { [0]=> string(11) " California" } ["locationZip"]=> array(1) { [0]=> string(6) " 90019" } ["locationPhone"]=> array(1) { [0]=> string(15) "1(323)-549-0400" } ["meta_priceGram"]=> array(1) { [0]=> string(1) "5" } ["meta_priceEighth"]=> array(1) { [0]=> string(2) "15" } ["meta_priceQuarter"]=> array(1) { [0]=> string(0) "" } ["meta_priceHalfOunce"]=> array(1) { [0]=> string(0) "" } ["meta_priceOunce"]=> array(1) { [0]=> string(0) "" } } string(0) "" array(12) { ["productName"]=> array(1) { [0]=> string(39) "Bananna Kush ***NEW*** 5grm 1/8 Special" } ["locationName"]=> array(1) { [0]=> string(10) "H+H CO-OP " } ["locationAddress"]=> array(1) { [0]=> string(21) "1938 West. Adams blvd" } ["locationCity"]=> array(1) { [0]=> string(11) "Los Angeles" } ["locationState"]=> array(1) { [0]=> string(3) " CA" } ["locationZip"]=> array(1) { [0]=> string(6) " 90018" } ["locationPhone"]=> array(1) { [0]=> string(12) "323-733-0442" } ["meta_priceGram"]=> array(1) { [0]=> string(2) "14" } ["meta_priceEighth"]=> array(1) { [0]=> string(2) "55" } ["meta_priceQuarter"]=> array(1) { [0]=> string(0) "" } ["meta_priceHalfOunce"]=> array(1) { [0]=> string(0) "" } ["meta_priceOunce"]=> array(1) { [0]=> string(0) "" } } string(0) "" array(12) { ["productName"]=> array(1) { [0]=> string(37) "Master Kush 5grm 1/8 Special **NEW**" } ["locationName"]=> array(1) { [0]=> string(10) "H+H CO-OP " } ["locationAddress"]=> array(1) { [0]=> string(21) "1938 West. Adams blvd" } ["locationCity"]=> array(1) { [0]=> string(11) "Los Angeles" } ["locationState"]=> array(1) { [0]=> string(3) " CA" } ["locationZip"]=> array(1) { [0]=> string(6) " 90018" } ["locationPhone"]=> array(1) { [0]=> string(12) "323-733-0442" } ["meta_priceGram"]=> array(1) { [0]=> string(2) "10" } ["meta_priceEighth"]=> array(1) { [0]=> string(2) "50" } ["meta_priceQuarter"]=> array(1) { [0]=> string(0) "" } ["meta_priceHalfOunce"]=> array(1) { [0]=> string(0) "" } ["meta_priceOunce"]=> array(1) { [0]=> string(0) "" } } string(0) "" array(12) { ["productName"]=> array(1) { [0]=> string(7) "OG Kush" } ["locationName"]=> array(1) { [0]=> string(30) "The Dragon Collective $45 CAP " } ["locationAddress"]=> array(1) { [0]=> string(16) "3977 West 6th st" } ["locationCity"]=> array(1) { [0]=> string(11) "Los Angeles" } ["locationState"]=> array(1) { [0]=> string(3) " CA" } ["locationZip"]=> array(1) { [0]=> string(6) " 90020" } ["locationPhone"]=> array(1) { [0]=> string(12) "213-529-4016" } ["meta_priceGram"]=> array(1) { [0]=> string(2) "40" } ["meta_priceEighth"]=> array(1) { [0]=> string(0) "" } ["meta_priceQuarter"]=> array(1) { [0]=> string(0) "" } ["meta_priceHalfOunce"]=> array(1) { [0]=> string(0) "" } ["meta_priceOunce"]=> array(1) { [0]=> string(0) "" } } string(0) ""
var\\u dump($pName)true未设置:
array(12) { ["productName"]=> array(1) { [0]=> string(10) "Lemon Kush" } ["locationName"]=> array(1) { [0]=> string(57) "La Brea Caregivers THE BEST $35 CAP in the 90019 Zip Code" } ["locationAddress"]=> array(1) { [0]=> string(18) "1188 S La Brea Ave" } ["locationCity"]=> array(1) { [0]=> string(11) "Los Angeles" } ["locationState"]=> array(1) { [0]=> string(11) " California" } ["locationZip"]=> array(1) { [0]=> string(6) " 90019" } ["locationPhone"]=> array(1) { [0]=> string(15) "1(323)-549-0400" } ["meta_priceGram"]=> array(1) { [0]=> string(0) "" } ["meta_priceEighth"]=> array(1) { [0]=> string(0) "" } ["meta_priceQuarter"]=> array(1) { [0]=> string(0) "" } ["meta_priceHalfOunce"]=> array(1) { [0]=> string(0) "" } ["meta_priceOunce"]=> array(1) { [0]=> string(0) "" } } Array array(0) { } Array array(12) { ["productName"]=> array(1) { [0]=> string(10) "Lemon Kush" } ["locationName"]=> array(1) { [0]=> string(57) "La Brea Caregivers THE BEST $35 CAP in the 90019 Zip Code" } ["locationAddress"]=> array(1) { [0]=> string(18) "1188 S La Brea Ave" } ["locationCity"]=> array(1) { [0]=> string(11) "Los Angeles" } ["locationState"]=> array(1) { [0]=> string(11) " California" } ["locationZip"]=> array(1) { [0]=> string(6) " 90019" } ["locationPhone"]=> array(1) { [0]=> string(15) "1(323)-549-0400" } ["meta_priceGram"]=> array(1) { [0]=> string(1) "5" } ["meta_priceEighth"]=> array(1) { [0]=> string(2) "15" } ["meta_priceQuarter"]=> array(1) { [0]=> string(0) "" } ["meta_priceHalfOunce"]=> array(1) { [0]=> string(0) "" } ["meta_priceOunce"]=> array(1) { [0]=> string(0) "" } } Array array(0) { } Array array(12) { ["productName"]=> array(1) { [0]=> string(39) "Bananna Kush ***NEW*** 5grm 1/8 Special" } ["locationName"]=> array(1) { [0]=> string(10) "H+H CO-OP " } ["locationAddress"]=> array(1) { [0]=> string(21) "1938 West. Adams blvd" } ["locationCity"]=> array(1) { [0]=> string(11) "Los Angeles" } ["locationState"]=> array(1) { [0]=> string(3) " CA" } ["locationZip"]=> array(1) { [0]=> string(6) " 90018" } ["locationPhone"]=> array(1) { [0]=> string(12) "323-733-0442" } ["meta_priceGram"]=> array(1) { [0]=> string(2) "14" } ["meta_priceEighth"]=> array(1) { [0]=> string(2) "55" } ["meta_priceQuarter"]=> array(1) { [0]=> string(0) "" } ["meta_priceHalfOunce"]=> array(1) { [0]=> string(0) "" } ["meta_priceOunce"]=> array(1) { [0]=> string(0) "" } } Array array(0) { } Array array(12) { ["productName"]=> array(1) { [0]=> string(37) "Master Kush 5grm 1/8 Special **NEW**" } ["locationName"]=> array(1) { [0]=> string(10) "H+H CO-OP " } ["locationAddress"]=> array(1) { [0]=> string(21) "1938 West. Adams blvd" } ["locationCity"]=> array(1) { [0]=> string(11) "Los Angeles" } ["locationState"]=> array(1) { [0]=> string(3) " CA" } ["locationZip"]=> array(1) { [0]=> string(6) " 90018" } ["locationPhone"]=> array(1) { [0]=> string(12) "323-733-0442" } ["meta_priceGram"]=> array(1) { [0]=> string(2) "10" } ["meta_priceEighth"]=> array(1) { [0]=> string(2) "50" } ["meta_priceQuarter"]=> array(1) { [0]=> string(0) "" } ["meta_priceHalfOunce"]=> array(1) { [0]=> string(0) "" } ["meta_priceOunce"]=> array(1) { [0]=> string(0) "" } } Array array(0) { } Array array(12) { ["productName"]=> array(1) { [0]=> string(7) "OG Kush" } ["locationName"]=> array(1) { [0]=> string(30) "The Dragon Collective $45 CAP " } ["locationAddress"]=> array(1) { [0]=> string(16) "3977 West 6th st" } ["locationCity"]=> array(1) { [0]=> string(11) "Los Angeles" } ["locationState"]=> array(1) { [0]=> string(3) " CA" } ["locationZip"]=> array(1) { [0]=> string(6) " 90020" } ["locationPhone"]=> array(1) { [0]=> string(12) "213-529-4016" } ["meta_priceGram"]=> array(1) { [0]=> string(2) "40" } ["meta_priceEighth"]=> array(1) { [0]=> string(0) "" } ["meta_priceQuarter"]=> array(1) { [0]=> string(0) "" } ["meta_priceHalfOunce"]=> array(1) { [0]=> string(0) "" } ["meta_priceOunce"]=> array(1) { [0]=> string(0) "" } } Array array(0) { }
---更新#2---
如果有什么区别的话:im想要的元数据来自一个CPT(产品)中的帖子(但我使用“搜索一切”插件将CPT包含在标准搜索结果中)
--找到了--
好吧,我犯了一个很愚蠢的错误。
$productName不应是变量。它应是字段的实际名称,即:productName。
所以做出这样的改变解决了所有的问题。
哈哈。。美好时光:)
谢谢大家的帮助!
SO网友:Craig Pearson
你有一个失踪的)
您将true作为字符串传递,而不是布尔值。所以这一行:
$pName = get_post_meta($post->ID, $productName, \'true\';
事实上应该是:
$pName = get_post_meta($post->ID, $productName, true);
根据您的更新,为什么不执行以下操作:
echo \'Product Name: \'. $pName[\'productName\'][0];
echo \'Location: \'. $pName[\'locationName\'][0];
echo \'Address: \'. $pName[\'locationAddress\'][0];
然而,这段代码需要改进,可能是stackoverflow的一个问题,因为它不是WordPress特有的。这是PHP,处理数组/多维数组