• Resolved Jay

    (@fectio1)


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

    Thread Starter Jay

    (@fectio1)

    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.

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

    https://codex.www.ads-software.com/Class_Reference/WP_Query

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

    https://codex.www.ads-software.com/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(); ?>
    Thread Starter Jay

    (@fectio1)

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

    Thread Starter Jay

    (@fectio1)

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

    Thread Starter Jay

    (@fectio1)

    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)
    );`

    Thread Starter Jay

    (@fectio1)

    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',
    	),
    	);

    Thread Starter Jay

    (@fectio1)

    For future reference.

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