• Resolved BStrong95

    (@bstrong95)


    Hi,

    I am trying to have some posts appear on a page other than the home page, based on the category ID of the posts (I currently have a feed automatically posting stories to my homepage – would like to divert some to another page).

    I read a WP post that said to insert this line of code: <?php query_posts(‘cat=-x’); ?> (where x=category #) before <?php if ( have_posts()… in the index.php file. However, when I did that, it did exclude the category posts specified, but some of my other pages did not load correctly. I’m not a developer, so I’m not sure what is wrong or how to fix it.

    Is this the best way to accomplish this, or is there a better way to go about this? If this is the way to do it, where should I put the code, or does it need to be changed at all?

    Any help would be greatly appreciated!

Viewing 14 replies - 1 through 14 (of 14 total)
  • I think it’s easier than that.

    Give each relevant post a specific Category or Tag, then go to Dashboard>Appearance>Menu, select Categories or Tags (left sidebar, Add to Menu and bingo!

    Thread Starter BStrong95

    (@bstrong95)

    Hi Dave,

    Can you elaborate a little on how the menu works? I did what you said, but my sports posts are still showing up on my main page. How is the menu supposed to differentiate between stories that go to the main page (news) versus stories that go on another page (sports)?

    Thanks!

    In the Codex, see the section Adding Items to a Menu. In the picture, you’ll see there’s a panel ‘Category’ and that’s what you need to use. You can setup 1 page per Category, or have different Pages filtered by a number of Categories.

    So you’d have Home set to all Categories excluding Sports.

    Thread Starter BStrong95

    (@bstrong95)

    Hi Dave,

    Thanks so much for that info. It’s starting to make some sense. I removed the sports posts from the menu and they stopped appearing on the home page (yeah!). Now I need to get them to appear on the Sports page. I noticed it said Customizr only supports 1 menu. Does that mean that I can’t have posts appear on more than one page? Or do I create another menu and link it to the Sports page and then also add the sports category? Would that work?

    Do you want a static home page?

    You now need to create a new blank Static Page called Sports. Then go through the Menu again and add a new item based on Category: Sports. Can all be done in the one menu, no need for any further menus.

    Thread Starter BStrong95

    (@bstrong95)

    Hi Dave,

    I tried doing what you said but I’m still finding sports posts appearing on my home page. Here is a screenshot of my menu setup: https://www.popfix.net/BA/menu-problem.jpg. Am I doing something wrong?

    Menu pic

    Forget my actual Categories, imagine you have something like Home-Blog-ESPN-Sky-Sports.

    So the Home entry will have all Categories except Sports ticked.
    The Sports entry will only have Sports ticked.

    Thread Starter BStrong95

    (@bstrong95)

    Hi Dave,

    Don’t mean to be obtuse here, but I just want to make sure I understand the process. So I select Home and add it to the menu. I then select other pages and categories and add them to the menu (reordering in the right hand column). I’ve tried both of these scenarios and neither worked:

    Scenario 1

    Home
    – News (Page)
    – News Feed 1 (Category)
    – News Feed 2 (Category)
    Sports (Page)
    – ESPN Feed (Category)

    Scenario 2

    Home
    – News (Page)
    – News Feed 1 (Category)
    – News Feed 2 (Category)
    – Sports (Page)
    – ESPN Feed (Category)

    Not sure what I’m doing wrong here

    Only thing I can see wrong is that Sports is also a Category which will filter all posts marked as Sports Category

    Thread Starter BStrong95

    (@bstrong95)

    Hi Dave,

    Thanks for all the help. Still no luck. I’m going to have to try and figure something else out.

    Thanks again.

    You can do this by setting links in the menu items, and linking to yoursite.com/category/cat1,cat2. That is, with each category separated by a comma. If the categories have spaces, it doesn’t seem to work. (Note to self: don’t put spaces in categories.)

    For example (with a random WordPress site found by searching “sportsfix”), here you can see the categories fashionfix AND sportsfix, together, but no other categories: https://hbcufix.wordpress.com/category/fashionfix,sportsfix/

    (With a very cheering juxtaposition of the “World’s most beautiful woman” followed by “How to avoid athlete’s foot”.)

    Thread Starter BStrong95

    (@bstrong95)

    Thanks for that info. I tried it an unfortunately it is still not working for me, for some reason (maybe my menu is broken?).

    I did discover another way around this. I found a post in the code snippets and added this code to my functions.php page in my child theme:

    \<?php
    \add_action(‘pre_get_posts’,’delete_category_home’);
    \
    \function delete_category_home($query) {
    \ $front_page_id = get_option(‘page_on_front’);
    \ if($front_page_id == $query->query_vars[‘page_id’]){
    \ $query->set( ‘cat’, ‘-98,-109’ );
    \ }
    \}

    That seems to have worked. The last line filters out the categories I want removed.

    Now I have to figure out how to have posts appear on a page that is not my home page. Do I have to create a new custom template for this?

    Would appreciate any advice. Thanks!

    Do I have to create a new custom template for this?

    Yes. See here.

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘How to post to page (other than home) based on category’ is closed to new replies.