在WordPress主页上显示自定义帖子类型元数据

时间:2012-03-29 18:10:14

标签: wordpress

我真的希望你能在这里帮忙。

我正在为客户创建一个主题,并且我创建了一个名为“Testimonials”的自定义帖子类型,现在我想在主页上显示推荐,但是我无法输出元信息的数据

我在我的functions.php文件中创建了一个名为'show_testimonials'的函数,我在我的index.php文件中调用了这个函数(我还没有将所有内容分成它们的组成部分,例如header.php,footer .php等)。此功能旨在显示“推荐书”自定义帖子类型中的帖子和元信息,但它不会。

我可以看到我正在将正确的元数据提取到我的'$ meta'变量中。

以下是'show_testimonials'功能的代码。

function show_testimonials() {
    $loop = new WP_Query(
        array(
        'post_type' => 'shv_testimonials',
        'order_by' => 'ID',
        )
    );

    if ($loop->have_posts()) {
        $output = '<ul class="testimonial-list">';

        while($loop->have_posts()) {
            $loop->the_post();
            $meta = get_post_meta(get_the_id(), '');
            print_r($meta);

            $output .= '
                <li>
                    <a href="' . get_permalink() . '">
                        ' . get_the_title() . ' | ' .
                        $meta['shv_testimonial_author'][0] . '
                    </a>
                </li>
            ';
        }
    }

    return $output;
}

下面是我的index.php文件。

<!DOCUMENT html>
<!--[if IE 6]>
<html id="ie6" <?php language_attributes(); ?>>
<![endif]-->
<!--[if IE 7]>
<html id="ie7" <?php language_attributes(); ?>>
<![endif]-->
<!--[if IE 8]>
<html id="ie8" <?php language_attributes(); ?>>
<![endif]-->
<!--[if !(IE 6) | !(IE 7) | !(IE) ]><!-->
<html <?php language_attributes(); ?>>
<!--<![endif]-->
<head>
    <meta charset="<?php bloginfo('charset');?>" />
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>
        <?php 

            global $page, $paged;

            wp_title('&laquo;', true, 'right');

            bloginfo('name');

        ?>
    </title>
    <link rel="profile" href="http://gmpg.org/xfn/11" />
    <link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>" type="text/css" media="screen" />
    <link rel="pingback" href="<?php bloginfo('pingback_url'); ?>" />
    <?php if ( is_singular() && get_option('thread_comments')) wp_enqueue_script('comment-reply'); ?>
    <?php wp_enqueue_script('jquery'); ?>
    <?php wp_head(); ?>
