Support » How-To and Troubleshooting » [Resolved] Query question

[Resolved] Query question

  • I’m having a little trouble and any help is appreciated.

    I need to display the title and permalink of 1 post in a specific category if that category is true (or has a post in it) else, it displays nothing.

    this will be the first of two more queries.

    Thanks for any help or suggestions.

Viewing 8 replies - 1 through 8 (of 8 total)
  • This is how you get the title:
    <?php echo get_the_title($ID); ?>

    This is how you show get the title to show up:
    <?php wp_title(); ?>

    This is how you get the permalink :
    <?php $permalink = get_permalink( $id ); ?>

    And this is how you get the permalink to show up:
    <?php the_permalink(); ?>

    Thank you for your response, but I understand that part.

    I need to get one <h1><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h1> of a specific category if there is a post in that category. If not, I get nothing.

    Moderator Michael


    Forum Moderator

    assuming the specific category id is 73:

    <?php $cat_post = new WP_Query( array( 'posts_per_page' => 1, 'category__in' => array(73) ) );
    if( $cat_post->have_posts() ) : while( $cat_post->have_posts() ) : $cat_post->the_post(); ?>
      <h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
    <?php endwhile; endif; wp_reset_postdata(); ?>


    or alternatively you could use get_posts() with the same arguments;


    <?php $cat_post = get_posts( array( 'posts_per_page' => 1, 'category__in' => array(73) ) );
    if( $cat_post ) :
    foreach( $cat_post as $post ) {
      setup_postdata($post); ?>
      <h1><a href="<?php echo the_permalink(); ?>"><?php the_title(); ?></a></h1>
      <?php }
    endif; wp_reset_postdata(); ?>

    Thank you very much alchymyth. I will give that a try.

    For some reason my custom post type is not showing up. I am guessing I need to put post_type in an array?

    Here is what I got and I keep getting a syntax error. What am I missing.

    ` $cat_post = new WP_Query(array(
    ‘post_type’ => array(
    ‘showposts’ => ‘1’),
    ‘category__in’ => array(2)

    round 2, still same issue.

    $cat_post = new WP_Query( array(
    	'posts_per_page' => 1,
    	'category__in' => array(2))
    	'post_type' => array(

    For future reference.

    $cat_post = new WP_Query( array(
    	'posts_per_page' => 1,
    	'category__in' => array(2),
    	'post_type' => array(
Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘[Resolved] Query question’ is closed to new replies.