• このサイトのトップに子サイトのブログの記事を反映させたいと思っております。

    そこでカテゴリーの「お知らせ」のみ5件までを表示させたいのですが、なかなかうまくいきません。

    どうやったら表示させることが出来るのかどなたか分かる方にご教授いただきたいと思っております。

    現在function.phpでショートコードを生成し、トップに反映させている状態です。

    • function.php
    //function.php---------------------//
    //NEWSの設定
    function dn_feed()
    {
      ob_start();
      $args = [
        'cat' => 'news',
        'num' => 5, //新着記事数
        'url' => 'https://ksstyle62.com/blog1/', //取得したいURL(※スラッシュまでいれる!)
      ];
      get_template_part('dn_feed', null, $args);
      return ob_get_clean();
    }
    add_shortcode('feed', 'dn_feed');
    • ショートコードの内容<dn_feed.php>
    
    //dn_feed.php
    <?php
    $num = $args['num'];
    $url = $args['url'] . 'feed/';
    
    include_once ABSPATH . WPINC . '/feed.php';
    $feed = fetch_feed($url);
    if (is_wp_error($feed)) {
      $max = 0;
    } else {
      $max = $feed->get_item_quantity($num);
      $items = $feed->get_items(0, $max);
    }
    ?>
    
    
    <?php if ($max) : ?>
      <!-- SWELL(サムネ無し)の場合 -->
      <ul class="p-postList -type-simple" style="margin: 20px">
        <!-- <p style="font-weight:700 font-family:'Noto Sans JP', 'sans-serif';">New Topics</p> -->
    
    
        <?php
        foreach ($items as $item) : ?>
    
          <li class="p-postList__item">
            <a href="<?php echo $item->get_permalink(); ?>" class="p-postList__link">
              <div class="p-postList__body" bis_skin_checked="1" style="margin: 10px;">
                <div class="p-postList__meta" bis_skin_checked="1" style=" display: flex; list-style: none;">
                  <div class="p-postList__times c-postTimes u-thin" bis_skin_checked="1">
                    <time class="c-postTimes__posted icon-posted"><?php echo $item->get_date('Y.m.d'); ?></time>
                  </div>
                  <span class="p-postList__cat u-thin icon-folder"><?php echo $item->get_category()->get_label(); ?></span>
                </div>
                <h3 class="p-postList__title" style="font-family: 'Noto Sans JP', sans-serif; font-weight: normal;   margin: 10px 0;
      font-size: 1rem; margin: 0; line-height: normal;">
                  <?php echo mb_strimwidth($item->get_title(), 0, 90, '…'); ?>
                </h3>
              </div>
            </a>
          </li>
        <?php endforeach; ?>
      </ul>
    <?php else : ?>
    <?php endif; ?>
    
    
    <?php
    $arg = array(
      'posts_per_page' => 4, // 表示する件数
      'orderby' => 'date', // 日付でソート
      'order' => 'DESC', // DESCで最新から表示、ASCで最古から表示
      'category_name' => 'wp-customize' // 表示したいカテゴリーのスラッグを指定
    );
    $posts = get_posts($arg);
    if ($posts) : ?>
      <ul>
        <?php
        foreach ($posts as $post) :
          setup_postdata($post); ?>
          <li><?php the_time('Y.m.d'); ?><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
        <?php endforeach; ?>
      </ul>
    <?php
    endif;
    wp_reset_postdata();
    ?>
1件の返信を表示中 - 1 - 1件目 (全1件中)
  • mackeeweb さん、こんにちは。

    そこでカテゴリーの「お知らせ」のみ5件までを表示させたいのですが、なかなかうまくいきません。

    「うまくいきません」では、どのような状況なのかが伝わりませんので具体的に発生している状況を記載していただけないでしょうか。 

    何かエラーが発生するのでしょうか? エラーが発生している場合は、エラーメッセージを質問に記載していただけないでしょうか。

    親サイトと子サイトの関係ですが、1つの WordPress でマルチサイトで構築されているのでしょうか。 複数の WordPress 等の構成でしょうか。

    例えば、1つのマルチサイト内であれば 他の子サイトを switch_to_blog で切り替えて get_posts で取得できると思います。

    マルチサイトでない場合は、サイト構成により異なると思いますが、ご自身で管理権限を持っている WordPress 同士であれば、 WP REST API を使うのが簡単かもしれません。

    • この返信は2年、 2ヶ月前にKOJIが編集しました。
1件の返信を表示中 - 1 - 1件目 (全1件中)
  • トピック「親サイトに子サイトのブログ(特定カテゴリー)表示」には新たに返信することはできません。