WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Query question (9 posts)

  1. Jay
    Member
    Posted 1 year ago #

    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.

  2. gholem
    Member
    Posted 1 year ago #

    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(); ?>

  3. Jay
    Member
    Posted 1 year ago #

    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.

  4. alchymyth
    Forum Moderator
    Posted 1 year ago #

    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(); ?>

    http://codex.wordpress.org/Class_Reference/WP_Query

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

    http://codex.wordpress.org/Template_Tags/get_posts

    <?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(); ?>
  5. Jay
    Member
    Posted 1 year ago #

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

  6. Jay
    Member
    Posted 1 year ago #

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

  7. Jay
    Member
    Posted 1 year ago #

    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(
    'post',
    'custom1',
    'custom2',
    ),
    'showposts' => '1'),
    'category__in' => array(2)
    );`

  8. Jay
    Member
    Posted 1 year ago #

    round 2, still same issue.

    $cat_post = new WP_Query( array(
    	'posts_per_page' => 1,
    	'category__in' => array(2))
    	'post_type' => array(
    	'post',
    	'custom1',
    	'custom2',
    	),
    	);
  9. Jay
    Member
    Posted 1 year ago #

    For future reference.

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

Topic Closed

This topic has been closed to new replies.

About this Topic