0

WordPressサイトにカスタムの「いいね」ボタンを追加しようとしています。

「post_likes」という名前の「wp_posts」テーブルに列を追加しました。

私が苦労しているのは、このデータを標準のWordPressループの一部として出力する方法があるかどうかです。

DBから情報を取得するための私のコード:

function get_articles() {
    $articles = array();

    $query = mysql_query("SELECT `ID`, `post_title`, `post_likes` FROM `wp_posts`");
    while (($row = mysql_fetch_assoc($query)) !== false) {
    $articles[] = array(
        'ID' => $row['ID'],
        'post_title' => $row['post_title'],
        'post_likes' => $row['post_likes']
    );

    }   

    return $articles;
}

これを出力する現在のコード:

 <?php
$articles= get_articles();
foreach ($articles as $article) { echo '<p><a href="#">Like</a> <span>', $article['post_likes'], '</span> Like this</p>'; }
?>

ただし、これをループの一部として追加する方法があるかどうかを知る必要があるため、サムネイル画像を使用したり、カテゴリでフィルタリングしたりできます。

4

1 に答える 1

1

Okay, this is wrong in SO many ways. never alter WP tables.
Your post_likes should be a post_meta (check add_post_meta)

Database connection should be done with $wpdb

And displaying posts should be done in a Loop, don't need a DB connection to do this.
In which you could use get_post_meta to fetch your post_likes
And the like_post +1 them with update_post_meta

If you need an altered loop use WP_Query

why you should not alter WordPress tables

  1. All alternative 'columns' you need for posts (and users) should be stored in there *_meta tables. Take a look at those tables and you should be able to understand how it works.
  2. This way you can use all *_post_meta to add/edit/update/delete this data. No need to extra code functions (and make DB calls) yourself
  3. If you did made your own columns with functions, caching plugins won't optimize your performance for fetching those parts.
  4. if a WP update does something on the database it might delete your data
  5. why use Wordpress when you use your own flows and hack/change around WordPress.
于 2012-09-28T12:21:27.233 に答える