如何在小部件内的循环中获取帖子ID(父自定义类型)?

时间:2011-08-27 作者:janoChen

下面是一个小部件,里面有一个Wordpress循环。它按预期工作。唯一的问题是这部分:

\'post_parent\' => $post->ID,
小部件未获取post->ID 当前的父级的topic 自定义帖子类型(with是另一个自定义帖子类型,名为forum).

当我将循环放置在模板文件中时,它确实起作用。这是:

<?php $current_post_id = $post->ID; ?>

<h2><?php echo $current_post_id; ?></h2>
输出帖子id(论坛自定义帖子类型,它是主题自定义帖子类型的父级)。

/**
 * Most voted topics widget
 */
class MostVotedTopics extends WP_Widget {
    /** constructor */
    function MostVotedTopics() {
        parent::WP_Widget(false, $name = \'Most Voted Topics\');
    }

    /** @see WP_Widget::widget */
    function widget($args, $instance) {
        extract( $args );
        $title = apply_filters(\'widget_title\', $instance[\'title\']);
        ?>
          <?php echo $before_widget; ?>
              <?php if ( $title )
                    echo $before_title . $title . $after_title; ?>

                <?php // Display the top topics of current forum
                    $args = array(
                        \'post_type\' => \'topic\',
                        \'posts_per_page\' => \'3\',
                        \'post_parent\' => $post->ID,
                        \'gdsr_sort\' => \'thumbs\',
                        \'gdsr_ftvmin\' => \'1\',
                        \'gdsr_order\' => \'desc\',
                        \'order\' => \'DESC\'
                    );
                ?>

            <?php query_posts( $args ); ?>
                    <?php while ( have_posts() ) : the_post(); ?>

                        <div class="topic-wrapper">
                            <div class="topic-left">

                <?php $current_post_id = $post->ID; ?>

                <h2><?php echo $current_post_id; ?></h2>

                                <h2><a href="<?php bbp_topic_permalink(); ?>" title="<?php bbp_topic_title(); ?>"><?php bbp_topic_title(); ?></a></h2>
                                <span><?php printf( __( \'Started by: %1$s\', \'bbpress\' ), bbp_get_topic_author_link( array( \'size\' => \'14\' ) ) ); ?></span>

        <?php echo get_avatar(get_the_author_meta(\'user_email\'), 64); ?>

            <?php the_author_meta(\'nicename\'); ?>

        <p>Last modified on the: <?php the_modified_date(); ?>. by: <?php the_modified_author();?></p>
        <p>Time last modified: <?php the_modified_time(); ?></p>
        <?php echo time_ago(); ?>

                                <div class="topic-like-count">
                                    <h4><?php wp_gdsr_render_article_thumbs(); ?></h4>

                                    <?php preg_match( \'!<div class="thumblock ">(.*)</div>!si\' , wp_gdsr_render_article_thumbs(0, false, "", 0, "", false) , $n );
                                    $thumbs_number = strip_tags( $n[1] ); ?>

                                    <?php if ( $thumbs_number == 1 || $thumbs_number == -1 ) : ?>
                                        <span><?php _e( \'vote\' ); ?></span>
                                    <?php else : ?>
                                        <span><?php _e( \'votes\' ); ?></span>
                                    <?php endif; ?>
                                </div>
                                <div class="topic-reply-count">
                                    <h4><?php bbp_topic_reply_count(); ?></h4>
                                    <?php if ( bbp_get_topic_reply_count() == 1 ) : ?>
                                        <span><?php _e( \'reply\' ); ?></span>
                                    <?php else : ?>
                                        <span><?php _e( \'replies\' ); ?></span>
                                    <?php endif; ?>
                                </div>

                                <div class="topic-freshness">
                                    <h4><?php bbp_topic_freshness_link(); ?></h4>
                                        <span>
                                            <?php bbp_author_link( array( \'post_id\' => bbp_get_topic_last_active_id(), \'size\' => 14 ) ); ?>
                                        </span>
                                </div>

                <?php $most_voted[] = get_the_ID(); // get the id of the most voted post ?>
                            </div>
                    <?php endwhile; ?>
            <?php wp_reset_query(); ?>
            <?php print_r($most_voted); ?>

              <?php echo $after_widget; ?>
        <?php
    }

    /** @see WP_Widget::update */
    function update($new_instance, $old_instance) {
    $instance = $old_instance;
    $instance[\'title\'] = strip_tags($new_instance[\'title\']);
        return $instance;
    }

    /** @see WP_Widget::form */
    function form($instance) {
        $title = esc_attr($instance[\'title\']);
        ?>
            <p><label for="<?php echo $this->get_field_id(\'title\'); ?>"><?php _e(\'Title:\'); ?> <input class="widefat" id="<?php echo $this->get_field_id(\'title\'); ?>" name="<?php echo $this->get_field_name(\'title\'); ?>" type="text" value="<?php echo $title; ?>" /></label></p>
        <?php
    }

} // class FooWidget

// register FooWidget widget
add_action(\'widgets_init\', create_function(\'\', \'return register_widget("MostVotedTopics");\'));
有没有解决这个问题的建议?

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

不使用query_posts. 如中所述Codex:

Not for Secondary Loops 您不应该使用query\\u posts()创建辅助列表(例如,页面底部的相关帖子列表,或边栏小部件中的链接列表)。

您应该使用WP_Query 相反例如。

$args = array(
    \'post_type\' => \'topic\',
    \'posts_per_page\' => \'3\',
    \'post_parent\' => get_the_ID(),
    \'gdsr_sort\' => \'thumbs\',
    \'gdsr_ftvmin\' => \'1\',
    \'gdsr_order\' => \'desc\',
    \'order\' => \'DESC\'
);

$query = new WP_Query($args);


if($query->have_posts()) :
    while ( $query->have_posts() ) : $query->the_post();
        // do your stuff
    endwhile;
endif;

结束