【问题标题】:Wordpress Loop w/ custom function returning more than once带有自定义函数的 Wordpress 循环多次返回
【发布时间】:2010-11-12 21:34:55
【问题描述】:

我在插件 Vote It Up for Wordpress 中使用了一个自定义函数,我发现 here. 但是由于某种原因,当我在主循环中使用该函数时,整个循环被返回了 3 次。

函数如下:

<?php
    function ShowPostByVotes() {
     global $wpdb, $voteiu_databasetable;

     mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) or die(mysql_error());
     mysql_select_db(DB_NAME) or die(mysql_error());
     //Set a limit to reduce time taken for script to run
     $upperlimit = get_option('voteiu_limit');
     if ($upperlimit == '') {
      $upperlimit = 100;
     }
     $lowerlimit = 0;

     $votesarray = array();
            $querystr = "
                SELECT *
                FROM $wpdb->posts
                WHERE post_status = 'publish'
                AND post_type = 'post'
                ORDER BY post_date DESC
             ";
            $pageposts = $wpdb->get_results($querystr, OBJECT);
     //Use wordpress posts table
     //For posts to be available for vote editing, they must be published posts.
     mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) or die(mysql_error());
     mysql_select_db(DB_NAME) or die(mysql_error());
     //Sorts by date instead of ID for more accurate representation
     $posttablecontents = mysql_query("SELECT ID FROM ".$wpdb->prefix."posts WHERE post_type = 'post' AND post_status = 'publish' ORDER BY post_date_gmt DESC LIMIT ".$lowerlimit.", ".$upperlimit."") or die(mysql_error());

     $returnarray = array();
     while ($row = mysql_fetch_array($posttablecontents)) {
      $post_id = $row['ID'];
      $vote_array = GetVotes($post_id, "array");
      array_push($votesarray, array(GetVotes($post_id)));
     }
     array_multisort($votesarray, SORT_DESC, $pageposts);
     $output = $pageposts;
     return $output;

    }
    ?>

index.php如下:

<?php get_header(); ?>

<?php if (have_posts()) : ?>

  <?php while (have_posts()) : the_post(); ?> <span>

<?php $pageposts = ShowPostByVotes(); ?>

 <?php if ($pageposts): ?>   <?php foreach ($pageposts as $post): ?>

        <div>
            <?php the_title(); ?>
            <?php the_content(); ?>
            <?php DisplayVotes(get_the_ID()); ?>
            <br /><br />

  </div>

<?php endforeach; ?>

</span>

 <?php endif; ?>

 <?php endwhile; endif; ?>

<?php get_sidebar(); ?>

<?php get_footer(); ?>

有人知道为什么会发生这种情况吗?

【问题讨论】:

    标签: wordpress plugins


    【解决方案1】:

    @Lee Simpson

    该代码是我认为我见过的用于 WordPress 的最痛苦的代码。为什么它如此糟糕是因为它完全绕过了 WordPress API,而且不是很好。

    如果你能描述投票数据的存储位置(你没有发布 GetVote() 函数,所以我不知道投票在哪里)我认为我们可以大大简化这种肮脏.

    -迈克

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-07
      • 2021-04-15
      • 2017-08-31
      • 1970-01-01
      • 1970-01-01
      • 2021-10-17
      相关资源
      最近更新 更多