搜索结果页面-显示结果的元数据值

时间:2015-04-28 作者:Mohamed Mokhtar

我有此自定义搜索结果页

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\',
) );

2 个回复
最合适的回答,由SO网友:fischi 整理而成

您只需要使用get_post_meta().

<h1><a href="<?php echo get_permalink()?>"><?php the_title(); ?></a></h1>
<span class="description">
    <?php echo get_post_meta( get_the_ID(), \'mobile_adv_service_desc\', true ); ?>
</span>
此插件将您的值存储为WordPress元数据。因此,要检索前端中的值,可以使用内置的WordPress函数。

SO网友:efoula

将其放在自定义搜索页面而不是代码中,不要忘记在查询参数中定义帖子类型。。

<?php
// Query arguments
global $wpdb;
// If you use a custom search form
// $keyword = sanitize_text_field( $_POST[\'keyword\'] );
// If you use default WordPress search form
$keyword = get_search_query();
$keyword = \'%\' . like_escape( $keyword ) . \'%\'; // Thanks Manny Fleurmond
// Search in all custom fields
$post_ids_meta = $wpdb->get_col( $wpdb->prepare( "
    SELECT DISTINCT post_id FROM {$wpdb->postmeta}
    WHERE meta_value LIKE \'%s\'
", $keyword ) );
// Search in post_title and post_content
$post_ids_post = $wpdb->get_col( $wpdb->prepare( "
    SELECT DISTINCT ID FROM {$wpdb->posts}
    WHERE post_title LIKE \'%s\'
    OR post_content LIKE \'%s\'
", $keyword, $keyword ) );
$post_ids = array_merge( $post_ids_meta, $post_ids_post );
// Query arguments
$args = array(
    \'post_type\'   => array(\'post\'),
    \'post_status\' => \'publish\',
    \'post__in\'    => $post_ids,
);
$query = new WP_Query( $args );
if ( $query->have_posts() ): while ( $query->have_posts() ) : $query->the_post();
// Do loop here
    echo \'<a href="\'.get_permalink().\'">\';
    echo the_title();
    echo \'</a>\';
    echo \'<br>\';
    echo the_content();




endwhile;

else :
    _e(\'Sorry, no posts matched your criteria.\');

endif;
?>
谢谢你。最好的,EF。

结束