0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

簡単にMODXからWordpressの投稿を表示する

Last updated at Posted at 2022-02-14

(取り急ぎ)MODXでWordpressの投稿を表示する方法です。
普通にWordpressをインストールした状態です。

バージョン
MODX:1.0.25J
Wordpress:5.9

以下をスニペット「wp_init」として保存する。
なおrequire_onceの引数をwp-load.phpへのパスに修正。

wp_init
require_once('/wordpressインストール先/wp-load.php');

以下をスニペット「wp_posts」として保存する。

wp_posts

if (is_null($params["tpl"])) {
    return "tpl is null.";
}
if (is_null($params["catTpl"])) {
    return "catTpl is null.";
}

$p_cat = $params["cat"] ? $params["cat"] : "";
$args = [
    "posts_per_page" => 10,
    "offset" => 0,
    "category" => $p_cat,
    "category_name" => "",
    "orderby" => "date",
    "order" => "DESC",
    "include" => "",
    "exclude" => "",
    "meta_key" => "",
    "meta_value" => "",
    "post_type" => "post",
    "post_mime_type" => "",
    "post_parent" => "",
    "author" => "",
    //    'post_status'      => 'public',
    //    'suppress_filters' => false
];
$result = "";
$posts = get_posts($args);

global $post;

foreach ($posts as $post) {
    setup_postdata($post);
    $cats = "";
    $category = get_the_category();
    foreach ($category as $cat) {
        $cats .= $modx->parseChunk(
            $params["catTpl"],
            [
                "link" => get_category_link($cat->term_id),
                "slug" => esc_attr($cat->slug),
                "name" => esc_html($cat->name),
            ],
            "[+",
            "+]"
        );
    }
    $result .= $modx->parseChunk(
        $params["tpl"],
        [
            "cats" => $cats,
            "date" => get_the_time("Y年m月d日"),
            "title" =>get_the_title()
        ],
        "[+",
        "+]"
    );
}

wp_reset_postdata();

return $result;

以下をチャンク「post_tpl」「post_cat_tpl」としてをそれぞれ作成

post_tpl
[+cats+]
[+date+]
[+title+]
post_cat_tpl
[+link+]
[+slug+]
[+name+]

Wordpressの記事を表示したいページに以下を記述。

[[wp_init]]
[[wp_posts? &tpl="post_tpl" &catTpl="post_cat_tpl"]]

完了。

ニーズにあわせてそれぞれ修正して使えるはず。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?