</head>
<body <?php body_class(); ?>>
    <div id="page">
        <header id="masthead" role="banner">
            <div id="header-strip">
                <nav id="secondary-navigation" role="navigation">
                    <?php wp_nav_menu(array(
                        'menu' => 'Secondary Navigation',
                        'container' => false
                    )); ?>
                </nav>
                <?php get_search_form(true); ?> 
            </div>
            <div id="branding-nav">
                <?php
                    if (is_front_page()) { ?>
                        <div id="logo" role="banner">
                            <img src="<?php echo bloginfo('template_directory') . '/images/logo.png'; ?>" alt="<?php bloginfo('name'); ?>" /> 
                        </div>
                    <?php } else { ?>
                        <div id="logo" role="banner">
                            <a href="<?php bloginfo('url'); ?>" title="<?php bloginfo('name'); ?>"><img src="<?php echo bloginfo('template_directory') . '/images/logo.png'; ?>" alt="<?php bloginfo('name'); ?>" /></a> 
                        </div>
                    <?php }
                ?>
                <nav id="primary-navigation" role="navigation">
                    <?php wp_nav_menu(array(
                        'menu' => 'Primary Navigation',
                        'container' => false
                    )); ?>
                </nav>
            </div>
        </header>
        <div id="content">
                <?php 
                if(is_front_page()) { ?>
                    <div id="feature">
                        <section id="introduction">
                            <?php
                                echo "<h1 class='title'>" . $options['shv_introduction_heading'] . '</h1>';
                                echo "<p class='short-description'>" . $options['shv_introduction_short_description'] . '</p>';
                            ?>
                            <nav id="call-to-action" role="navigation">
                                <ul>
                                    <li id="book-now">
                                        <a href="<?php echo get_bloginfo('url') . '/bookings/' ?>" title="Book Now">Book Now</a>
                                    </li>
                                    <li id="learn-more">
                                        <a href="<?php echo get_bloginfo('url') . '/what-we-do/' ?>" title="Learn More">Learn More</a>
                                    </li>
                                </ul>
                            </nav>
                        </section>
                        <section id="gallery">

                        </section>
                    </div>
                <?php } else { ?>
                    <section id="entries">
                        <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
                                <article id="post-<?php the_ID(); ?>" <?php post_class(); ?> role="main">
                                    <header>
                                        <aside class="post-image">  
                                            <?php  
                                            if (has_post_thumbnail()) {  
                                                the_post_thumbnail();  
                                            } else { ?>  
                                                <a href="<?php the_permalink() ?>"><img src="<?php echo get_bloginfo('template_directory') . '/images/no-image.png'; ?>" title="<?php the_title(); ?>" /></a>  
                                            <?php }?>  
                                        </aside> 
                                        <h1 id="post-<?php the_ID(); ?>">
                                            <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a>
                                        </h2>
                                        <time datetime="YYYY-MM-DD"></time>
                                    </header>
                                    <section class="post-content">
                                        <p><?php echo get_the_excerpt(); ?></p>
                                        <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>" class="read_more ">Read More</a>
                                    </section>
                                    <section class="meta">
                                        <p><?php the_category(',') ?></p>
                                        <p><?php the_tags(""); ?></p>
                                    </section>
                                </article>
                        <?php endwhile; else: ?>
                                <div class="error">
                                    <h2>Oooops!</h2>
                                    <p>Sorry something went wrong! No posts matched your criteria. Please try again.</p>
                                </div>
                        <?php endif; ?>
                        <?php posts_nav_link(' ⏼ ', __('« Newer Posts'), __('Older Posts »')); ?> 
                        <aside role="sidebar">  
                            <h2>Some Widget in The Sidebar</h2>  
                        </aside>
                    </section>
                <?php } ?>
        </div>
        <footer role="foottext">  
            <div id="footer-top">
                <section id="social">
                    <div id="twitter">
                        <?php show_tweets(); ?>
                    </div>
                    <div id="share">
                        <h2>Share</h2>
                        <div id="icons">
                            <a href="http://twitter.com/home?status=Shiverschool Theatre in Education+http://www.shiverschool.co.uk/" title="Tweet About Us">
                                <img src="<?php echo get_bloginfo('template_directory') . '/images/twitter-share-icon.png'; ?>" alt="Tweet About Us" />
                            </a>
                            <a href="http://www.stumbleupon.com/submit?url={http://www.shiverschool.co.uk}&amp;title={Shiverschool Theatre in Education}" title="Recommend us on StumbleUpon">                                  
                                <img src="<?php echo get_bloginfo('template_directory') . '/images/stumbleupon-share-icon.png'; ?>" alt="Recommend us on StumbleUpon" />
                            </a>
                            <a href="http://digg.com/submit?url=http://www.shiverschool.co.uk/" title="Digg Us"> 
                                <img src="<?php echo get_bloginfo('template_directory') . '/images/digg-share-icon.png'; ?>" alt="Digg Us" />
                            </a>
                            <a href="http://www.facebook.com/sharer.php?u=http://www.shiverschool.co.uk&amp;t=Shiverschool Theatre in Education" title="Recommend us on Facebook"> 
                                <img src="<?php echo get_bloginfo('template_directory') . '/images/facebook-share-icon.png'; ?>" alt="Recommend us on Facebook" />
                            </a>    
                            <a href="https://plusone.google.com/_/+1/confirm?hl=en&url=http://www.shiverschool.co.uk&title=Shiverschool Theatre in Education" title="Recommend us on Google+"> 
                                <img src="<?php echo get_bloginfo('template_directory') . '/images/google-share-icon.png'; ?>" alt="Recommend us on Google+" />
                            </a>
                        </div>
                    </div>
                </section>
                <section id="testimonials">
                    <div id="testimonial">
                        <?php show_testimonials(); ?>
                    </div>
                    <div id="mascot">
                        <img src="<?php echo bloginfo('template_directory') . '/images/mascot.png' ?>" alt="Shiverschool Mascot" />
                    </div>
                </section>
            </div>
            <?php wp_footer(); ?>
        </footer>
    </div>
</body>
</html>

这是完整的'functions.php'文件。

<?php
// Add JavaScript files
function add_scripts() {
    wp_enqueue_script('modernizr_script', get_template_directory_uri() . '/js/modernizr.js', __FILE__);
    wp_enqueue_script('global_functions_script', get_template_directory_uri() . '/js/global-functions.js', __FILE__);
}
add_action('wp_enqueue_scripts', 'add_scripts');

// Setup and load the theme options page and related code
require(get_template_directory() . '/inc/theme-options.php');

//Get theme options
$options = get_option('shv_theme_options');

// Add support for custom backgrounds
add_custom_background();

// Add support for post thumbnails and admin bar
add_theme_support('post-thumbnails');
add_theme_support('admin-bar');

// This theme uses wp_nav_menu() in two locations
    if (function_exists('register_nav_menus')) {
        register_nav_menus(array(
            'primary' => 'Primary Navigation',
            'secondary' => 'Secondary Navigation'
        ));
    }

/*
 * Register custom post types
 */

//Testimonials Post Type
class SHV_Testimonials_Post_Type {

    //Create construct function
    public function __construct() {
        $this->register_post_type();
        $this->metaboxes();
    }

