我有此自定义搜索结果页
global $query_string;
$query_args = explode("&", $query_string);
$search_query = array(
\'nopaging\' => true,
\'post_type\' => \'page\'
);
foreach($query_args as $key => $string) {
$query_split = explode("=", $string);
$search_query[$query_split[0]] = urldecode($query_split[1]);
} // foreach
$search = new WP_Query($search_query);
<div class="page_title">
<div class="container">
<h1><?php echo $search->found_posts; ?> <?php if ( is_rtl() ) { echo \'نتائج بحث عن\'; } else { echo \'Search Results Found For\'; } ?> : "<?php the_search_query(); ?>"</h1>
</div>
</div>
<div class="content_fullwidth">
<div class="container">
<?php if ( $search->have_posts() ) : while ( $search->have_posts() ) : $search->the_post(); ?>
<h1><a href="<?php echo get_permalink()?>"><?php the_title(); ?></a></h1>
<?php endwhile; else : ?>
<p><?php _e( \'Sorry, no posts matched your criteria.\' ); ?></p>
<?php endif; ?>
</div>
</div>
我期望的搜索结果将具有图像和描述的自定义字段,这些字段具有不同的ID,这取决于此结果来自哪个页面,我在找出从这些自定义字段中提取值的正确代码时遇到了一个问题。
<h1><a href="<?php echo get_permalink()?>"><?php the_title(); ?></a></h1>
工作正常,因为它更像是一个通用函数,可以处理所有结果。因此,我的问题是找出一个动态代码来提取这些自定义字段值,因为它们在页面ID和自定义字段ID方面具有不同的ID。
注意:我正在使用CMB2 要显示自定义元框,下面是显示在特定页面上的元框示例
function mobile_adv_metabox() {
// Start with an underscore to hide fields from custom fields list
$prefix = \'mobile_adv_\';
/**
* Metabox to be displayed on a single page ID
*/
$cmb_mobile_adv = new_cmb2_box( array(
\'id\' => $prefix . \'metabox\',
\'title\' => __( \'Page Details\', \'cmb2\' ),
\'object_types\' => array( \'page\', ), // Post type
\'context\' => \'normal\',
\'priority\' => \'high\',
\'show_names\' => true, // Show field names on the left
\'show_on\' => array( \'id\' => array( 10 ) ), // Specific post IDs to display this metabox
) );
$cmb_mobile_adv->add_field( array(
\'name\' => \'Service Image\',
\'desc\' => \'Upload an image or add one from the library. \',
\'id\' => $prefix . \'service_image\',
\'type\' => \'file\',
\'options\' => array(
\'url\' => false,
),
) );
$cmb_mobile_adv->add_field( array(
\'name\' => \'Service Description\',
\'desc\' => \'Add a description for the service.\',
\'id\' => $prefix . \'service_desc\',
\'type\' => \'textarea\',
) );