【问题标题】:How to show data from database in wordpress?如何在wordpress中显示数据库中的数据?
【发布时间】:2015-02-18 11:56:20
【问题描述】:

我正在尝试在 WordPress 中添加以下代码,但找不到正确的方法。请有人帮我...

<?php
include("config.php");
$results = $mysqli->query("SELECT COUNT(*) as t_records FROM wp_posts");
$total_records = $results->fetch_object();
$total_groups = ceil($total_records->t_records/$items_per_group);
$results->close(); 
?>

如果我在 WordPress 之外使用它,效果会非常好。但我不知道如何在我的 WordPress index.php 中实现这一点

我的主题中有很多代码,所以我不能给你索引页的确切代码。但这里是主要的 index.php 文件代码

<?php

$is_filterable_index = is_home() && x_get_option( 'x_ethos_filterable_index_enable', '' ) == '1';

?>

<?php get_header(); ?>

  <div class="x-container-fluid max width main">

    <?php x_get_view( 'ethos', '_post', 'slider' ); ?>

    <div class="offset cf">
      <div class="<?php x_main_content_class(); ?>" role="main">

        <?php if ( $is_filterable_index ) : ?>
          <?php x_get_view( 'ethos', '_index' ); ?>
        <?php else : ?>
          <?php x_get_view( 'global', '_index' ); ?>          
        <?php endif; ?>
<?php /*?>         
<?php */?>      </div>
      <?php get_sidebar(); ?>

    </div>       
  </div>  

<div id="content"></div>


<?php get_footer(); ?>

【问题讨论】:

标签: php database mysqli wordpress


【解决方案1】:

您应该使用 Wordpress 数据库连接。试试这个简单的代码并根据您的需要进行调整。

global $wpdb;
$query = "SELECT COUNT(*) as t_records FROM wp_posts";
$result = $wpdb->get_results($query);
var_dump($result);

http://codex.wordpress.org/Class_Reference/wpdb

【讨论】:

  • 我收到以下输出,您能帮助了解它的实际含义吗? array (size=1) 0 => object(stdClass)[3661] public 't_records' => string '1257' (length=4)
【解决方案2】:

WordPress 定义了一个名为wpdb 的类,其中包含一组用于与数据库交互的函数。

global $wpdb; //global connection object 
$results = $wpdb->get_var( "SELECT COUNT(*) as t_records FROM wp_posts" ); // The get_var function returns a single variable from the database
$total_groups = ceil($results->t_records/$items_per_group);

check

【讨论】:

    【解决方案3】:

    使用全局 Wordpress 数据库对象

    global $wpdb;
    
    $total_groups = $wpdb->get_var("SELECT COUNT(*) as t_records FROM wp_posts");
    

    【讨论】:

      【解决方案4】:

      正确的方法是使用 wordpress post meta。Post meta 正是为这种东西而构建的。所有验证和安全功能都由这些功能处理,因此您不必担心任何事情。您所要做的就是

      获取价值

      <?php $meta_values = get_post_meta( $post_id, $key, $single ); ?>
      

      创建/更新值

      <?php update_post_meta($post_id, $meta_key, $meta_value, $prev_value); ?>
      

      删除值

       <?php delete_post_meta($post_id, $meta_key, $meta_value); ?> 
      

      同样,您也为用户提供元值功能。查看法典以获取更多信息,

      【讨论】:

        【解决方案5】:

        使用$wpdb

        <?php
        global $wpdb;
        $total_records = $wpdb->get_var("SELECT COUNT(*) as t_records FROM $wpdb->posts");
        $total_groups = ceil($total_records/$items_per_group);
        ?>
        

        根据您要对结果执行的操作,有多种方法(例如,如果有几行或多列,则不会使用get_var

        我使用$wpdb-&gt;posts 而不是wp_posts 以防有人更改表前缀(例如在wp-config.php 中)。

        【讨论】:

        • 我收到此警告:除以零
        • 这可能意味着$items_per_group 未设置。但我也更新了ceil() 调用的第一部分——应该是$total_records,而不是$total_records-&gt;t_records
        猜你喜欢
        • 1970-01-01
        • 2013-09-20
        • 1970-01-01
        • 1970-01-01
        • 2016-12-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多