在循环之前添加内容 [英] Adding content before the loop

查看:25
本文介绍了在循环之前添加内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法通过 WordPress 插件在循环之前添加内容?

Is there a way to add content before the loop through a WordPress plugin?

具体来说,我希望在帖子之前(在帖子列表和单个帖子中)将 HTML 添加到内容区域

Specifically, I'm looking to add HTML to the content area, before the posts (both in post lists AND individual posts)

这是理想位置的屏幕截图:http://note.io/1ne3J73

Here's a screenshot of the ideal placement: http://note.io/1ne3J73

这是否可能适用于所有主题?

Is this at all possible to work across all themes?

推荐答案

这可能很棘手,因为每个主题都因循环的显示方式而异,但是您可以创建一个插件来使用 loop_start 动作,在标准 WP 循环的第一篇文章之前调用:

This could be tricky simply because every theme differs with how the loop is displayed, however you could create a plugin to use the loop_start action, which is called before the first post of the standard WP loop:

add_action( 'loop_start', 'test_loop_start' );

function test_loop_start( $query ){
    echo 'this is my inserted text';
}

现在使用它会在每次调用循环时显示它(无论是在页面、帖子、类别页面、搜索页面等上),这是您可能不想要的.因此,您可以使用 is_category()、is_archive()、is_singular() 等对其进行微调(基本上是任何可以帮助识别用户所在页面类型的内置 WP 函数):

Now using this would display it every single time the loop is called (whether on a page, a post, category page, search page, etc.), which you may not want. So you could fine tune it with is_category(), is_archive(), is_singular(), etc. (basically any of the built in WP functions that can help identify what kind of page the user is on):

add_action( 'loop_start', 'test_loop_start' );

function test_loop_start( $query ){

    if(is_category() OR is_singular()) { 
    echo 'this is my inserted text';
    }
}

这篇关于在循环之前添加内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