WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to get an Action Hook to work as expected? (3 posts)

  1. heavym
    Member
    Posted 4 years ago #

    Good Afternoon,

    The below block of code is used to display the parent page and submenu pages in list format. Rather than pasting this on each of my different templates, I would like to paste it once in say the function.php theme file and then use an action hook single line of code to call it on each of the templates.

    I understand the idea of how this works but in practice it doesnt for me.

    <?php
    if($post->post_parent)
    $children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");
    else
    $children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
    if ($children) { ?>

      <?php echo $children; ?>

    <?php } ?>

    In the functions.php file I added the code with function mycustom_menu() { in front of it, and } after it. And yes their already is a function.php file with code in it, so all of this code above is between and initial opening php and closing php.

    Then in my template where I want to call it I put: <?php do_action('mycustom_menu'); ?>

    But it did not display. Is their a better way to go about this, or different syntax needed? Thanks!

    Cheers,
    Christopher

  2. heavym
    Member
    Posted 4 years ago #

    Oh and to note if in the function I write: echo '<div id="random">test</div>'; for instance that does work. If I do an echo for the code above however it just spits out the actual code rather than rendering it.

    I've tried echoing just the divs, and no the php code, but the page continues to go blank. Any ideas? Thank you!

    Cheers,
    Christopher

  3. heavym
    Member
    Posted 4 years ago #

    Spoke with a PHP guy, had to change the code a bit by echoing HTML lines and not others. So was just a matter of syntax.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.