    //Register post type
    public function register_post_type() {
        $args = array(
            'labels' => array(
                'name' => 'Testimonials',
                'singular_' => 'Testimonial',
                'add_new' => 'Add New Testimonial',
                'all_items' => 'All Testimonials',
                'add_new_item' => 'Add New Testimonial',
                'edit_item' => 'Edit Testimonial',
                'new_item' => 'New Testimonial',
                'view_item' => 'View Testimonial',
                'search_items' => 'Search Testimonials',
                'not_found' => 'No Testimonials Found',
                'not_found_in_trash' => 'No Testimonials Found in Trash'
            ),
            'query_var' => 'testimonials',
            'rewrite' => array(
                'slug' => 'testimonials',
            ),
            'public' => true,
            'supports' => array(
                'title'
            )
        );
        register_post_type('shv_testimonials', $args);
    }

    //Build metaboxes
    public function metaboxes() {

        //Add new meta boxes testimonial and testimonial author
        add_action('add_meta_boxes', 'add_testimonial_meta_box');
        add_action('add_meta_boxes', 'add_testimonial_author_meta_box');

        //Run add_meta_box functions
        function add_testimonial_meta_box() {
            add_meta_box('shv_testimonial', 'Testimonial', 'testimonial', 'shv_testimonials');
        }
        function add_testimonial_author_meta_box() {
            add_meta_box('shv_testimonial_author', 'Testimonial Author', 'testimonial_author', 'shv_testimonials');
        }

        //Create form elements and pull through any data associated with each meta box
        function testimonial($post) {
            $testimonial = get_post_meta($post->ID, 'shv_testimonial', true); ?>
            <p>
                <label for="shv_testimonial">Please enter your testimonial</label>
                <textarea class="large-text" name="shv_testimonial" id="shv_testimonial" cols="50" rows="5"><?php echo esc_attr($testimonial); ?></textarea>
            </p>
        <?php }

        function testimonial_author($post) {
            $author = get_post_meta($post->ID, 'shv_testimonial_author', true); ?>
            <p>
                <label for="shv_testimonial_author">Please enter a testimonial author name and school, e.g. John Doe, St Ambrose Barlow</label>
                <input type="text" class="widefat" name="shv_testimonial_author" id="shv_testimonial_author" value="<?php echo esc_attr($author); ?>" />
            </p>
        <?php }

        //Save meta box input
        add_action('save_post', 'save_meta_data');

        function save_meta_data($id) {

            if (isset($_POST['shv_testimonial'])) {
                update_post_meta(
                    $id,
                    'shv_testimonial',
                    strip_tags($_POST['shv_testimonial'])
                );
            }

            if (isset($_POST['shv_testimonial_author'])) {
                update_post_meta(
                    $id,
                    'shv_testimonial_author',
                    strip_tags($_POST['shv_testimonial_author'])
                );
            }
        }
    }
}

add_action('init', 'register_testimonials');

function register_testimonials() {
    new SHV_Testimonials_Post_Type();
}

function show_tweets() { ?>
    <img src="<?php echo get_bloginfo('template_directory') . '/images/twitter-bird-icon.png'; ?>" alt="Tweets" />
        <?php
            //Get theme options
            $options = get_option('shv_theme_options');
            include_once(ABSPATH . WPINC . '/feed.php');
            $username = $options['shv_twitter_username'];
            $rss = fetch_feed('https://api.twitter.com/1/statuses/user_timeline.rss?screen_name=' . $username);

            if (!is_wp_error( $rss ) ) : // Checks that the object is created correctly 
                // Figure out how many total items there are, but limit it to 5. 
                $maxitems = $rss->get_item_quantity(1); 

                // Build an array of all the items, starting with element 0 (first element).
                $rss_items = $rss->get_items(0, $maxitems); 
            endif;
        ?>
        <ul>
            <?php if ($maxitems == 0) echo '<li>No items.</li>';
            else
            // Loop through each feed item and display each item as a hyperlink.
            foreach ( $rss_items as $item ) : ?>
                <li>
                    <a href='<?php echo $item->get_permalink(); ?>'>
                        <?php echo $item->get_title(); ?>
                    </a>
                </li>
            <?php endforeach; ?>
        </ul>
<?php }

function show_testimonials() {
    $loop = new WP_Query(
        array(
        'post_type' => 'shv_testimonials',
        'order_by' => 'ID',
        )
    );

    if ($loop->have_posts()) {
        $output = '<ul class="testimonial-list">';

        while($loop->have_posts()) {
            $loop->the_post();
            $meta = get_post_meta(get_the_id(), '');
            print_r($meta);

            $output .= '
                <li>
                    <a href="' . get_permalink() . '">
                        ' . get_the_title() . ' | ' .
                        $meta['shv_testimonial_author'][0] . '
                    </a>
                </li>
            ';
        }
    }

    return $output;
}
?>

这是开发网站的链接,您可以在其中看到我使用'print_r($ meta);'输出正确的元数据。 http://dev.garethdaine.com/shiverschool

我希望有人能指出我正确的方向。提前干杯。

1 个答案:

答案 0 :(得分:1)

show_testimonials的输出不会在任何地方打印,请在您的模板中尝试echo show_testimonials(),或在echo $output;函数中使用return $output;代替show_testimonials