0

enter image description here

Hi. I don't know ajax programming. I know HTML, CSS, little bit of php(so much that I am editing and customizing all the default themes, and learning along the way).

I have attached the UI of the page which my designer has designed. Now I am making this in wordpress. I have assigned the left menu as the category menu, so it has categories of all the posts.

I can create different templates for trending posts and fresh posts. So basically I will have then 3 templates for the category page (default, trending and fresh)

What I would like to know is how to display a separate menu for trending, fresh and default. Eg if the user is on default page (home page), then the category menu on the left will use the default template, when he is on the trending page, the category menu on the left will use the trending template and so on. Is there a simple way to do this ? Can I do this with ajax too ?

The theme is twenty fourteen child ..

4
  • Simple way? I can't think of any as you'll need to modify the query. I guess those up and down arrow values are custom field, therefor you should use meta_query and order parameters when showing Tending posts.. And yes, you can do it using AJAX too, that will improve the UX by a mile.
    – Abhik
    Sep 25, 2015 at 20:07
  • Ok . I will try to research on it. Though I did not get what you mean exactly .. I am rephrasing the question in the last paragraph, please have a look. Thanks.
    – A Kashyap
    Sep 25, 2015 at 20:21
  • Not sure what you mean by "default' and 'fresh'. By default, WordPress will display newest first (i.e fresh) for category templates unless you put a filter to modify it. Well, you can choose to have different templates for different category (Have a look at the Template Hierarchy), but not two templates for a particular category (if that's what you are thinking of). You'll need to modify the query, and doing that using AJAX is much more simpler that other methods.
    – Abhik
    Sep 25, 2015 at 20:29
  • OK yeah i understand default and fresh are same. I wanted two templates for a particular category. Or now as MSTannu said below and like you said that a particular category cannot have two templates so I will approach the problem from another way. So maybe the category template could use an if statement if ispage and according to the page it will display that part of the template ??
    – A Kashyap
    Sep 25, 2015 at 21:33

2 Answers 2

0

If you have 3 templates, you can use the conditional check is_page_template to check which one is currently in use.

See https://developer.wordpress.org/reference/functions/is_page_template/

Or if you weren't talking about page templates, but rather a specific archive template, you can get the current term id with get_queried_object_id.

See https://developer.wordpress.org/reference/functions/get_queried_object_id/

Then, you could register 3 different menu locations and make an if statement to choose between them when outputting the menu in the header file.

15
  • No not two templates I am planning to change the strategy as Abhik pointed out above. So I am thinking of using the if ispage statement in the category template and apply 2 different loops, one for fresh or latest posts and another for trending posts. Would that work fine ? as I think then according to ispage then it would display that part of the template if a page is found ..
    – A Kashyap
    Sep 25, 2015 at 21:36
  • is_page is for pages. If you look at the template hierarchy - developer.wordpress.org/files/2014/10/template-hierarchy.png - you can see multiple category templates do exist. Either by slug or id, for instance category-fresh.php. You would still need to check which category you are in when outputting the menu in header, since it's shared between all templates.
    – MSTannu
    Sep 25, 2015 at 21:44
  • OK . thank you for the advice, I think I am headed to the solution. What I did was install the is_page plugin , then set 2 custom conditions, one for trending and one for fresh, the trending returns true if page is one of the trending pages and the same goes for fresh. I then created 2 custom menus now I am adding template pages.
    – A Kashyap
    Sep 26, 2015 at 1:07
  • OK . thanks . what I ended up doing was create 2 pages, one for trending and one for fresh, the fresh page does not have any child pages, and the trending page has child pages of all the categories which will have fresh template. The normal category menu was good for the fresh pages.
    – A Kashyap
    Sep 26, 2015 at 11:50
  • Hi . I am stuck a little bit .. I managed to create two menus and select which one to display based on the page the user is on using if menu plugin and the condition is_page .. What I couldn't do was create two separate templates for both the menus . The first menu is just a category menu .. the second menu I am confused as I have just assigned pages to it till now.. Help
    – A Kashyap
    Sep 27, 2015 at 15:28
0

OK . So by following advice from MSTannu, what I did was

  • I registered two menus with two theme locations in functions.php using the code below and then added individual pages in both the menus using appearance->menus from the admin section.

    function register_my_menus() {
        register_nav_menus(
            array(
              'trending-menu' => __( 'Trending Menu' ),
              'fresh-menu' => __( 'Fresh Menu' )
            )
        );
    }
    
  • Then I declared in functions.php to find whether page is a descendent and made an is_tree function following some advice online. The code is below.

    // GET PAGE ID FROM THE SLUG, HELPER FUNCTION FOR IS_TREE
    function get_ID_by_slug($page_slug) {
            $page = get_page_by_path($page_slug);
            if ($page) {
                return $page->ID;
            } else {
                return null;
            }
    }
    
    // DETERMINE IF A PAGE IS IN A DESCENDANT TREE
    function is_tree($pid) {      // $pid = The ID of the page we're looking for pages underneath
            global $post;         // load details about this page
            $pid = get_ID_by_slug($pid);
            if( is_page() && ($post->post_parent==$pid || is_page($pid) ) )
                       return true;   // we're at the page or at a sub page
            else
                       return false;  // we're elsewhere
    };
    
  • Lastly I added the two menus in sidebar.php and then checked it conditionally using if and else statement with is_tree and the parent pages trending and fresh . The code is below.

    <?php if ( is_tree('trending') ) { 
         wp_nav_menu( array( 'theme_location' => 'trending-menu' ) );
    } elseif ( is_tree('fresh') ) { 
         wp_nav_menu( array( 'theme_location' => 'fresh-menu' ) );
    } ?>
    

you can soon check out my site at this link to find out how it looks.

edit: I have improved the code and the way it works to make it just use one template for trending posts and one for fresh posts, among other things. I will post the code soon.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.