0

I want to make the code to display the list of the page title in footer. I made the following code in functions.php:

  add_action('yamada_action', 'list_page_2');

    function list_page_2() {

      global $title_yama;
      $title_yama = '';

      $args  = array(
        'post_type'   => 'page',
        'post_status' => 'publish',
      );
      $queries_yama = new WP_Query($args);
      if($queries_yama->have_posts()): while ($queries_yama->have_posts()):$queries_yama->the_post();
        $title_yama .= '<li class="aaa"><a href="' .get_permalink(). '">'.the_title().'</a></li>';
       endwhile; endif;
       wp_reset_query();
       return $title_yama;
 }

and I inputed in footer.php the following code:

    <?php do_action('yamada_action'); ?>

However, the code displays just text as title.

How should I make the code in order to output including HTML code?

  • You're echoing the post title and not adding it to $title_yama. Use the_title( '', '', false ) where the false means "don't echo". See the reference. – Sally CJ Nov 3 '18 at 17:16
1

It seems you are confusing actions and filters here. You are using an action. That means if you want to output some html you have to that inside the function. Now your are returning the value, but nothing is done with it.

So in the last line of your function you should have echo $title_yama rather than return $title_yama.

Also, in your code you are accessing a global variable $title_yama, which you are then erasing. That doesn't seem to make much sense.

| improve this answer | |
0

Thank you very much for your advice. I can solve this problrems as follows.

function.php

add_action('yamada_action', 'list_page_2');

function list_page_2() {

    $title_yama = '';

    $args  = array(
        'post_type'   => 'page',
        'post_status' => 'publish',
    );
    $queries_yama = new WP_Query($args);
    if($queries_yama->have_posts()): while ($queries_yama->have_posts()):$queries_yama->the_post();
        $title_yama .= '<li class="aaa"><a href="'.esc_url(get_the_permalink()). '">'.get_the_title().'</a></li>';
    endwhile; endif;
    wp_reset_query();
    echo $title_yama;
}

footer.php

    <?php do_action('yamada_action'); ?>

I fixed it as follows.

  1. delete global $title_yama;
  2. fixed href attribute get_permalink() to esc_url(get_the_permalink())
  3. fixed inside <a> tag the_title() to get_the_title()
  4. fixed return to echo
| improve this answer | |
-1

get_permalink() returns permalink of the current post as a variable, but does not echo it out. Try <a href="' . echo esc_url(get_the_permalink()). '"> instead of

<a href="' .get_permalink(). '">
| improve this answer | |
  • Thank you for your advice. I made a step forward by your advice. I can display the link and title in footer now. however, I cannot generate the link. – Yamada Nov 3 '18 at 13:22
  • hmm, what do you mean when speaking about "generating the link"? May be you should try get_the_permalink() instead of get_permalink()? get_the_permalink() retrieves the full permalink. – yurha Nov 3 '18 at 13:34
  • Currently, although the_permalink is in href tag, my code make displaying permalink as text. – Yamada Nov 3 '18 at 13:40
  • You have get_permalink() in your code. Try get_the_permalink() instead. I've edit my answer a bit. Check it. – yurha Nov 3 '18 at 13:45
  • Thank you for your reply.it seems I cannot insert echo before esc_url. it displayed error. – Yamada Nov 3 '18 at 13:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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