$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>wp query - Regarding a custom loop and output HTML tags|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

wp query - Regarding a custom loop and output HTML tags

matteradmin8PV0评论

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(). '">
Post a comment

comment list (0)

  1. No comments so far