duanmo6937 2017-11-02 08:47
浏览 52

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

I created a custom post type and then I did the following: In the homepage I have a normal loop which I display the content of that post type like custom fields,thumbnail etc.

Then, via functions.php I created a widget that allows me to show 2 custom post types based on category. All works fine, but when I view the page the custom field does not show.

I used this code which works in my first loop of the homepage:

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

For some reason that does not work in the loop that I have in the functions for the widget.

Here the image where you can see that the custom field exists in the backend.

enter image description here

My code in the function looks like this in case needed:

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' );
});

How can I show my custom field?

  • 写回答

2条回答 默认 最新

  • dppn67180 2017-11-02 09:16
    关注

    Use query_posts instead of 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' );
    });
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源