Display posts from certain categories on certain pages
-
Hello!
I am developing my site on localhost XAMPP and my goal is to have two different pages that display recent posts from two different categories.
I want to have a ‘Beat Making Videos’ page on my site that displays posts I’ve categorized into ‘Beat Making Videos’
I want to have a ‘Producer Interviews’ page that displays posts categorized into ‘Producer Interviews’
I know that I need to do something with my functions file, and the template heirarchy will chose a template based off the name, I’m just confused.
I followed a few tutorials and I couldn’t get anything to work right.
Here’s what I tried to do:
function wpb_postsbycategory() { // the query $the_query = new WP_Query( array( 'category_name' => 'beatmaking', 'posts_per_page' => 15 ) ); // The Loop if ( $the_query->have_posts() ) { $string .= '<ul class="postsbycategory widget_recent_entries">'; while ( $the_query->have_posts() ) { $the_query->the_post(); if ( has_post_thumbnail() ) { $string .= '<li>'; $string .= '<a href="' . get_the_permalink() .'" rel="bookmark">' . get_the_post_thumbnail($post_id, array( 50, 50) ) . get_the_title() .'</a></li>'; } else { // if no featured image is found $string .= '<li><a href="' . get_the_permalink() .'" rel="bookmark">' . get_the_title() .'</a></li>'; } } } else { // no posts found } $string .= '</ul>'; return $string; /* Restore original Post Data */ wp_reset_postdata(); } // Add a shortcode add_shortcode('beatmakingposts', 'wpb_postsbycategory'); // Enable shortcodes in text widgets add_filter('widget_text', 'do_shortcode');
^^^ Added that to my functions.php
Created a page template called beat-making page (beat-making-videos.php)
Added the page in wordpress admin section and set the template to beat-making page.
Created a post and categorized it ‘Beat Making Videos’
I tried to add the shortcode [beatmakingposts] in the text area and I also tried adding <?php wpb_postsbycategory() ?> to the .php file of the page.
No posts are showing but it is different than my index file.
These posts will contain videos (each post) so it would be nice to set it up like *Thumbnail of Video* *Brief Description* *View/Read More link*
where the link takes you to the post page and the video is loaded there (trying to save load time)
I hope this makes sense! I want to be at the point where I can develop sites for clients one day and this is my learning journey!
- The topic ‘Display posts from certain categories on certain pages’ is closed to new replies.