Jump to content

Retrieving posts by category on Wordpress Archive


Rory1984

Recommended Posts

Hi,

 

I am editing a Wordpress child theme. I have an existing page template which displays all documents from the post type 'publications'.

 

I simply want to display a single category (id=29) from these posts.

I have been given the following code but the posts are not displaying on the page -

 

 

<?php $posts_per_page_option = ot_get_option( 'wpl_publications_per_page' ); ?>
<?php $posts_per_page = ( !empty( $posts_per_page_option ) ? ot_get_option( 'wpl_publications_per_page' ) : 10 ); ?>
<?php $args = array(
'post_type' => 'post_publications',
'post_status' => 'publish',
'posts_per_page' => 5,
'cat' => 29,
'paged'=> $paged
); ?>

Any help much appreciated, thanks.
Link to comment
Share on other sites

You're not actually querying the database in the code you've posted. You can create a new WP_Query object, or use the get_posts() function, like so:

$posts = get_posts($args);

// or

$qry = new WP_Query($args);

var_dump $qry and you should see your posts.

Link to comment
Share on other sites

Sorry I should have posted the full code:

 

It displays all the posts if I remove 'cat' => 29, from the array but will not display the posts from a category.

 

Any more ideas?

 

Thanks

 

 

<?php $posts_per_page_option = ot_get_option( 'wpl_publications_per_page' ); ?>
				<?php $posts_per_page = ( !empty( $posts_per_page_option ) ? ot_get_option( 'wpl_publications_per_page' ) : 10 ); ?>
	<?php $args = array(
'post_type' => 'post_publications',
'post_status' => 'publish',
'posts_per_page' => 5,
'cat' => 29,
'paged'=> $paged
); ?>

				<?php $wp_query = null;
				$wp_query = new WP_Query( $args );

				if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post();?>
					<?php
						$publication_file = get_post_meta(get_the_ID(), 'wpl_publication_file', true);
						$publication_file_size = get_post_meta($post->ID, 'wpl_publication_file_size', true);
					?>
					<article class="list pub">
						<div class="short-content">
							<?php if ( has_post_thumbnail() ) { ?>
								<figure>
									<a title="<?php the_title(); ?>" href="<?php the_permalink(); ?>">
										<?php the_post_thumbnail('publications-thumb'); ?>
										<div class="mask radius">
											<div class="mask-square"><i class="icon-download"></i></div>
										</div>
									</a>
								</figure>
							<?php } ?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.