1

What I understand from the CODEX, (https://codex.wordpress.org/Class_Reference/WP_Query#Multiple_Loops).

then I'm trying to do a list of posts like this:

<nav id="cbp-hrmenu" class="cbp-hrmenu main-menu">
          <ul>
            <li>
              <a href="#">Secciones</a>
              <div class="cbp-hrsub">
                <div class="cbp-hrsub-inner">
                  <div>
                    <h4><a href="#" class="menu-section">Topic 1</a></h4>
                    <ul>
                      <?php echo get_posts_menus('topic-1','3'); ?>
                    </ul>
                    <h4><a href="#" class="menu-section">Topic 2</a></h4>
                    <ul>
                      <?php echo get_posts_menus('topic-2','3'); ?>
                    </ul>
                  </div>
                  <div>
                    <h4><a href="#" class="menu-section">Topic 3</a></h4>
                    <ul>
                      <?php echo get_posts_menus('topic-3','3'); ?>
                    </ul>
                    <h4><a href="#" class="menu-section">Topic 4</a></h4>
                    <ul>
                      <?php echo get_posts_menus('topic-4','3'); ?>
                    </ul>

                  </div>
</li>
</ul>
</nav>

If you notice , I'm calling a function named get_posts_menus() .

Where the function I'm using is:

if (!function_exists('get_posts_menus')):
  function get_posts_menus($category_name, $count)
{
  $permalink = get_the_permalink();
  $title     = get_the_title($the_menu->post->ID);
  $post_class = get_the_id();

  $args = array(
    'category_name' => $category_name,
    'posts_per_page' => $count
    );

  $the_menu = new WP_Query($args);

  while ($the_menu->have_posts()) {
    $the_menu->the_post();
    echo "<li class=\"post-id-$post_class\"><a href=\"$permalink\" alt=\"$title\">$title</a></li>";
  }
    wp_reset_postdata();

  }
  endif;

What happened here , is that it will give us back only the last post 3 times.

But the main idea is that this give back the 3 latest post of the category.

I'm very confused about this , that I'm start thinking of using JSON instead of the wp_query.

thank you

1 Answer 1

2

the permalink and post title won't be available until after you've run the query. the following should return the last 3 posts from the designated category.

the same post is being displayed 3 times because it doesn't look like you're setting the permalink or post title from within "the loop"

if (!function_exists('get_posts_menus')):
  function get_posts_menus($category_name, $count)
{

  $args = array(
    'category_name' => $category_name,
    'posts_per_page' => $count
    );

  $the_menu = new WP_Query($args);

  while ($the_menu->have_posts()) {
    $post_class = get_the_id();
    $permalink = get_the_permalink();
    $title     = get_the_title();
    $the_menu->the_post();
    echo "<li class=\"post-id-$post_class\"><a href=\"$permalink\" alt=\"$title\">$title</a></li>";
  }
    wp_reset_postdata();

  }
  endif;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.