在前端从主题选项调用自定义字段

时间:2014-10-01 作者:user3756781

从自定义主题选项页面调用字段并在前端显示其内容时遇到问题。这是我用来在后端显示自定义主题选项的代码

  add_action( \'admin_init\', \'theme_options_init\' );
  add_action( \'admin_menu\', \'theme_options_add_page\' );

 /** Init plugin options to white list our options **/
  function theme_options_init(){
        register_setting( \'options\', \'website_options\', \'theme_options_validate\' );
  }
 /** Load up the menu page **/
  function theme_options_add_page() {
        add_theme_page( __( \'Blurb Options\', \'websiteBlurbs\' ), __( \'Blurb Options\',      \'websiteBlurbs\' ), \'edit_theme_options\', \'theme_options\', \'theme_options_do_page\' );
 }

 /** Create the options page **/
  function theme_options_do_page() {
            if ( ! isset( $_REQUEST[\'settings-updated\'] ) )
            $_REQUEST[\'settings-updated\'] = false;
 ?>
  <div class="wrap">
  <?php screen_icon(); echo "<h2>" . get_current_theme() . __( \' Page Settings\', \'websiteBlurbs\' ) . "</h2>"; ?>
  <?php if ( false !== $_REQUEST[\'settings-updated\'] ) : ?>
  <div class="updated fade"><p><strong><?php _e( \'Options saved\', \'websiteBlurbs\' ); ?></strong></p></div>
  <?php endif; ?>
  <form method="post" action="options.php">
        <?php settings_fields( \'options\' ); ?>
        <?php $options = get_option( \'website_options\' ); ?>
        <table class="form-table">
            <td><h2>About Page</h2></td>
            <?php /* ABOUT */ ?>
            <tr valign="top"><th scope="row"><?php _e( \'Title Blurb\', \'aboutBlurb\' ); ?></th>
        <td>
                    <input id="website_options[about]" class="regular-text" type="text" name="website_options[about]" value="<?php esc_attr_e( $options[\'about\'] ); ?>" style="width:50%; padding:1em;" />
                    <label class="description" for="website_options[about]"></label>
                </td>
            </tr>
            <td><h2>Our Team</h2></td>
            <?php /* PROPERTY */ ?>
            <tr valign="top"><th scope="row"><?php _e( \'Title Blurb\', \'teamBlurb\' ); ?></th>
                <td>
                    <input id="website_options[team]" class="regular-text" type="text" name="website_options[team]" value="<?php esc_attr_e( $options[\'team\'] ); ?>" style="width:50%; padding:1em;" />
                    <label class="description" for="website_options[team]"></label>
                </td>
            </tr>
            <td><h2>Property Page</h2></td>
            <?php /* PROPERTY */ ?>
            <tr valign="top"><th scope="row"><?php _e( \'Title Blurb\', \'propertyBlurb\' ); ?></th>
                <td>
                    <input id="website_options[property]" class="regular-text" type="text" name="website_options[property]" value="<?php esc_attr_e( $options[\'property\'] ); ?>" style="width:50%; padding:1em;" />
                    <label class="description" for="website_options[property]"></label>
                </td>
            </tr>
        </table>
        <p class="submit">
            <input type="submit" class="button-primary" value="<?php _e( \'Save Options\', \'websiteBlurbs\' ); ?>" />
        </p>
  </form>
  </div>
  <?php
 }

 /** Sanitize and validate input. Accepts an array, return a sanitized array. **/
  function theme_options_validate( $input ) {
 // Say our text option must be safe text with no HTML tags
        $input[\'about\'] = wp_filter_nohtml_kses( $input[\'about\'] );
        $input[\'property\'] = wp_filter_nohtml_kses( $input[\'property\'] );
        $input[\'team\'] = wp_filter_nohtml_kses( $input[\'team\'] );
        return $input;
 }
我知道如何使用echo get\\u post\\u meta($post->ID,“customfieldname”,true),从自定义post类型调用字段;但是,当我尝试使用此函数调用字段aboutBlurb时,我无法从字段中生成要在前端显示的内容。我没有收到任何错误或无法显示的原因。

要使这项工作正常运行,我缺少什么?

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

我不确定您是如何决定保存选项的,但如果我根据上面的代码进行猜测,您正在将“aboutBlurb”保存为website_options 数组,我还假设您在实际WordPress Options - website_options[about].

要获取这些数据,可以尝试以下方法:

$website_options = get_option(\'website_options\');
echo $website_options[\'about\'];

SO网友:Imran Javed

这样试试,可能对你有帮助。我获取a页面的线程,然后通过输入其名称获取关联的自定义字段。

     $homeInsurance= new WP_Query( \'post_type=page&page_id=7\' );    
        while ($homeInsurance->have_posts()) : $homeInsurance->the_post();

        $thisPageID         = get_the_id(); 
        $home_insurance_urdu_translation_image_path = get_post_meta($thisPageID, \'home_insurance_urdu_translation_image_path\', true);


        endwhile;

结束