admin管理员组文章数量:1130349
I have some code that generates a dropdown list of all my post files. I would like to add this dropdown to the top of every post page, right before the post content.
What's the correct hook/action to do so?
custom-functions.php:
<?php
function create_post_dropdown($titles){
?>
<div id="article-choice">
<h3>Choose an Article, or browse below</h3>
<select onchange="if (this.value) window.location.href=this.value">
<?php
foreach($titles as $title => $url){
echo "<option value=" . $url . ">" . $title . "</option>";
}
?>
</select>
</div>
<?php
}
add_action('__after_header', 'create_post_dropdown');
function add_dropdown_to_posts(){
$args = [
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1
];
$posts = new WP_Query( $args );
$titles = get_post_titles($args);
if (get_post_type() == "post"){
$title = $post->post_title; // get_the_title();
$title = create_post_dropdown($titles) . "<br>" . $title;
}
return $title;
}
add_filter('the_content', 'add_dropdown_to_posts');
The idea is that when a post page is opened/viewed, this dropdown (made via create_post_dropdown) will be added before the post content.
I have some code that generates a dropdown list of all my post files. I would like to add this dropdown to the top of every post page, right before the post content.
What's the correct hook/action to do so?
custom-functions.php:
<?php
function create_post_dropdown($titles){
?>
<div id="article-choice">
<h3>Choose an Article, or browse below</h3>
<select onchange="if (this.value) window.location.href=this.value">
<?php
foreach($titles as $title => $url){
echo "<option value=" . $url . ">" . $title . "</option>";
}
?>
</select>
</div>
<?php
}
add_action('__after_header', 'create_post_dropdown');
function add_dropdown_to_posts(){
$args = [
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1
];
$posts = new WP_Query( $args );
$titles = get_post_titles($args);
if (get_post_type() == "post"){
$title = $post->post_title; // get_the_title();
$title = create_post_dropdown($titles) . "<br>" . $title;
}
return $title;
}
add_filter('the_content', 'add_dropdown_to_posts');
The idea is that when a post page is opened/viewed, this dropdown (made via create_post_dropdown) will be added before the post content.
1 Answer
Reset to default 1One way to do that is to have the custom dropdown function return the custom html. Then you can call it inside your add_dropdown_to_posts and put the returned content into a helper variable. After that just prepend the custom html to the $content variable that the the_content filter provides.
Like this,
function my_custom_dropdown_html() {
return '<html stuff here>'; // you could put your html also in a variable and then return that.
}
function add_dropdown_html_before_the_content($content) {
$dropdown = my_custom_dropdown_html();
// if statement just to be safe - e.g you change the custom function output to something else than string of html and forget you've used it here
if ( $dropdown && is_string( $dropdown ) ) {
$content = $dropdown . $content; // prepend custom html to content
}
return $content;
}
add_filter( 'the_content', 'add_dropdown_html_before_the_content' );
I have some code that generates a dropdown list of all my post files. I would like to add this dropdown to the top of every post page, right before the post content.
What's the correct hook/action to do so?
custom-functions.php:
<?php
function create_post_dropdown($titles){
?>
<div id="article-choice">
<h3>Choose an Article, or browse below</h3>
<select onchange="if (this.value) window.location.href=this.value">
<?php
foreach($titles as $title => $url){
echo "<option value=" . $url . ">" . $title . "</option>";
}
?>
</select>
</div>
<?php
}
add_action('__after_header', 'create_post_dropdown');
function add_dropdown_to_posts(){
$args = [
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1
];
$posts = new WP_Query( $args );
$titles = get_post_titles($args);
if (get_post_type() == "post"){
$title = $post->post_title; // get_the_title();
$title = create_post_dropdown($titles) . "<br>" . $title;
}
return $title;
}
add_filter('the_content', 'add_dropdown_to_posts');
The idea is that when a post page is opened/viewed, this dropdown (made via create_post_dropdown) will be added before the post content.
I have some code that generates a dropdown list of all my post files. I would like to add this dropdown to the top of every post page, right before the post content.
What's the correct hook/action to do so?
custom-functions.php:
<?php
function create_post_dropdown($titles){
?>
<div id="article-choice">
<h3>Choose an Article, or browse below</h3>
<select onchange="if (this.value) window.location.href=this.value">
<?php
foreach($titles as $title => $url){
echo "<option value=" . $url . ">" . $title . "</option>";
}
?>
</select>
</div>
<?php
}
add_action('__after_header', 'create_post_dropdown');
function add_dropdown_to_posts(){
$args = [
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1
];
$posts = new WP_Query( $args );
$titles = get_post_titles($args);
if (get_post_type() == "post"){
$title = $post->post_title; // get_the_title();
$title = create_post_dropdown($titles) . "<br>" . $title;
}
return $title;
}
add_filter('the_content', 'add_dropdown_to_posts');
The idea is that when a post page is opened/viewed, this dropdown (made via create_post_dropdown) will be added before the post content.
1 Answer
Reset to default 1One way to do that is to have the custom dropdown function return the custom html. Then you can call it inside your add_dropdown_to_posts and put the returned content into a helper variable. After that just prepend the custom html to the $content variable that the the_content filter provides.
Like this,
function my_custom_dropdown_html() {
return '<html stuff here>'; // you could put your html also in a variable and then return that.
}
function add_dropdown_html_before_the_content($content) {
$dropdown = my_custom_dropdown_html();
// if statement just to be safe - e.g you change the custom function output to something else than string of html and forget you've used it here
if ( $dropdown && is_string( $dropdown ) ) {
$content = $dropdown . $content; // prepend custom html to content
}
return $content;
}
add_filter( 'the_content', 'add_dropdown_html_before_the_content' );
本文标签: hooksAdd function to every post
版权声明:本文标题:hooks - Add function to every post? 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749252225a2339877.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论