如何在wordpress中显示数据库中的数据? [英] How to show data from database in wordpress?

查看:978
本文介绍了如何在wordpress中显示数据库中的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在WordPress中添加以下代码,但我找不到正确的方法来做。请帮助某人...

I am trying to add following code in WordPress, but I cant find the right way to do it. Please help me someone...

<?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实现这个

It's work great nice if I use it outside of WordPress. But I don't know hot to implement this in my WordPress index.php

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

There is a lot of code in my theme so I can't give you exact code of the index page. But here is the main index.php file code

<?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(); ?>


推荐答案

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

You should use the Wordpress db connection. Try this simple code and adapt it for your needs.

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

这篇关于如何在wordpress中显示数据库中的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