• I need to place an ordered list that displays links that depend on the purpose of the page (page purpose = department name). So if the page’s name is “Dept 1” the links in the ordered list will be to the “Dept 1 support” page, etc.

    I have 13 departments, so I need take the current page’s name and compare it to each department, and if there is a match assign the correct link to a variable. The variable will be used in a html link.

    I’m not familiar with PHP, but I know Visual Basic and understand loops. Perhaps there is a better way than using PHP to loop? I’m open to suggestions and would greatly example code.

Viewing 13 replies - 1 through 13 (of 13 total)
  • How are your departments setup? Are they a custom post type, pages tagged with a custom taxonomy, or another way?

    How is a department page connected to the support page? Do you have any variables in the database that connect the two?

    Thread Starter thisismyalias

    (@thisismyalias)

    Well it’s very basic. The departments are just using a custom page template, and the page’s name is the department and thus the qualifier for the loop. The support page is a child of the department page. That’s really it.

    OK, so in your page template, you will have access to the post ID. Using that, you can retrieve any fields for that page.

    Let’s say you have a page called “Department 1” that lives at example.org/department-1. And let’s say you have a child page called “Support” that lives at example.org/department-1/support. In your template, you could just link to <a href="support"> and it would be relative to whichever department you were on.

    If you have multiple child pages and want to display them all, you could use something like get_pages with the child_of parameter to retrieve all sub-pages, and then loop through them in your page template.

    To answer your question, if you have an array of anything, you’re going to need to use a PHP loop in one way or another.

    Thread Starter thisismyalias

    (@thisismyalias)

    Thank you. Not all the pages are child pages (2 out of the 3 are), and the child pages are not named the same under each parent. I will definitely have to loop.

    I don’t see any nice & tidy way though. It’ll be more like if A = B then the links will be 1, 2, and 3. If A = C then the links will be 4, 5, and 6, etc.

    I’ll look further into get_pages and child_of though.

    Or you can use custom fields. If you are on the “edit page” page, take a look at the very top. There is a tab called “Screen Options”. Click on that and select “Custom Fields”.

    You can add something like “link1” in the name and the actual link in the value. Or you can create one item called “links” and then enter something like:

    Name of link 1|support
    Name of link 2|some-other-page
    Name of link 3|third-page

    Which you can then parse in the template.

    If you want a more elegant solution, you need to learn how to create meta boxes and save their values (tutorials online). Or you could use a plugin like Meta Box that gives you a little nicer GUI for adding custom fields.

    With either method described above, you don’t need if A == B, if A == C, you can just simply display a uniform field on all pages that will just have different values.

    Thread Starter thisismyalias

    (@thisismyalias)

    I’m going to re-read that another 10 times, and maybe it’ll start to make sense. ?? Thank you.

    OK, no problem! If you there is anything you need clarification on, just post back here.

    Thread Starter thisismyalias

    (@thisismyalias)

    I figured it out, using your help; however, after getting it I realized that I couldn’t loop through the child pages because I needed to add verbiage after each link. My solution is to hard code the output, but fill in the links from the get_pages array.

    My new problem is that I can’t figure out how to call a particular result of the array. For example, the code gets the child page of the current parent (there will only ever be 2 child pages). I need to call the URL of the first child page, but I can’t figure out how to access the first dimension of the array. Every single example I found loops through arrays.

    <?php
                $mypages = get_pages( array( 'child_of' => $post->ID, 'sort_column' => 'post_title', 'sort_order' => 'asc' ) );
    
                ?>
                <section class="span4 articles-list" style="margin-left:0;">
                    <ul class="articles">
                        <li class="article-entry standard"><h4><a href="<?php echo ; ?>"><?php echo ; ?></a></h4>
                        <span class="article-meta">If you are ..., start here.</span>
                        </li>
                        <li class="article-entry standard"><h4><a href="<?php echo ; ?>"><?php echo ; ?></a></h4>
                        <span class="article-meta">If you are ..., start here.</span>
                        </li>
                    </ul>
                </section>
                <?php
    
            ?>

    Here is what I would do. Add a custom field to each of the child pages, just as described earlier, and call it “link_title” and give it the name of the link.

    Then on your main page, loop over $mypages, and within the loop, make a call to that custom field:

    foreach ($mypages as $mypage) {
      $link_title = get_post_meta($mypage->ID, 'link_title', true);
      echo '<a href="' . get_permalink($mypage->ID) . '">' . $link_title . '</a>';
    }

    If you need to add more fields, feel free to do so. This should solve your loop and your hard coding problem.

    Thread Starter thisismyalias

    (@thisismyalias)

    Thank you. That’s a good suggestion. I get weird spacing with the loop, and I’m sure I could fix it with some CSS modifications, but being a novice at all this, I don’t want to go down that path at the moment. Therefore, I’m going to figure out how to assign each child page data from the array statically.

    In that case, you can just reference each item in the array by its index:

    $mypages[0] and $mypages[1]

    Just know that if the order every changes or you add more pages, you might not be getting the page you think you’re getting.

    Another way might be to call each page by its ID, as that would be a bit more reliable:

    get_post(1234) and get_post(5678)

    Just a side note, the space issue you describe, it shouldn’t be due to the implementation, but rather just the markup you wrote, which wouldn’t be solved by either implementation.

    Thread Starter thisismyalias

    (@thisismyalias)

    You are right about the space issue. I’m not sure what was happening, but I redid the code and the spacing issue is gone. Now I can use the for loop, and I used your custom field idea, and it works. Thank you so much for your help. I learned a lot with this exercise.

    Glad to hear you found a solution that worked for you!

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘Display links based off of page’ is closed to new replies.