Wordpress 使用插件替换主WP循环
我想用其他文件中的代码替换主WP循环,例如:Wordpress 使用插件替换主WP循环,wordpress,custom-wordpress-pages,Wordpress,Custom Wordpress Pages,我想用其他文件中的代码替换主WP循环,例如: <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> <?php get_template_part( 'tpl/content', 'single' ); ?> <?php endwhile; endif;?> 类似于Woocommerce的东西-我只想替换网站的内容,网站的页眉和页脚应该看起来像一个主题。 我
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'tpl/content', 'single' ); ?>
<?php endwhile;
endif;?>
类似于Woocommerce的东西-我只想替换网站的内容,网站的页眉和页脚应该看起来像一个主题。
我曾尝试使用template_include和single_template,但这些方法正在取代整个页面
我的主要目标是使用我的插件替换内容,无论WP中使用的是什么主题
我的主要目标是使用我的插件替换内容,无论WP中使用的是什么主题
如果你想改变WordPress页面的内容,你应该考虑
编写函数并将该函数添加到内容过滤器中,以过滤\u内容,或 创建一个短代码,让用户选择在哪个页面上呈现插件的内容。 您还可以创建一个函数,该函数被钩住以注册\u激活\u钩子,该钩子创建了一个新页面,其中预装了插件的短代码。我很确定WooCommerce就是这么做的。 编写一个函数并将该函数添加到内容过滤器中以过滤内容! 如果你还没有读过的话,我鼓励你读一下WP抄本!此外,您还可以知道在哪里可以钩住或过滤到: 还有一些参考资料: 编辑:OP写入:我的坏:内容-我是指我的CPT的所有字段:例如:标题、描述、作者、创建日期、价格…房间数量…等等 在这种情况下,您将需要进行模板覆盖,但是,然后,在模板中包含对和的调用。这些功能 包括当前主题目录中的[header/footer].php模板文件
您试图在何处使用模板包含和单个模板?在插件的文件中?上下文是什么?我在插件中尝试过,例如函数eventOrganizer_set_template$template{$template=plugin_dir_path FILE.'/templates/test.php';return$template;}add_filter'template_include',eventOrganizer_set_template';啊,好的。见下面我的答案。你可能不想在那里上钩!它的覆盖级别太高。!我的坏:内容-我指的是我的CPT的所有字段:例如:标题、描述、作者、创建日期、基于CMB2的价格、基于CMB2的房间数量等。我希望在这里看到所有这些字段:主题的其余部分应保持不变。提前感谢。编辑以解决其他问题:如果回答您的问题,请不要忘记点击“接受”!
<?php
// one/of/your/plugin/files.php
function marcin_plugin_content($content) {
/**
* Some check to establish that the request is for
* a page that your plugin is responsible for altering.
*/
if (true === is_page('marcin_plugin_page')) {
$new_content = 'whatever it is you do to create your plugin\'s content, do it here!';
return $new_content;
}
// we don't want to alter content on regular posts/pages
return $content;
}
add_filter('the_content', 'marcin_plugin_content');
<?php
// one/of/your/plugin/files.php
function marcin_plugin_shortcode($atts)
{
// Borrowed from https://codex.wordpress.org/Shortcode_API
$a = shortcode_atts(
[
'foo' => 'something',
'bar' => 'something else',
],
$atts
);
// Do whatever you do here to generate your plugin's output...
$output = 'Foo: "'.$a['foo'].'", Bar: "'.$a['bar'].'"';
return $output;
}
add_shortcode('marcin', 'marcin_plugin_shortcode');
<?php
// path/to/your/plugin/files.php
function marcin_on_activate() {
// Maybe do a check that this doesn't exist already to avoid duplicates.. this is just an example!!
$data = [
'post_title' => 'Plugin Page',
'post_content' => '[]',
'post_status' => 'publish',
];
// Insert the post into the database.
wp_insert_post($data);
}
register_activation_hook(__FILE__, 'marcin_on_activate');