• Resolved ciaravino

    (@ciaravino)


    How would you make it so values of custom fields in the page are also displayed next to the title of the page when you are displaying a list of pages? Here’s an example: https://cl.ly/1QcB

    I am using wp_list_pages to list the pages.

    I would eventually want to be able to sort the data in each field, but I need to find out how to at least display it first…

Viewing 15 replies - 1 through 15 (of 17 total)
  • Thread Starter ciaravino

    (@ciaravino)

    I guess this would be similar to displaying a list of products with their price and stuff.

    Thread Starter ciaravino

    (@ciaravino)

    Can anyone even see this or did a moderator mark me as the thing that makes it so no one can see my posts…?

    I haven’t got a reply to one of my posts in here for like 5 days and I’ve bumped them up once they get on the 3rd page. Like, I’ll see someone answer 6 posts (so they are all at the top at the same time), and one of my posts will be like the 7th one lol, every time. The things I’m asking could be too complicated I guess. I’m just wondering :O

    Moderator keesiemeijer

    (@keesiemeijer)

    Put this in your theme’s functions.php:

    function get_my_meta(){
      $html= '';
      $my_pages = get_pages();
      if(!empty($my_pages)){
      $html .= '<ul>' . "\n";
        foreach ($my_pages as $pag) {
        $year = get_post_meta($pag->ID, 'year', true);
        $channel = get_post_meta($pag->ID, 'channel', true);
        $html .=  '<li>' . $pag->post_title;
        $html .= ($channel != '') ? '<span class="channel">' . $channel . '</span>' : '';
        $html .= ($channel != '') ? '<span class="year">' . $year . '</span>' : '';
        $html .= '</li>'. "\n";
    	  }
      }
      $html .= '</ul>' . "\n";
      return $html;
    }

    This will display an unordered list off all your Pages with a span inside containing the channel and the year. You have to give your Pages the custom fields “channel” and “year” or change the meta_keys in the function.

    Call the function in your theme’s template files like so:
    <?php echo get_my_meta(); ?>
    You can style <span class="channel"> and <span class="year"> in your theme’s stylesheet (usualy: style.css)

    Thread Starter ciaravino

    (@ciaravino)

    Thanks! I’m using keesiemeijer’s method and it’s working for displaying the list and the custom fields.

    Is it possible to get the name of a page’s parent if it’s not a certain page? For example, something like this:

    if ([parent title or slug] != "games") {
    [get parent's title or slug]
    }

    The website I have is about games, not movies. I used movies as an example because I figured it would be more generally recognized.

    So right now, I have a page called “games”. Every video game listing page is a child of the games page. Some video games have expansions though, so the expansion’s game listing is a child of the video game, which is a grandchild of games. I guess another option would be something like:

    if ([grandchild of games [or the games id]) {
    [get parent's title or slug]
    }

    I’m trying to generate the links to each game and expansion in the list I just made. I can get the links to the child pages (games/[game name]) but I can’t figure out how to get links to the grandchildren (games/[game name]/[game’s expansion]). I think if I can find out a grandchild’s parent’s name, I can input that as the [game name] after str_replacing the spaces for “-“.

    When something is in brackets in the if statements it’s because I don’t know what code would be used there to get what is inside the brackets ??

    Moderator keesiemeijer

    (@keesiemeijer)

    Where do you want to use these conditionals? Inside the loop or inside the function? Did you look at Conditional Tags. There is an example there of Testing for sub-Pages :

    <?php
    
    global $post;     // if outside the loop
    
    if ( is_page() && $post->post_parent ) {
        // This is a subpage
    
    } else {
        // This is not a subpage
    }
    ?>

    Thread Starter ciaravino

    (@ciaravino)

    https://cl.ly/1S1v

    In that image, when I hover over Wrath of the Lich King, I get https://mac.gamingapple.com/games/wrath-of-the-lich-king but I need it to be https://mac.gamingapple.com/games/world-of-warcraft/wrath-of-the-lich-king since Wrath of the Lich King is an expansion of World of Warcraft, so I have it as a child of the World of Warcraft page.

    WordPress automatically forwards the user to https://mac.gamingapple.com/games/world-of-warcraft/wrath-of-the-lich-king once they click on https://mac.gamingapple.com/games/wrath-of-the-lich-king, which is kind of cool, but I don’t know if it will mess with SEO.

    Moderator keesiemeijer

    (@keesiemeijer)

    WordPress redirects (based on the last part of the url) to the correct address. What tags or code are you using to show these links?

    Thread Starter ciaravino

    (@ciaravino)

    function get_my_meta(){
      $html= '';
      if (get_bloginfo('description') == "Mac") {
    	$gamesid = 15;
    	}
      $my_pages = get_pages('child_of='. $gamesid . '&depth=1&echo=0&sort_colum=menu_order');
    
      $getesrbrating = get_post_meta($pag->ID, 'ESRB Rating', true);
      $esrbratingreplace = str_replace(" ", "-", $getesrbrating);
    
      $getdeveloper = get_post_meta($pag->ID, 'Developer', true);
      $developerreplace = str_replace(" ", "-", $getdeveloper);
    
      if(!empty($my_pages)){
      $html .= '<ul>' . "\n";
        foreach ($my_pages as $pag) {
        $getgame = $pag->post_title;
        $gamereplace = str_replace(" ", "-", $getgame);
      	$getesrbrating = get_post_meta($pag->ID, 'ESRB Rating', true);
    	$esrbratingreplace = str_replace(" ", "-", $getesrbrating);
    	$getdeveloper = get_post_meta($pag->ID, 'Developer', true);
    	$developerreplace = str_replace(" ", "-", $getdeveloper);
        $releasedate = get_post_meta($pag->ID, 'Release Date', true);
        $esrbrating = '<a href="https://' . get_bloginfo('description') . '.' . 'gamingapple.com/esrb-ratings' . '/' . $esrbratingreplace . '">' . get_post_meta($pag->ID, 'ESRB Rating', true) . '</a>';
        $developer = '<a href="https://' . get_bloginfo('description') . '.' . 'gamingapple.com/developers' . '/' . $developerreplace . '">' . get_post_meta($pag->ID, 'Developer', true) . '</a>';
        $html .=  '<li><a href="https://' . get_bloginfo('description') . '.' . 'gamingapple.com/games' . '/' . $gamereplace . '">' . $pag->post_title . '</a>';
        $html .= ($releasedate != '') ? '<span class="releasedate">' . $releasedate . '</span>' : '';
        $html .= ($esrbrating != '') ? '<span class="esrbrating">' . $esrbrating . '</span>' : '';
        $html .= ($developer != '') ? '<span class="developer">' . $developer . '</span>' : '';
        $html .= '</li>'. "\n";
    	  }
      }
      $html .= '</ul>' . "\n";
      return $html;
    }

    To get links for things, I’m using their title and replacing all the spaces with hyphens, lol. I’m sure there is a better way, but this is what I came up with. It’s your original code.

    The main problem I’m having is I don’t know how to get a variable for the actual game’s title in the links for expansion games, and how to tell WordPress when one of the links should be an expansion (meaning it’s a grandchild of /games/)

    Another problem is that the list isn’t displaying the game expansions in “menu_order”. For example, at the very bottom of the page (if you look at the live https://mac.gamingapple.com/games/), you see World of Warcraft followed by Cataclysm, The Burning Crusade and Wrath of the Lich King. The problem is Cataclysm’s menu_order is 3 because it’s the newest World of Warcraft expansion, therefore it should be last in the list. The Burning Crusade is 2 because it’s the 2nd expansion, etc. This page is currently displaying all of the game pages in alphabetical order, which I want. But, I don’t want the game expansions to be ordered alphabetically, I need them ordered by menu_order. The expansions are any games that are grandchildren of the /games/ page.

    Moderator keesiemeijer

    (@keesiemeijer)

    I don’t see the code for the expansions links. For correct links to the page you could do something like this:

    $html .= '<a href="' . get_page_link($pag->ID) . '" ';
    $html .= 'title="read more about: ' . $pag->post_title . '" >';

    And work that in to your function

    Moderator keesiemeijer

    (@keesiemeijer)

    For the expansion links you can do this:

    $html .= '<ul>';
    $html .= wp_list_pages("title_li=&child_of=".$pag->ID."&echo=0");
    $html .= '</ul>';

    it displays a list of subpages. If a page does not have any expansions (subpages) it will display nothing

    Thread Starter ciaravino

    (@ciaravino)

    I don’t have any code for expansion links yet. They are just being displayed with all the normal game pages.

    If I use:

    $html .= '<ul>';
    $html .= wp_list_pages("title_li=&child_of=".$pag->ID."&echo=0");
    $html .= '</ul>';

    will the expansion links be displayed with the normal links? Because I still want them to be listed with the normal game links, just under them and in the correct order, like this: https://mac.gamingapple.com/esrb-ratings/teen/ it keeps the hierarchy.

    EDIT: I just used:

    $html .= '<a href="' . get_page_link($pag->ID) . '" ';
    $html .= 'title="read more about: ' . $pag->post_title . '" >';

    And it works for getting me the proper links! ?? I’m still looking for the solution to the ordering though. I’m trying wp_list_pages.

    EDIT 2: I tried using wp_list_pages instead of get_pages but it gave me an error on my foreach statement. Is there something I can do to just target grandchildren of a page? Like, make it so every grandchild gets displayed as not ASC alphabetical, but menu_order without affecting the child pages?

    Moderator keesiemeijer

    (@keesiemeijer)

    You can do this. put the last code between the

    foreach ($my_pages as $pag) {
    }
    $childpages = get_pages('child_of='. $pag->ID);
    if(!empty($childpages)){
      foreach ($childpages as $cpag) {
        $html .= '<a href="' . get_page_link($cpag->ID) . '" ';
        $html .= 'title="read more about: ' . $cpag->post_title . '" >';
        $html .= $cpag->post_title . '</a>';
      }
    }

    Thread Starter ciaravino

    (@ciaravino)

    Thanks!

    I just had to add/change a few things. I had to make it so the original get_pages only listed pages whose parent was /games/ so I didn’t get duplicate listings from the main get_pages and then from the child get_pages. In the above code you just gave me I also just added sort_column=menu_order. Last, I put that code at the bottom of the original $my_pages foreach.

    EDIT: It’s starting to look good. Thanks a lot, you’ve helped a ton :D. I’m putting everything in a table instead of ul. I’d like users to be able to filter the data, can you point me in the right direction if I wanted to accomplish it? Like a tutorial or something.

    Also, do you know of any WordPress functions for finding the number of children or grandchildren of a page? I’ve looked but I didn’t see anything. I’d like to be able to list a number of games and expansions, if possible.

    Moderator keesiemeijer

    (@keesiemeijer)

    You can do a lot of filtering with get_pages (get_pages('arguments');) but I think you mean javascript filtering. Start a new topic for that one.
    To count the childpages do this

    $child_pages = get_pages('child_of='.$pag->ID);
    $count = count($child_pages); //subpage count

    check if a WordPress page has children/subpages

    $children  = get_pages('child_of='.$pag->ID);?>
    if( count( $children ) != 0 ) { // Has Children }
    else { // No children }

    or

    $children  = wp_list_pages('&child_of='.$pag->ID.'&echo=0');
    if($children) {
    // This page has subpages
    }

    another handy function to get the page ID by pagename

    function get_ID_by_page_name($page_name){
    global $wpdb;
    $page_name_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name = '".$page_name."'");
    return $page_name_id;
    }

    call like this:

    $page_id = get_ID_by_page_name('developers');

    for testing and looking what the get_pages array contains:

    $mypages = get_pages();
    echo '<pre>';
    print_r($mypages);
    echo '</pre>';

    This spits out an array of all your pages.

Viewing 15 replies - 1 through 15 (of 17 total)
  • The topic ‘How would you make it so values of custom fields in the page are also displayed’ is closed to new replies.