我有一些代码可以生成所有post
文件。我想把这个下拉列表添加到每个帖子页面的顶部,就在帖子内容之前。
这样做的正确钩/动作是什么?
自定义函数。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\');
我们的想法是post
打开/查看页面,此下拉列表(通过create_post_dropdown
) 将添加在帖子内容之前。