如何将页面的自定义字段数据作为模板文件中的值推送?

时间:2013-01-19 09:49:03

标签: wordpress wordpress-theming

长话短说。我正在尝试扩展投资组合模板的功能。它目前只能处理一页。我可以复制模板,依靠自定义字段来提取特定的portfilio项目(主题使用自定义帖子类型)。

所以,这是我的portfolio-template.php中的一点:

   $args = array(
          'post_type' => 'portfolio',
        'orderby' => 'menu_order',
        'order' => 'ASC',
        'meta_value' => 'ao',
        'posts_per_page' => -1
    );

所以,我想创建一个新页面,选择项目组合模板,然后在该页面上定义一个自定义字段,该字段将meta_value传递给portfolio-template.php

所以它看起来像这样:

    $args = array(
        'post_type' => 'portfolio',
        'orderby' => 'menu_order',
        'order' => 'ASC',
        'meta_value' => 'PULL_VALUE_FROM_CUSTOM_FIELD',
        'posts_per_page' => -1
    );

“PULL_VALUE_FROM_CUSTOM_FIELD”是我无法弄清楚的。对不起,如果我的白话是code_crude,我是新来的!

编辑:添加模板的完整代码

    <?php
    $args = array(
        'post_type' => 'portfolio',
        'orderby' => 'menu_order',
        'order' => 'ASC',
        'meta_value' => 'ao',
        'posts_per_page' => -1
    );
    $portfolio_query = new WP_Query($args);

    if( $portfolio_query->have_posts() ) : 

        echo '<div id="primary" class="hfeed">';

        while( $portfolio_query->have_posts() ) : $portfolio_query->the_post();

        // styles
        $style = 'zilla-' . get_post_meta($post->ID, '_zilla_portfolio_style', true);
        $media_pos = 'zilla-' . get_post_meta($post->ID, '_zilla_portfolio_media_position', true);
        $width = ( $media_pos == 'zilla-media-center' ) ? 940 : 600;

        // project url
        $portfolio_url = get_post_meta($post->ID, '_zilla_portfolio_project_url', true);
        if( !empty($portfolio_url) ) 
            $portfolio_button_copy = get_post_meta($post->ID, '_zilla_portfolio_project_url_copy', true);

        // determine which media to display
        $portfolio_display_gallery = get_post_meta($post->ID, '_zilla_portfolio_display_gallery', true);
        $portfolio_display_video = get_post_meta($post->ID, '_zilla_portfolio_display_video', true);
        $portfolio_display_audio = get_post_meta($post->ID, '_zilla_portfolio_display_audio', true);

        // grab everything else
        $custom_bg = get_post_meta($post->ID, '_zilla_portfolio_display_background', true);
        $portfolio_caption = get_post_meta($post->ID, '_zilla_portfolio_caption', true);

    ?>

            <?php zilla_page_before(); ?>
            <!--BEGIN .hentry-->
            <div <?php post_class( $style . ' ' . $media_pos ) ?> id="post-<?php the_ID(); ?>">
            <?php zilla_page_start(); ?>

                <div class="hentry-inner">

                    <!--BEGIN .entry-media -->
                    <div class="entry-media">

                        <?php 
                        if( $portfolio_display_gallery == 'on' ) {
                            $gallery_layout = get_post_meta( $post->ID, '_zilla_gallery_layout', true);
                            $slideshow = ( $gallery_layout == 'slideshow' ) ? true : false;
                            $size = ( $media_pos == 'zilla-media-center' ) ? 'portfolio-full' : 'portfolio-index';
                            zilla_gallery( $post->ID, $size, $slideshow, $slideshow );
                        }

                        if( $portfolio_display_video == 'on' ) {
                            $embed = get_post_meta($post->ID, '_zilla_video_embed_code', true);
                            if( !empty( $embed ) ) {
                                echo stripslashes(htmlspecialchars_decode($embed));
                            } else {
                                zilla_video( $post->ID, $width );
                            }
                        }

                        if( $portfolio_display_audio == 'on' ) {
                            zilla_audio( $post->ID, $width );
                        }

                        ?>
                    <!--END .entry-media -->
                    </div>

                    <!--BEGIN .entry-content -->
                    <div class="entry-content">

                        <h2 class="entry-title"><?php the_title(); ?></h2>

                        <?php if( !empty($portfolio_caption) ) 
                            echo "<p class='zilla-caption'>" . stripslashes(htmlspecialchars_decode($portfolio_caption)) . "</p>"; ?>

                        <?php if( !empty($portfolio_url) && $media_pos == 'zilla-media-center' ) { ?>
                            <a href="<?php echo esc_url($portfolio_url); ?>" class="more-link"><?php echo $portfolio_button_copy; ?></a>
                        <?php } ?>

                        <?php the_content(); ?>

                        <?php wp_link_pages(array('before' => '<p><strong>'.__('Pages:', 'zilla').'</strong> ', 'after' => '</p>', 'next_or_number' => 'number')); ?>

                        <?php if( !empty($portfolio_url) && ( $media_pos == 'zilla-media-left' || $media_pos == 'zilla-media-right' ) ) { ?>
                            <a href="<?php echo esc_url($portfolio_url); ?>" class="more-link"><?php echo $portfolio_button_copy; ?></a>
                        <?php } ?>

                    <!--END .entry-content -->
                    </div>

                </div>

            <?php zilla_page_end(); ?>
            <!--END .hentry-->
            </div>
            <?php zilla_page_after(); ?>

        <?php endwhile; ?>

        <!--END #primary .hfeed-->
        </div>

    <?php else: ?>

    <div id="content">
        <!--BEGIN #post-0-->
        <div id="post-0" <?php post_class(); ?>>

            <h2 class="entry-title"><?php _e('Error: No Portfolios Found', 'zilla') ?></h2>

            <!--BEGIN .entry-content-->
            <div class="entry-content">
                <p><?php _e("Sorry, but no portfolios have been created.", "zilla") ?></p>
            <!--END .entry-content-->
            </div>

        <!--END #post-0-->
        </div>
    </div>

    <?php endif; ?>

1 个答案:

答案 0 :(得分:0)

在循环之前,您可以访问全局变量$post,在single.phppage.php的情况下,它将表示非常发布的类型(post / page / cpt)对象由模板文件显示。

如果没有弄错,例如,在category.php中使用时,它会抓住第一个。

因此,在有问题的情况下:

<?php
global $post;
$the_meta = get_post_meta( 'custom_field_name', $post->ID );

if( !$the_meta )
    $the_meta = 'your_backup_solution';

$args = array(
    'post_type'      => 'portfolio',
    'orderby'        => 'menu_order',
    'order'          => 'ASC',
    'meta_value'     => $the_meta,
    'posts_per_page' => -1
);

$portfolio_query = new WP_Query($args);
// et cetera