• AGray410

    (@agray410)


    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!

Viewing 1 replies (of 1 total)
  • Thread Starter AGray410

    (@agray410)

    Ok I have found the solution (sort of)

    What I had to do was create a single.php file, and here is the code I added there.

    <?php
    /**
     * The template for displaying all single posts and attachments
     *
     */
    
    get_header(); ?>
    
     <!-- Start the Loop. -->
     <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    
     	<!-- Test if the current post is in category 3. -->
     	<!-- If it is, the div box is given the CSS class "post-cat-three". -->
     	<!-- Otherwise, the div box is given the CSS class "post". -->
    
     	<?php if ( in_category( '3' ) ) : ?>
     		<div class="post-cat-three">
     	<?php else : ?>
     		<div class="post">
     	<?php endif; ?>
    
     	<!-- Display the Title as a link to the Post's permalink. -->
    
     	<h2><a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
    
     	<!-- Display the date (November 16th, 2009 format) and a link to other posts by this posts author. -->
    
     	<small><?php the_time('F jS, Y'); ?> by <?php the_author_posts_link(); ?></small>
    
     	<!-- Display the Post's content in a div box. -->
    
     	<div class="entry">
     		<?php the_content(); ?>
     	</div>
    
     	<!-- Display a comma separated list of the Post's Categories. -->
    
     	<p class="postmetadata"><?php _e( 'Posted in' ); ?> <?php the_category( ', ' ); ?></p>
     	</div> <!-- closes the first div box -->
    
     	<!-- Stop The Loop (but note the "else:" - see next line). -->
    
     <?php endwhile; else : ?>
    
     	<!-- The very first "if" tested to see if there were any Posts to -->
     	<!-- display.  This "else" part tells what do if there weren't any. -->
     	<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
    
     	<!-- REALLY stop The Loop. -->
     <?php endif; ?>

    At first I had something different, and it was working but not displaying the post content. It did have some other things that this code doesnt have such as next/previous post. So I’ll just have to study the code and figure out where to add those functions or whatever.

    Now it seems as though I just have to style it. My thing is I keep searching and finding fixes but once I get something how i want it I kind of just forget about it. I guess studying the fundamentals will help with that.

Viewing 1 replies (of 1 total)
  • The topic ‘Display posts from certain categories on certain pages’ is closed to new replies.