• Resolved jonpfraser

    (@jonpfraser)


    Hi,

    I have come across this piece of code from this website.

    function my_home_category( $query ) { if ( $query->is_home() && $query->is_main_query() ) { $query->set( 'cat', '2'); } } add_action( 'pre_get_posts', 'my_home_category' );

    So how it works is my posts index page is called ‘work’, i also have a category for posts called ‘work’ with an id of ‘2’. The above code is placed into the functions.php file. It then only displays posts of category id ‘2’ on the posts index page. It works great, I have it running perfectly and its all fine.

    My question is if i create a second page called ‘blog’ and have a category called ‘blog’ setup with an id of ‘3’, could i manipulate this piece of code to only display category ‘3’ on the ‘blog’ page?

    Being fairly new to PHP and looking at the code the only reference I can see to the index page is $query->is_home().

    Anyway, any help would be greatly appreciated.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Digico Paris

    (@digico-paris)

    Again, you should create a custom page or post categoy, it’s tons more easier to do, than modifying it like you try.

    Custom post or pages are powerful, and you can child it too.

    And to answer your question, now you make a call similar to this

    <php?

    if is front_page (
    echo …;
    )

    ?>

    Hope it helps,

    Thread Starter jonpfraser

    (@jonpfraser)

    Thanks for your help Digico Paris.

    I’m not sure if i was clear enough in the first post, I have made a custom page called ‘blog’, which i want to display posts that have the category name ‘blog’.

    In reply to your comment, both a custom page (called blog) and a post category (again called blog) have been setup already.

    The code i posted works so I’d much rather use that if its possible. It needs to do pretty much the same thing but not on the index page.

    Thread Starter jonpfraser

    (@jonpfraser)

    Okay I figured this one out, for future reference I have added the steps taken. Seems so simple now…

    Before doing any of this make sure to create a child theme or your changes may be lost when the theme is updated by the author.

    Step 1: Under Appearance > Editor, create a new PHP file. call it what ever you want just remember to add ‘.php’ after the name you chose e.g. blog.php

    Step 2: Copy all the contents of index.php to this new file.

    Step 3: At the top of the file above all the other code add this snippet of code <?php /* Template Name: "Your chosen name" template */ ?>
    Give it any name you want.

    Step 4: In your new .php file, locate the while loop that contains (have_posts()) : the_post(); and above it copy the code below.
    <?php query_posts('category_name=your category name you want displayed'); ?>

    Step 5: Go to Pages > Add New, Give the page any name you wish. On the right side of the page you will see a dropdown labelled template, select your template name you created in Step 3 and publish the page.

    It should all be working now.

    I also went back and removed the function from my functions.php that was what this whole thread was about. It seems the same list of steps works for that too, just have to change the category name in the code snippet to whatever category you want to display.

    And thanks again to Digico Paris for your help.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Using PHP to display one category on page.’ is closed to new replies.