Wordpress那点事

摘要:记路一些常见的问题

自定义产品字段

add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );
function woo_add_custom_general_fields() {
    global $woocommerce, $post;
    echo '<div class="options_group">';
 
woocommerce_wp_text_input( 
    array( 
        'id'          => '_pro_mainStoneIntro', 
        'label'       => __( 'Product size', 'woocommerce' ), 
        'placeholder' => 'Add product size',
        'desc_tip'    => 'true',//如果有了这一行,字段输入框后面会多出一个问号小图标,description的值会在鼠标移到小图标的时候显示
        'description' => __( 'If you have any questions, please contact the administrator.', 'woocommerce' ) 
    )
);
    //此处留待添加具体内容,见下文
 
    echo '</div>';
}
add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' );
 
function woo_add_custom_general_fields_save( $post_id ){
    $woocommerce_pro_mainStone = $_POST['_pro_mainStoneIntro'];
    if( !empty( $woocommerce_pro_mainStone ) )
        update_post_meta( $post_id, '_pro_mainStoneIntro', esc_attr( $woocommerce_pro_mainStone ) );
}

在前台展示自定义的产品字段

                    <?php echo get_post_meta( get_the_ID(), '_pro_mainStoneIntro', true );?>

在框架使用原生sql

<?php 
header("Content-Security-Policy: upgrade-insecure-requests");
header('Access-Control-Allow-Origin:*');
include_once('./wp-config.php');
    global $wpdb;
    function formatting($arr)
    {
        echo '<pre>';
        var_dump($arr);
    }

    $sql = 'SELECT *  FROM `wp_posts` where `post_type` = "post"';
    $result = $wpdb->get_results($sql,ARRAY_A);

formatting($result);
<?php 
$mysql_conf = array(
    'host'    => '127.0.0.1:3306', 
    'db'      => 'famen', 
    'db_user' => 'famen', 
    'db_pwd'  => 'sm3eMJ7ySc2riE6D', 
    );
 
$mysqli = @new mysqli($mysql_conf['host'], $mysql_conf['db_user'], $mysql_conf['db_pwd']);
if ($mysqli->connect_errno) {
    die("could not connect to the database:\n" . $mysqli->connect_error);//诊断连接错误
}
$mysqli->query("set names 'utf8';");//编码转化
$select_db = $mysqli->select_db($mysql_conf['db']);
if (!$select_db) {
    die("could not connect to the db:\n" .  $mysqli->error);
}
$sql = "select * from ps_companynews";  //查询user表
$res = $mysqli->query($sql);
if (!$res) {
    die("sql error:\n" . $mysqli->error);
}
$time = date('Y-m-d H:i:s');
while($row = $res->fetch_assoc()){
    $allData[] = $row;
}
$res->free();
    
    $test = $allData[0];
    $content = addslashes($test['remark']);
    $title   = $test['name'];
    $postname = rtrim($test['seo_url'],'.html');
    $newSql = "INSERT INTO `famen`.`wp_posts` (`ID`, `post_author`, `post_date`, `post_date_gmt`, `post_content`, `post_title`, `post_excerpt`, `post_status`, `comment_status`, `ping_status`, `post_password`, `post_name`, `to_ping`, `pinged`, `post_modified`, `post_modified_gmt`, `post_content_filtered`, `post_parent`, `guid`, `menu_order`, `post_type`, `post_mime_type`, `comment_count`) VALUES (NULL, '1', '$time', '$time', '$content', '$title', '', 'publish', 'open', 'open', '', '$postname', '', '', '$time', '$time', '', '0', 'urlup', '0', 'post', '', '0');";
$test = $mysqli->query($newSql);
echo '<pre>';
var_dump($test);
var_dump($newSql);
die;

$mysqli->close();
评论
  • 2019-01-25 10:19:59 by Eric Guo
    echo get_post_meta( $post->ID, '_pro_mainStoneIntro', true ); 用于在前台输出
  • 2019-01-24 13:26:03 by Eric Guo
    可以参考https://blog.brain1981.com/1050.html