窗口小部件插件中的自定义字段未显示

时间:2017-11-02 08:47:01

标签: php wordpress custom-fields

我创建了自定义帖子类型,然后我执行了以下操作: 在主页中我有一个正常的循环,我显示该帖子类型的内容,如自定义字段,缩略图等。

然后,通过functions.php我创建了一个小部件,允许我根据类别显示2个自定义帖子类型。 一切正常,但当我查看页面时,自定义字段不显示。

我使用的代码在我的第一个主页循环中起作用:

<?php echo get_post_meta($post->ID, 'game_offer', true); ?>

由于某些原因,我在窗口小部件的函数中的循环中不起作用。

此处是您可以看到自定义字段存在于后端的图像。

look here

在需要的情况下,函数中的代码如下所示:

class My_Widget extends WP_Widget {

    public function __construct() {
        $widget_ops = array( 
            'classname' => 'my_widget',
            'description' => 'Custom Post Type widget',
        );
        parent::__construct( 'my_widget', 'Cutom Post Type Widget', $widget_ops );
    }

    public function widget( $args, $instance ) {

        echo '<section id="custom_post_type_widget">';
        ?>
        <div class="top_bar">
            <h3 class="border-radius">Top Casino Offers</h3>
        </div>
        <?php
        $mypost = array( 
            'post_type' => 'game_reviews',
            'posts_per_page' => 2,
            'type'  => 'top-casino-offers',
        );

        $loop = new WP_Query( $mypost );
        while ( $loop->have_posts() ) : $loop->the_post();?>
        <div class="custom_post_widget_container flex outer">   
            <div class="left">
                <?php
                    $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); 
                    if ( has_post_thumbnail() ) {

                        echo '<div class="game_imag">';
                        echo the_post_thumbnail();
                        echo '</div>';
                    }  
                ?>
            </div>
            <div class="right">
                <div>
                    <div class="flex">
                        <div>
                            <p>
                                <span class="title"><?php the_title(); ?></span> | <span class="rating">
                                <?php
                                    $nb_stars = intval( get_post_meta( get_the_ID(), 'game_rating', true ) );
                                    for ( $star_counter = 1; $star_counter <= 5; $star_counter++ ) {
                                        if ( $star_counter <= $nb_stars ) {
                                            echo '<img src="'.get_template_directory_uri().'/images/icon.png"/>';
                                        } else {
                                            echo '<img src="'.get_template_directory_uri().'/images/grey.png"/>';
                                        }
                                    }
                                 ?>
                                </span>
                            </p>
                            <ul class="custom-field-list">
                                <li><?php echo get_post_meta($post->ID, 'game_offer', true); ?></li>
                            </ul>
                            <?php echo get_post_meta($post->ID, 'game_offer', true); ?>

                        </div>
                        <div class="right">
                            <ul class="play-reviews flex">
                                <li><a href="#" class="play_now">Play Now</a></li>
                                <li><a href="#" class="reviews">Reviews</a></li>
                            </ul>
                        </div>
                    </div>
                    <p><?php echo get_the_excerpt(); ?><a class="read-more" href="<?php the_permalink(); ?>">Read More</a></p>
                </div>

            </div> 
        </div> 
        <?php endwhile; ?>
    <?php wp_reset_query();

    echo '</section>';
    }
}

add_action( 'widgets_init', function(){
    register_widget( 'My_Widget' );
});

如何显示自定义字段?

2 个答案:

答案 0 :(得分:1)

使用query_posts而不是WP_Query

 <?php
class My_Widget extends WP_Widget {

    public function __construct() {
        $widget_ops = array( 
            'classname' => 'my_widget',
            'description' => 'Custom Post Type widget',
        );
        parent::__construct( 'my_widget', 'Cutom Post Type Widget', $widget_ops );
    }

    public function widget( $args, $instance ) {

        echo '<section id="custom_post_type_widget">';
        ?>
        <div class="top_bar">
            <h3 class="border-radius">Top Casino Offers</h3>
        </div>
        <?php
        global $post;
        $game_reviews_args=array(
                                        'post_type'   => 'game_reviews',
                                        'post_status' => 'publish',
                                        'showposts'   => 2,
                                        'tax_query' => array(
                                                array(
                                                    'taxonomy' => 'type', //taxonomy name
                                                    'terms' => 'top-casino-offers', //category name slug
                                                    'field' => 'slug'
                                                )
                                            ),
                                        'orderby'   => 'date',
                                        'order'   => 'DESC'
                                    );

        query_posts($game_reviews_args);
        if (have_posts()) : 
            while (have_posts()) : the_post();
                ?>
                <div class="custom_post_widget_container flex outer">   
                    <div class="left">
                        <?php
                            $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); 
                            if ( has_post_thumbnail() ) {

                                echo '<div class="game_imag">';
                                echo the_post_thumbnail();
                                echo '</div>';
                            }  
                        ?>
                    </div>
                    <div class="right">
                        <div>
                            <div class="flex">
                                <div>
                                    <p>
                                        <span class="title"><?php the_title(); ?></span> | <span class="rating">
                                        <?php
                                            $nb_stars = intval( get_post_meta( $post->ID, 'game_rating', true ) );
                                            for ( $star_counter = 1; $star_counter <= 5; $star_counter++ ) {
                                                if ( $star_counter <= $nb_stars ) {
                                                    echo '<img src="'.get_template_directory_uri().'/images/icon.png"/>';
                                                } else {
                                                    echo '<img src="'.get_template_directory_uri().'/images/grey.png"/>';
                                                }
                                            }
                                         ?>
                                        </span>
                                    </p>
                                    <ul class="custom-field-list">
                                        <li><?php echo get_post_meta($post->ID, 'game_offer', true); ?></li>
                                    </ul>
                                    <?php echo get_post_meta($post->ID, 'game_offer', true); ?>

                                </div>
                                <div class="right">
                                    <ul class="play-reviews flex">
                                        <li><a href="#" class="play_now">Play Now</a></li>
                                        <li><a href="#" class="reviews">Reviews</a></li>
                                    </ul>
                                </div>
                            </div>
                            <p><?php echo get_the_excerpt(); ?><a class="read-more" href="<?php the_permalink(); ?>">Read More</a></p>
                        </div>

                    </div> 
                </div>
                <?php                   
            endwhile; 
        endif;
        wp_reset_query();                       
    echo '</section>';
    }
}

add_action( 'widgets_init', function(){
    register_widget( 'My_Widget' );
});
?>

答案 1 :(得分:0)

所以我认为我需要的只是WP_Query实例之前的全局$ post变量:

global $post;