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?
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.
global $title_yama;
get_permalink()
to esc_url(get_the_permalink())
<a>
tag the_title()
to get_the_title()
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(). '">
$title_yama
. Usethe_title( '', '', false )
where thefalse
means "don't echo". See the reference. – Sally CJ Commented Nov 3, 2018 at 17:16