wp query - Regarding a custom loop and output HTML tags

admin2025-06-06  4

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?

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?

Share Improve this question edited Nov 3, 2018 at 12:59 cjbj 15k16 gold badges42 silver badges89 bronze badges asked Nov 3, 2018 at 12:55 X-MUK-625X-MUK-625 12 bronze badges 1
  • 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 Commented Nov 3, 2018 at 17:16
Add a comment  | 

3 Answers 3

Reset to default 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.

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

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(). '">
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749208242a317268.html

最新回复(0)