0

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();

?>
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 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 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 at 9:31

1 Answer 1

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.

2
  • Thanks Audun. My main issue is that when I am trying to customize the navigation bar but also make it show dynamic menus the theme adds a lot of WP classes that make it difficult to style in a css file
    – Shanahando
    Commented Jan 3 at 23:22
  • I agree that it is sometimes difficult to pinpoint which classes that causes what, and how, with the block themes. As pointed out by Tom Nowell in his comment, try to use the block editor controls as much as possible. Beyond that, the greater specificity of your CSS, the more likely it is that your CSS work as expected. Though, the basic nav block of block themes is very limited, and probably won't support dynamic menus very well. A quick search shows that there already are plugins for it, maybe that's your best bet. Commented Jan 5 at 9:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.