WordPress.org

Forums

Showing posts without moving away from category archive (5 posts)

  1. zandercent
    Member
    Posted 6 years ago #

    Hi,

    I am currently displaying a menu of all posts within a category archive using the following

    ?php while (have_posts()) : the_post(); ?>
    <ul id="navlist">
    <li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>" ><?php the_title(); ?> </a></li>
    </ul>
    <?php endwhile; ?>

    The idea is to create a menu from the posts in a category. The problem is when you click on a post you move away from the archive, meaning the menu disappears. Is there any way to change what post is displaying WITHIN the archive, rather than navigating away to a single post... or for the new single page to show the menu list from the category id we have just navigated away from?

    does that make sense?!

    thanks!

    [moderated--bump removed. Please refrain from bumping as per Forum Rules]

  2. MichaelH
    Member
    Posted 6 years ago #

    or for the new single page to show the menu list from the category id we have just navigated away from?

    A possible plugin:
    http://wordpress.org/extend/plugins/related-links-by-category/

    Or something like:

    <?php
    if ( is_single() ) {
      $cats = wp_get_post_categories($post->ID);
        if ($cats) {
        $first_cat = $cats[0];
        $args=array(
          'cat' => $first_cat,
          'post__not_in' => array($post->ID),
          'showposts'=>5,
          'caller_get_posts'=>1
        );
        $my_query = new WP_Query($args);
        if( $my_query->have_posts() ) {
          echo 'Related Posts';
          while ($my_query->have_posts()) : $my_query->the_post(); ?>
            <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
           <?php
          endwhile;
        } //if ($my_query)
      } //if ($cats)
      wp_reset_query();  // Restore global post data stomped by the_post().
    } //if (is_single())
    ?>
  3. zandercent
    Member
    Posted 6 years ago #

    Hi

    Thanks for this - its incredibly close to what I'm after - unfortunately the related post function seems to call the newest category created - is there any way to make it show the last category viewed?

    thanks again!!

  4. notreallysure
    Member
    Posted 6 years ago #

    you need to add the order by descending as seen here, i believe

    http://codex.wordpress.org/Function_Reference/get_categories

  5. zandercent
    Member
    Posted 5 years ago #

    This is all working as I hoped it would now, I ended up hacking the database to re-order the categories, not ideal but its worked... so thanks for your help.

    Only one more question, how do I get MichaelH's code above to display the category name instead of 'Related Posts'.

    Thanks

Topic Closed

This topic has been closed to new replies.

About this Topic