admin管理员组

文章数量:1389807

the original .php part of my theme looks like this:

$bam_related_posts_taxonomy = get_theme_mod( 'bam_related_posts_taxonomy', 'category' );


$bam_post_args = array(
    'posts_per_page'    => absint( get_theme_mod( 'bam_related_posts_count', '3' ) ),
    'orderby'           => 'rand',
    'post__not_in'      => array( get_the_ID() ),
);


(*A lot of other codes here*)


wp_reset_postdata();

I added this part below the 'post___not_in' myself:

'category__not_in'  => array(21),

This way, I exclude a certain category. It's working, however, I want to add this last part of the code in my own plugin, so it won't reset when I update my theme. How do I get this last line into the functions.php of my own plugin? Simply adding that line to it won't work, it needs to point to the rest of the code...

the original .php part of my theme looks like this:

$bam_related_posts_taxonomy = get_theme_mod( 'bam_related_posts_taxonomy', 'category' );


$bam_post_args = array(
    'posts_per_page'    => absint( get_theme_mod( 'bam_related_posts_count', '3' ) ),
    'orderby'           => 'rand',
    'post__not_in'      => array( get_the_ID() ),
);


(*A lot of other codes here*)


wp_reset_postdata();

I added this part below the 'post___not_in' myself:

'category__not_in'  => array(21),

This way, I exclude a certain category. It's working, however, I want to add this last part of the code in my own plugin, so it won't reset when I update my theme. How do I get this last line into the functions.php of my own plugin? Simply adding that line to it won't work, it needs to point to the rest of the code...

Share Improve this question asked Mar 14, 2020 at 15:30 IzzyIzzy 1 2
  • Is this a theme or a child theme? Note that the __not_in type parameters are extremely expensive/slow those queries are very heavy on the server – Tom J Nowell Commented Mar 14, 2020 at 15:49
  • This is not a child theme. That's why I use my own plugin for .php codes – Izzy Commented Mar 14, 2020 at 16:09
Add a comment  | 

1 Answer 1

Reset to default 0

You don't, what you've modified is a template file, those don't go in plugins.

Instead, create a child theme and copy this file into that theme. WordPress will preferentially load templates from the child theme, falling back to the original parent theme. This way any modifications you make in the child theme will overwrite the parent, and if the parent is updated no changes are lost

本文标签: How to get this php code from my theme into my plugin