logo
Ask your WordPress questions! Pay money and get answers fast! (more info)

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.

$30
List items from a determined custom post type

We are tweaking a single-customposttype.php template aiming to create a sidebar menu where all items belonging the custom post type in question are listed with the actual (active) item being highlighted on the list (menu) with a special css class.

STRUCTURAL EXAMPLE

Custom Post Type = "Services"
User acesssing = "Service 2"

HTML code outputed by single-services.php (sidebar menu):


<ul class="post-type-items">
<li>Service 1</li>
<li class="current-item">Service 2</li>
<li>Service 3</li>
<li>Service 4</li>
</ul>



Looking forward for the code snippet that need to be placed on single-customposttype.php template.

Thanks in advance.

This question has been answered.

Liantas | 01/07/12 at 1:57pm Edit

Previous versions of this question: 01/07/12 at 2:00pm

(5) Possible Answers Submitted...

See a chronological view of answers?

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

  • avatar
    Last edited:
    01/07/12
    2:04pm
    Milan Petrovic says:

    WordPress wp_list_pages and get_pages functions can be used with post types:


    <ul class="post-type-items">
    <?php wp_list_pages(array("post_type" => "services", "title_li" => "")); ?>
    </ul>

    Previous versions of this answer: 01/07/12 at 2:04pm

    • 01/07/12 2:10pm

      Liantas says:

      What about active item highlighting?

      <li class="current-item">Service 2</li>


      Thanks

    • 01/07/12 2:12pm

      Milan Petrovic says:

      It should work fine, but the highlight class is: current_page_item. It can be modified, but would require a big walker php class changes, so better adjust your CSS styling to work with this CSS instead.

    • 01/07/12 2:52pm

      Liantas says:

      Not working Milan.

      Your code applicated to page single-services.php template:


      (...)
      <div id="content-containers" class="leftside">

      <?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>

      <div id="narrow-container" class="make-shadow global-radius">

      <div id="narrow-content" class="global-radius">

      <div id="sidebar">

      <ul class="post-type-items">
      <?php wp_list_pages(array("post_type" => "services", "title_li" => "")); ?>
      </ul>

      </div> <!-- /#sidebar -->

      </div><!-- /#narrow-content -->

      </div><!-- /#narrow-container -->

      <div id="withsidebar-container" class="make-shadow global-radius">

      <div id="withsidebar-content" class="global-radius">

      <div id="page-<?php the_ID(); ?>" <?php post_class('content'); ?>>

      <?php the_content(); wp_reset_postdata();?>

      <?php //if ( comments_open() && cloudfw_comments_open() ) {echo '<div class="sector"></div>' ;comments_template();} ?>

      </div><!-- /.content -->

      </div><!-- /#withsidebar-content -->

      </div><!-- /#withsidebar-container -->

      <div class="cf"></div>

      <?php endwhile; ?>
      (...)



      Thanks!

    • 01/07/12 3:04pm

      Milan Petrovic says:

      Stupid get_pages function used by wp_list_pages works only if the post type is hierarchical. You need to use some manual display code some other people suggested here.

  • avatar
    Last edited:
    01/07/12
    2:04pm
    Arnav Joy says:

    if you are creating sidebar then why do you want to create single-custom.php?

    • 01/07/12 2:12pm

      Arnav Joy says:

      you can use:-

      <?php
      query_posts('post_type=survices&showposts=-1');
      if(have_posts())
      while(have_posts()):the_post();
      ?>
      <li <?php if(get_the_ID() == $post->ID) { ?> class="current" <?php } ?>><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
      <?php endwhile;endif;?>

    • 01/07/12 2:13pm

      Liantas says:

      I'm not registering a new wordpress sidebar.

      This is a sidebar hardcoded on custom post type single template.

      Sorry for the misunderstanding and thanks for your attention.

    • 01/07/12 2:15pm

      Arnav Joy says:

      try this

      <ul class="post-type-items">
      <?php
      wp_reset_query();
      query_posts('post_type=survices&showposts=-1');
      if(have_posts()):
      while(have_posts()):the_post();
      ?>
      <li <?php if(get_the_ID() == $post->ID) { ?> class="current-item" <?php } ?>><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
      <?php endwhile;endif; wp_reset_query();?>
      </ul>

    • 01/07/12 2:17pm

      Arnav Joy says:

      try this as previous code was missplled with custom post type services

      try this this will work for you

      <ul class="post-type-items">
      <?php
      wp_reset_query();
      query_posts('post_type=Services&showposts=-1');
      if(have_posts()):
      while(have_posts()):the_post();
      ?>
      <li <?php if(get_the_ID() == $post->ID) { ?> class="current-item" <?php } ?>><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
      <?php endwhile;endif; wp_reset_query();?>
      </ul>

    • 01/07/12 2:53pm

      Liantas says:

      Arnav,

      Your last code is outputting "current item" class for all CPT items listed!

      Thanks

    • 01/07/12 2:54pm

      Arnav Joy says:

      did you test my code?

    • 01/07/12 3:00pm

      Liantas says:

      Yes Arnav... Like I said all items are being listed as "current-item". Do u know how to fix it?

    • 01/07/12 3:02pm

      Arnav Joy says:

      try this

      <ul class="post-type-items">
      <?php
      $id = get_the_ID();
      wp_reset_query();
      query_posts('post_type=Services&showposts=-1');
      if(have_posts()):
      while(have_posts()):the_post();
      ?>
      <li <?php if($id == $post->ID) { ?> class="current-item" <?php } ?>><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
      <?php endwhile;endif; wp_reset_query();?>
      </ul>

    • 01/07/12 3:09pm

      Arnav Joy says:

      this is your full fele

      <?php
      $id = get_the_ID();
      ?>

      <div id="content-containers" class="leftside">



      <?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>



      <div id="narrow-container" class="make-shadow global-radius">



      <div id="narrow-content" class="global-radius">



      <div id="sidebar">



      <ul class="post-type-items">
      <?php
      wp_reset_query();
      query_posts('post_type=Services&showposts=-1');
      if(have_posts()):
      while(have_posts()):the_post();
      ?>
      <li <?php if($id == $post->ID) { ?> class="current-item" <?php } ?>><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
      <?php endwhile;endif; wp_reset_query();?>
      </ul>



      </div> <!-- /#sidebar -->



      </div><!-- /#narrow-content -->



      </div><!-- /#narrow-container -->



      <div id="withsidebar-container" class="make-shadow global-radius">



      <div id="withsidebar-content" class="global-radius">



      <div id="page-<?php the_ID(); ?>" <?php post_class('content'); ?>>



      <?php the_content(); wp_reset_postdata();?>



      <?php //if ( comments_open() && cloudfw_comments_open() ) {echo '<div class="sector"></div>' ;comments_template();} ?>



      </div><!-- /.content -->



      </div><!-- /#withsidebar-content -->



      </div><!-- /#withsidebar-container -->



      <div class="cf"></div>



      <?php endwhile; ?>

      (...)

    • 01/07/12 3:10pm

      Arnav Joy says:

      if you can talk to me then ping me at

      skype: arnav.joy

      i will make that work for you

    • 01/07/12 3:11pm

      Liantas says:

      Same situation!
      All listed items still being marked as "current item".

    • 01/07/12 3:17pm

      Arnav Joy says:

      can you provide me access to your file I can do it.

    • 01/07/12 4:16pm

      Liantas says:

      Arnav solved the issue for us by Skype!
      Thanks

  • avatar
    Last edited:
    01/07/12
    2:12pm
    Manoj Raj says:

    your question is not clear at least to me. What do you want to list in the sidebar?

    posts of custom post type? or categories(custom taxonomy) of custom post type?

  • avatar
    Last edited:
    01/07/12
    2:28pm
    Reland Pigte says:


    <?php $custom_page = get_pages( array( 'post_type' => 'Services', 'sort_column' => 'post_title', 'sort_order' => 'ASC' ) ); ?>
    <?php global $post; ?>

    <ul class="post-type-items">
    <?php foreach( $custom_page as $pages ) : ?>
    <li <?php echo ($post->ID == $pages->ID)? 'class="current-item"':''; ?>><?php echo $pages->post_title; ?></li>
    <?php endforeach; ?>
    </ul>


    Please try the code above.

    Cheers,

    Previous versions of this answer: 01/07/12 at 2:22pm | 01/07/12 at 2:23pm | 01/07/12 at 2:28pm

    • 01/07/12 2:29pm

      Reland Pigte says:

      sure the code I provided above should work. :)

    • 01/07/12 3:15pm

      Liantas says:

      Hello Reland,

      Your code isn't working!

    • 01/07/12 3:52pm

      Reland Pigte says:

      Maybe you should try changing the post_type, I tested it on my local server 'post_type' => 'page' and it worked perfect like the output that you wanted.

      If you wish to let me do it to your site you can contact me on skype (relandpigte)

      cheers,

  • avatar
    Last edited:
    01/07/12
    2:17pm
    Francisco Javier Carazo Gil says:

    Hi Liantas,

    As Milan says, you can use wp_list_pages and later use a <script> with jQuery to change class depends on active service.


    <script>
    $('a[hreflang|="<?php echo get_permalink(); ?>"]').addClass("current-item");
    </script>

This question has expired.



Gabriel Reguly, Kannan C, Luis Abarca, Francisco Javier Carazo Gil, Liantas voted on this question.



Current status of this question: Completed



Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.