Macからワードプレスへ特定のフォルダの画像を自動追加し表示する方法

NO IMAGE

Macの特定のフォルダに画像を追加するだけで、WordPressのメディアライブラリへ自動でアップロードする方法をご紹介します。この方法は、Macの標準機能であるAutomatorと、WordPress CLIを組み合わせて実現します。

Google PhotoやAmazon Photosといった外部サービスとの有料連携は不要です。2025年8月現在、この方法が最もシンプルで効果的です。


手順1: function.phpにショートコードを追加する

メディアライブラリに自動で追加された画像を、固定ページに表示するためのショートコードを設置します。テーマの「**functions.php**」ファイルに、以下のコードを追記してください。

<?php// --- ここから追加するコードです ---// メディアライブラリの最新画像を自動表示するショートコードを定義(タイトル付き)function display_latest_images_from_media_library_with_title($atts) {  $atts = shortcode_atts( array(  'count' => 10,  'size'  => 'medium', ), $atts  );  $args = array( 'post_type'=> 'attachment', 'post_mime_type' => 'image', 'post_status' => 'inherit', 'posts_per_page' => (int)$atts['count'], 'orderby'  => 'date', 'order' => 'DESC'  );  $images = new WP_Query($args);  $output = '';  if ($images->have_posts()) { $output .= '<div class="latest-images-gallery-fullwidth">'; while ($images->have_posts()) {$images->the_post();$image_url = wp_get_attachment_image_url(get_the_ID(), $atts['size']);$image_alt = get_post_meta(get_the_ID(), '_wp_attachment_image_alt', true);$image_title = get_the_title();$output .= '<figure class="image-with-title">';$output .= '<img src="' . esc_url($image_url) . '" alt="' . esc_attr($image_alt) . '" />';$output .= '<figcaption class="image-title">' . esc_html($image_title) . '</figcaption>';$output .= '</figure>'; } $output .= '</div>'; wp_reset_postdata();  }  return $output;}add_shortcode('latest_images_fullwidth', 'display_latest_images_from_media_library_with_title');

※ コードの合体方法が不明な場合は、AIアシスタントに「**functions.phpに記載されているコードと合体させて**」と指示すれば、自動で修正してくれます。


手順2: CSSコードを追加する

次に、画像のレイアウトを調整するCSSコードを、WordPressの「追加CSS」に貼り付けます。

.latest-images-gallery-fullwidth { display: flex; flex-wrap: wrap; gap: 15px; }.latest-images-gallery-fullwidth .image-with-title { width: calc(33.333% - 10px);  margin-bottom: 20px; }.latest-images-gallery-fullwidth .image-with-title img { width: 100%; height: auto; display: block; border: 1px solid #ddd; border-radius: 5px;}.latest-images-gallery-fullwidth .image-with-title .image-title { text-align: center; margin-top: 5px; font-size: 0.9em; color: #555;}@media (max-width: 768px) { .latest-images-gallery-fullwidth .image-with-title {  width: calc(50% - 10px);  }}@media (max-width: 480px) { .latest-images-gallery-fullwidth .image-with-title {  width: 100%;  }}

手順3: 固定ページにショートコードを挿入する

画像を自動表示したいページを編集し、以下のショートコードを貼り付けます。

[latest_images_fullwidth]

表示枚数や画像サイズを変更したい場合は、以下のように引数を指定してください。

[latest_images_fullwidth count="5" size="large"]

これで、メディアライブラリの最新画像が自動で横並びに表示されます。

次のステップ: Automatorの設定へ

ここまでの作業が完了したら、MacのAutomatorとWordPress CLIを使って、実際の自動アップロード設定に進みます。

AIアシスタントに「**Macの特定のフォルダの画像をWordPressのメディアライブラリへ自動追加するアイデアありますか**」と質問すれば、詳しい手順を教えてくれます。もし回答が分かりづらい場合は、「**もう少しわかりやすく手順を説明して**」と追加で質問することで、より理解しやすい回答が得られます。

AIツールを使いこなすことで、作業はどんどん効率化されます。日々進化する技術を柔軟に取り入れ、あなたに最適な方法を見つけていきましょう。