WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Display certain category posts on certain pages using 1 template part (8 posts)

  1. wpnovice241
    Member
    Posted 1 year ago #

    Hi Guys,

    I am new to this so bare with me haha!

    This is what I am trying to do:

    I have a site I am building for my brother, it is an Evil Dead website, he wants multiple pages for different things such as 1 for movies, 1 for games etc. He has said that he would like to show the latest post for each page at the top, I suggested using categories to organise the posts....here is my problem....i created a template part to call the latest post for a certain category (which works great!).

    What i am after now is using that same template part to call the other posts from categories if it matches the page name for example

    if your on the games post and it has posts, fetch the latest post from the games category and display it here!

    I am not sure how hard this is or how well I have explained myself but I hope I did well!!

    Thanks for your help Guys

    WPNovice

  2. esmi
    Forum Moderator
    Posted 1 year ago #

    if your on the games post and it has posts, fetch the latest post from the games category and display it here!

    Sorry - this part doesn't make any sense? are you talking about Posts, Pages or Categories?

  3. wpnovice241
    Member
    Posted 1 year ago #

    yeah i apologise for that, i re read it and it was disgusting. haha!

    For instance, if i have 2 categories and 2 pages (movies and games). I want to display the posts I make in the movies category on the movies and games posts on the games page.

    At the minute I have a template part which looks like this:

    <?php
        query_posts('posts_per_page=1&category_name=movies');
        if ( have_posts() ) : while ( have_posts() ) : the_post();
    ?>
    
    <div class="latest">
        <h3>Latest Post: <a>"><?php the_title(); ?></a></h3>
        <p><?php the_excerpt() ; ?></p>
        <a>">Read Full Article..</a>
    </div>
    
    <?php endwhile; endif; wp_reset_query(); ?>

    [Please post code or markup snippets between backticks or use the code button. As it stands, your posted code has been permanently damaged/corrupted by the forum's parser.]

    What I want to know is----

    Is there a way that I can add to the code above to tell wordpress that if i am on the movies page show the latest post from the movies category and if I am on the games page show the latest post from the games category.

    I dont want to have to write multiple templates or template parts as that seems to be more files on my server!

    I really do hope this helps to clear up the matter and I have explained it in a better way!

  4. esmi
    Forum Moderator
    Posted 1 year ago #

    Try:

    <?php
    if( is_page( 'movies' ) ) $cat_name = 'movies';
    elseif( is_page( 'games' ) ) $cat_name = 'games';
    if ( $cat_name ) :
    query_posts( 'posts_per_page=1&category_name=' . $cat_name );
    if ( have_posts() ) : while ( have_posts() ) : the_post();
    endif;?>
  5. wpnovice241
    Member
    Posted 1 year ago #

    Thanks, I am having trouble inserting it into my code and making it work.

    Sorry about this, I am fairly new to PHP in general and am a visual learner, it looks right but strikes up a syntax error of unexpected T_ENDIF error!

    My code again looks like this :

    <div class="column half">
    
    <?php query_posts('posts_per_page=1&category_name=movies'); if ( have_posts() ) : while ( have_posts() ) : the_post();?>
    
    		<div class="latest">
        		<h3>Latest Post: <a href="<?php the_permalink();?>"><?php the_title(); ?></a></h3>
        		<p><?php the_excerpt() ; ?></p>
        		<a href="<?php the_permalink();?>">Read Full Article..</a>
    		</div>
    
    <?php endwhile; endif; wp_reset_query(); ?>
    
    	</div>  <!-- Column Half -->

    Thank you and sorry for not being better haha!!

  6. esmi
    Forum Moderator
    Posted 1 year ago #

    <div class="column half">
    <?php
    if( is_page( 'movies' ) ) $cat_name = 'movies';
    elseif( is_page( 'games' ) ) $cat_name = 'games';
    if ( $cat_name ) :
    query_posts( 'posts_per_page=1&category_name=' . $cat_name );
    if ( have_posts() ) : while ( have_posts() ) : the_post();>
    
    		<div class="latest">
        		<h3>Latest Post: <a href="<?php the_permalink();?>"><?php the_title(); ?></a></h3>
        		<p><?php the_excerpt() ; ?></p>
        		<a href="<?php the_permalink();?>">Read Full Article..</a>
    		</div>
    
    <?php endwhile; endif; wp_reset_query(); endif;?>
    
    	</div>  <!-- Column Half -->
  7. wpnovice241
    Member
    Posted 1 year ago #

    You are a legend and I sincerely thank you for this.

    Now that I have seen it in action, I understand it and would be able to write it again, I am a very weird learner (as you can probably tell) and need to see things, then dissect them to understand how they work!!!

    Thank you again

  8. esmi
    Forum Moderator
    Posted 1 year ago #

    No problem. Glad I could help :-)

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags