admin管理员组

文章数量:1128488

I am trying to create a single.php file to be the default template for displaying single posts in a child theme that I have created from the TwentyTwentyThree theme. The reason I want to set this as the default is I have a lot of custom css used for the navbar that is a real pain to get working with the new editor, so I just want to have the single.php file that I can use to display the content as default. The code required is rendered in my header.php file and works well for the page templates, like front-page.php or other custom templates which I have created and is working fine Is there a way to do this?

I know that the block themes work a little differently but I thought that the hierarchy would still apply and I like the other features of the theme so I would like to keep it.

Here is the code for the single.php file:

<?php

get_header();

?>

<article class='content px-3 py-5 p-md-5'>


  <?php
    if( have_posts() ){
        while(have_posts()){
          the_post();
          the_content();
        }
    }
  ?>


</article>

<?php

get_footer();

?>

I am trying to create a single.php file to be the default template for displaying single posts in a child theme that I have created from the TwentyTwentyThree theme. The reason I want to set this as the default is I have a lot of custom css used for the navbar that is a real pain to get working with the new editor, so I just want to have the single.php file that I can use to display the content as default. The code required is rendered in my header.php file and works well for the page templates, like front-page.php or other custom templates which I have created and is working fine Is there a way to do this?

I know that the block themes work a little differently but I thought that the hierarchy would still apply and I like the other features of the theme so I would like to keep it.

Here is the code for the single.php file:

<?php

get_header();

?>

<article class='content px-3 py-5 p-md-5'>


  <?php
    if( have_posts() ){
        while(have_posts()){
          the_post();
          the_content();
        }
    }
  ?>


</article>

<?php

get_footer();

?>
Share Improve this question asked Jan 3, 2024 at 7:22 ShanahandoShanahando 31 bronze badge 3
  • 2 if your parent theme is a block theme then your child theme is also a block theme, trying to mix and match like this will get more and more frustrating as time goes on. The theme hierarchy does apply but it applies to the initial .html template files and block templates. I'd recommend you work entirely within the site editor for as much as possible, you can always enqueue additional CSS/JS, as well as add classes to blocks to apply that CSS. Putting CSS in a header.php is bad practice even when using PHP templates – Tom J Nowell Commented Jan 3, 2024 at 9:23
  • OK, thanks for letting me know. I guess I could tackle it that way I'm just struggling with how to tackle all the random wordpress classes the theme injects by default. It seemed much easier to work the old way with the classic themes. I appreciate you taking the time to help me understand this! – Shanahando Commented Jan 3, 2024 at 22:11
  • 2 WP should already have all the styling for those, and you're meant to adjust that styling using the global styles interface in the site editor as well as theme.json, not with new CSS. At all costs avoid inline CSS be that when you write it in the UI or insert it into files in the theme manually, that's true of both block themes and classic themes. As far as block themes are concerned the pain usually occurs because it's difficult to do something you don't need to do or shouldn't be doing. E.g. don't try to write CSS to adjust margins and paddings on the header, use the block editor controls – Tom J Nowell Commented Jan 4, 2024 at 9:31
Add a comment  | 

1 Answer 1

Reset to default 0

The answer is, AFAIK, that you shouldn't. It goes against how block themes work. Rather you should try to get your css to work with the block theme. What are the issues with getting your CSS to work with the new editor?

You can add it as style.css in your child theme, or you can add custom block styles with a simple plugin. The later is my prefered method as it works quite well with the block editor, and as a plugin it can be used even if I switch themes.

本文标签: How do I create a singlephp file as the default template for single posts in the twentytwentythree theme