Forum Replies Created

Viewing 7 replies - 1 through 7 (of 7 total)
  • Thread Starter alifhughes

    (@alifhughes)

    Is there anything you would recommend to get the previous post and all of its data? I would use just get_previous_post() but I want post data such as featured image.

    Thanks

    Thread Starter alifhughes

    (@alifhughes)

    Ah sorry, thank you for your detailed answer, I appreciate it.

    You are correct though, it didn’t die when I did the second if statement. However, it still is showing the same post in my previous post display.

    I think you are right, I will not use the global namespace and try tidy things up, then it might start to work.

    Thanks

    Thread Starter alifhughes

    (@alifhughes)

    Thanks for the response!

    I’ve used $post outside of the main loop, and used wp_reset_postdata(); after the main loop so thought that it would be fine to use it as I have been, which is why I am confused to why it isn’t working.

    When I use your test, it returns true, I did:

    if (assert($current_post->ID != $previous_post->ID )) { die(‘Same post’); }

    and it did display the die message.

    Any suggestions ?

    Bump! Having the exact same problem, really want to be able to get rid of it.

    Thread Starter alifhughes

    (@alifhughes)

    Thank you both for you answers, I looked into both but I think leejosepho suggestion of that plugin is exactly what I need.

    Thread Starter alifhughes

    (@alifhughes)

    I have found a solution if anyone else is having problems with this.

    I copied the function from Building a simple list and appended my own link to it at the end like so

    $homeUrl =  home_url();
    $menuList .= '<li><a class="menu-item menu-item-type-post_type menu-item-object-page" href="'. $homeUrl .'">Home</a></li>';

    The full code is:

    <?php
    // Get the location of the navigation menu
    $locations = get_nav_menu_locations();
    
    // Get the menu itself from the location (primary is header in my case)
    $menu = wp_get_nav_menu_object($locations['primary'] );
    
    // Get the menu items
    $menuItems = wp_get_nav_menu_items($menu->term_id);
    
    // Initialise a string to hold the unorded list
    $menuList = '<ul id="menu-' . $menuName . '">';
    
    // Iterate through each menu item from the list of all menu items and append
    // Name of menu item and url to its page
    foreach ( (array) $menuItems as $key => $menuItem ) {
        $title = $menuItem->title;
         $url = $menuItem->url;
         $menuList .= '<li><a href="' . $url . '">' . $title . '</a></li>';
     }
    // Get the link to home url (in my case root (localhost/wordpress/)
    $homeUrl =  home_url();
    
    // Append own link
    $menuList .= '<li><a href="'. $homeUrl .'">Home</a></li>';
    
    // Close the unordered list tag
    $menuList .= '</ul>';
    
    // Echo the list back to html
    echo $menuList;
    ?>

    The problem in my case is that using wp_nav_menu($menuItems); would mean that the hardcoded list item I needed to add wasn’t included in the unordered list that the function automatically created, thus causing styling problems.
    This unpacks the list, appends own custom link, and displays it.

    Thread Starter alifhughes

    (@alifhughes)

    Do you mean in the actual HTML code? I have tried but my custom styling and JavaScrip doesn’t seem to like the combination of both.

Viewing 7 replies - 1 through 7 (of 7 total)