admin管理员组文章数量:1130349
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 |3 Answers
Reset to default 1It 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.
- delete
global $title_yama; - fixed href attribute
get_permalink()toesc_url(get_the_permalink()) - fixed inside
<a>tagthe_title()toget_the_title() - fixed
returntoecho
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(). '">
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. Usethe_title( '', '', false )where thefalsemeans "don't echo". See the reference. – Sally CJ Commented Nov 3, 2018 at 17:16
3 Answers
Reset to default 1It 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.
- delete
global $title_yama; - fixed href attribute
get_permalink()toesc_url(get_the_permalink()) - fixed inside
<a>tagthe_title()toget_the_title() - fixed
returntoecho
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(). '">
本文标签: wp queryRegarding a custom loop and output HTML tags
版权声明:本文标题:wp query - Regarding a custom loop and output HTML tags 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749208215a2332925.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


$title_yama. Usethe_title( '', '', false )where thefalsemeans "don't echo". See the reference. – Sally CJ Commented Nov 3, 2018 at 17:16