wp,wordpress直接sql取数据

直接sql取数据

require('./wp-blog-header.php');
global $wpdb;
$arr = $wpdb->query("UPDATE $wpdb->posts SET click = $click2 WHERE id = $post_id");

print_r($arr);

根据文章id取内容,取字段

$post_id = $_GET['id'];
$post_content = get_post($post_id)->post_content;
print_r($post_content );

 举个例子

<?php /* Template Name: 下载页面 */ ?>
<?php
/**
 * The template for displaying all pages.
 *
 * @package flatsome
 */

if($_GET['dongzuo'] == 'jia'){
    $post_id = $_GET['id'];
    $click = get_post($post_id)->click;

    $click2 = $click+1;

    require('./wp-blog-header.php');
    global $wpdb;
    $wpdb->query("UPDATE $wpdb->posts SET click = $click2 WHERE id = $post_id");
    // echo 'Update Success!';
    
    header('Location: '.$_GET['url']);
    die;
}

function trans_byte($byte){
    $KB = 1024;
    $MB = 1024 * $KB;
    $GB = 1024 * $MB;
    $TB = 1024 * $GB;
    if ($byte < $KB) {
        return $byte . "B";
    } elseif ($byte < $MB) {
        return round($byte / $KB, 2) . "KB";
    } elseif ($byte < $GB) {
        return round($byte / $MB, 2) . "MB";
    } elseif ($byte < $TB) {
        return round($byte / $GB, 2) . "GB";
    } else {
        return round($byte / $TB, 2) . "TB";
    }
}

//PHP stdClass Object转array  
function object_array($array) {  
    if(is_object($array)) {  
        $array = (array)$array;  
     } if(is_array($array)) {  
         foreach($array as $key=>$value) {  
             $array[$key] = object_array($value);  
             }  
     }  
     return $array;  
}

if(flatsome_option('pages_layout') != 'default') {
    
    // Get default template from theme options.
    echo get_template_part('page', flatsome_option('pages_layout'));
    return;

} else {

get_header();
do_action( 'flatsome_before_page' ); ?>

<style>
*{margin:0;padding:0;}
ul{list-style-type: none;}
.down_ul li{float: left;border-bottom:1px solid #eee;padding:30px 0;width:33%;}
.down_ul li>div{margin:0 0 0 30px;}
.clearfix:after{
    clear: both;
    content: ".";
    display: block;
    height: 0;
    visibility: hidden;
}

</style>
<div style="width:1050px;margin:0 auto;">

    <ul class="down_ul clearfix">
            <?php 
              $aa = query_posts('cat=285&showposts=99');
              if(have_posts()) : while (have_posts()) : the_post();
              // $content = get_the_content();
            $id = $post->ID;
            $args = array(
                'post_parent' => $id,
                'post_type' => 'attachment',
                'posts_per_page' => 1,
                'orderby' => 'menu_order',
                'order' => 'ASC',
            );
             
            $attachments = get_children($args);

            foreach($attachments as $k=>$v){
                $url = object_array($v)['guid'];
            }

            // print_r($url);

            $path = str_replace('https://www.sanrenmu.com.cn', '', $url);

            $path2 = realpath('.').$path;

            $ext = strtoupper(substr($url, strripos($url, '.')+1 ,strlen($url)));

            // print_r($path2);
            // print_r('...');
            // print_r(trans_byte(filesize($path2)));

        ?>
        <li>
            <div>
                <div style="width:280px;white-space:nowrap; text-overflow:ellipsis;overflow: hidden;">
                    <a style="font-size:18px;color:#333;"><?php the_title()?></a>
                </div>
                <div style="font-size:14px;color:#999;margin-top:10px;line-height:24px;">
                    格式:<?php echo($ext); ?><br />
                    大小:<?php echo(trans_byte(filesize($path2))); ?><br />
                    下载次数:<?php echo(get_post($id)->click);?><br />
                </div>
                <div style="margin-top:10px;">
                    <img src="http://www.sanrenmu.com.cn/wp-content/themes/flatsome/assets/img/download.png"  style="vertical-align:middle;" />
                    <a href="download/?dongzuo=jia&id=<?php echo($id); ?>&url=<?php echo($url); ?>" target="_blank" style="font-size: 14px;color:#f7ad00;">点击下载</a>
                </div>
            </div>
        </li>
        <?php
            endwhile;
            endif;
            wp_reset_query();
        ?>

    </ul>


<?php
do_action( 'flatsome_after_page' );
get_footer();

}

?>

猜你喜欢

转载自www.cnblogs.com/ximishuier/p/10554966.html