Grow your CSS skills. Land your dream job.

Display limited number of Custom Post Types titles in sidebar.

  • # July 26, 2011 at 4:44 am

    Hi there

    I have a couple of different custom post types in my site – for job vacancies and tenders respectively. I need to display just the titles of of each post in a particular post type in the the sidebar.

    I have done so by using the following:


    < ?php $sidebar_vacancies_loop = new WP_Query( array( 'post_type' => 'vacancies', 'posts_per_page' => 3 ) ); ?>

    Vacancies


    /vacancies/">View all job openings




      < ?php while ( $sidebar_vacancies_loop->have_posts() ) : $sidebar_vacancies_loop->the_post(); ?>
    • ">< ?php the_title(); ?>

    • < ?php endwhile; ?>

    This works well, but I need to display alternate content if there are no vacancies.

    • Either by having an alternate li that simply says, ‘No vacancies’
    • Or just not displaying the entire section if there are none.

    Any help would be appreciated.

    Thanks in advance.

    # July 27, 2011 at 2:40 am

    How about:


    < ?php if($sidebar_vacancies_loop->have_posts()):?>
    < ?php while ( $sidebar_vacancies_loop->have_posts() ) : $sidebar_vacancies_loop->the_post(); ?>
  • ">< ?php the_title(); ?>

  • < ?php endwhile; ?>
    < ?php else:?>
  • No vacancies

  • < ?php endif;?>
    # September 7, 2011 at 2:41 am

    I got this working eventually. Pretty similar to the above, so thanks ddliu.

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".