Display a custom single.twig for specific categories












0















In timber you can display custom pages, based on page.twig, like page-casestudies.twig, which I'm using to show a list of all posts with a category of 'casestudies'. Just including /casestudies in the URL will render the page. This is default timber functionality and works exactly as I'd expect. I then want to show (for casestudies) a custom single-casestudies.twig, which is based off but different to single.twig (which I use for blog posts) and needs a different layout. I use additional ACF's in the casestudies. I have no idea how I go about this, or if it is even possible, or if there is a better way, such as custom post types. I've seen lots of posts about custom post types, but they only mention a list of posts, and not the actual display of the custom post itself. Thanks in advance.










share|improve this question





























    0















    In timber you can display custom pages, based on page.twig, like page-casestudies.twig, which I'm using to show a list of all posts with a category of 'casestudies'. Just including /casestudies in the URL will render the page. This is default timber functionality and works exactly as I'd expect. I then want to show (for casestudies) a custom single-casestudies.twig, which is based off but different to single.twig (which I use for blog posts) and needs a different layout. I use additional ACF's in the casestudies. I have no idea how I go about this, or if it is even possible, or if there is a better way, such as custom post types. I've seen lots of posts about custom post types, but they only mention a list of posts, and not the actual display of the custom post itself. Thanks in advance.










    share|improve this question



























      0












      0








      0








      In timber you can display custom pages, based on page.twig, like page-casestudies.twig, which I'm using to show a list of all posts with a category of 'casestudies'. Just including /casestudies in the URL will render the page. This is default timber functionality and works exactly as I'd expect. I then want to show (for casestudies) a custom single-casestudies.twig, which is based off but different to single.twig (which I use for blog posts) and needs a different layout. I use additional ACF's in the casestudies. I have no idea how I go about this, or if it is even possible, or if there is a better way, such as custom post types. I've seen lots of posts about custom post types, but they only mention a list of posts, and not the actual display of the custom post itself. Thanks in advance.










      share|improve this question
















      In timber you can display custom pages, based on page.twig, like page-casestudies.twig, which I'm using to show a list of all posts with a category of 'casestudies'. Just including /casestudies in the URL will render the page. This is default timber functionality and works exactly as I'd expect. I then want to show (for casestudies) a custom single-casestudies.twig, which is based off but different to single.twig (which I use for blog posts) and needs a different layout. I use additional ACF's in the casestudies. I have no idea how I go about this, or if it is even possible, or if there is a better way, such as custom post types. I've seen lots of posts about custom post types, but they only mention a list of posts, and not the actual display of the custom post itself. Thanks in advance.







      wordpress twig timber






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 2 at 16:00







      doifeellucky

















      asked Jan 2 at 14:23









      doifeelluckydoifeellucky

      255




      255
























          1 Answer
          1






          active

          oldest

          votes


















          2














          I don't think Timber has this functionality by default, but you could add it yourself to single.php like this:



          $context = Timber::get_context();
          $context['post'] = new TimberPost();
          $template = has_category('casestudies') ? 'single-casestudies.twig' : 'single.twig';

          Timber::render($template, $context);


          The $template variable checks if the post contains the category casestudies, and sets the template to either single-casestudies.twig or single.twig.






          share|improve this answer
























          • Hi Daniel. That's great, works perfectly, much appreciated. If I wanted to add a second category what's the syntax for that? If I repeat the third line and replace 'casestudies' with 'news' that works fine but then case studies no longer works and falls back to the defauly single.twig.

            – doifeellucky
            Jan 3 at 8:29













          • Hi Daniel, in the end I was able to achieve this using if, elseif and else. Thanks again for your help.

            – doifeellucky
            Jan 3 at 9:18











          • @doifeellucky Your solution will work fine. Just keep in mind that the order of the if statements will determine which template is used (because a post can have both 'casestudies' and 'news' as categories). Don't forget to mark your question as answered if everything is clear :)

            – Daniel Post
            Jan 3 at 21:24











          • I did think about the fact that they might use multiple categories. Is there a better way expanding on your original solution? I'd expect there to be multiple categories like case studies, news, blog, etc. Thanks.

            – doifeellucky
            Jan 4 at 9:33













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54008031%2fdisplay-a-custom-single-twig-for-specific-categories%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          I don't think Timber has this functionality by default, but you could add it yourself to single.php like this:



          $context = Timber::get_context();
          $context['post'] = new TimberPost();
          $template = has_category('casestudies') ? 'single-casestudies.twig' : 'single.twig';

          Timber::render($template, $context);


          The $template variable checks if the post contains the category casestudies, and sets the template to either single-casestudies.twig or single.twig.






          share|improve this answer
























          • Hi Daniel. That's great, works perfectly, much appreciated. If I wanted to add a second category what's the syntax for that? If I repeat the third line and replace 'casestudies' with 'news' that works fine but then case studies no longer works and falls back to the defauly single.twig.

            – doifeellucky
            Jan 3 at 8:29













          • Hi Daniel, in the end I was able to achieve this using if, elseif and else. Thanks again for your help.

            – doifeellucky
            Jan 3 at 9:18











          • @doifeellucky Your solution will work fine. Just keep in mind that the order of the if statements will determine which template is used (because a post can have both 'casestudies' and 'news' as categories). Don't forget to mark your question as answered if everything is clear :)

            – Daniel Post
            Jan 3 at 21:24











          • I did think about the fact that they might use multiple categories. Is there a better way expanding on your original solution? I'd expect there to be multiple categories like case studies, news, blog, etc. Thanks.

            – doifeellucky
            Jan 4 at 9:33


















          2














          I don't think Timber has this functionality by default, but you could add it yourself to single.php like this:



          $context = Timber::get_context();
          $context['post'] = new TimberPost();
          $template = has_category('casestudies') ? 'single-casestudies.twig' : 'single.twig';

          Timber::render($template, $context);


          The $template variable checks if the post contains the category casestudies, and sets the template to either single-casestudies.twig or single.twig.






          share|improve this answer
























          • Hi Daniel. That's great, works perfectly, much appreciated. If I wanted to add a second category what's the syntax for that? If I repeat the third line and replace 'casestudies' with 'news' that works fine but then case studies no longer works and falls back to the defauly single.twig.

            – doifeellucky
            Jan 3 at 8:29













          • Hi Daniel, in the end I was able to achieve this using if, elseif and else. Thanks again for your help.

            – doifeellucky
            Jan 3 at 9:18











          • @doifeellucky Your solution will work fine. Just keep in mind that the order of the if statements will determine which template is used (because a post can have both 'casestudies' and 'news' as categories). Don't forget to mark your question as answered if everything is clear :)

            – Daniel Post
            Jan 3 at 21:24











          • I did think about the fact that they might use multiple categories. Is there a better way expanding on your original solution? I'd expect there to be multiple categories like case studies, news, blog, etc. Thanks.

            – doifeellucky
            Jan 4 at 9:33
















          2












          2








          2







          I don't think Timber has this functionality by default, but you could add it yourself to single.php like this:



          $context = Timber::get_context();
          $context['post'] = new TimberPost();
          $template = has_category('casestudies') ? 'single-casestudies.twig' : 'single.twig';

          Timber::render($template, $context);


          The $template variable checks if the post contains the category casestudies, and sets the template to either single-casestudies.twig or single.twig.






          share|improve this answer













          I don't think Timber has this functionality by default, but you could add it yourself to single.php like this:



          $context = Timber::get_context();
          $context['post'] = new TimberPost();
          $template = has_category('casestudies') ? 'single-casestudies.twig' : 'single.twig';

          Timber::render($template, $context);


          The $template variable checks if the post contains the category casestudies, and sets the template to either single-casestudies.twig or single.twig.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 2 at 16:40









          Daniel PostDaniel Post

          867




          867













          • Hi Daniel. That's great, works perfectly, much appreciated. If I wanted to add a second category what's the syntax for that? If I repeat the third line and replace 'casestudies' with 'news' that works fine but then case studies no longer works and falls back to the defauly single.twig.

            – doifeellucky
            Jan 3 at 8:29













          • Hi Daniel, in the end I was able to achieve this using if, elseif and else. Thanks again for your help.

            – doifeellucky
            Jan 3 at 9:18











          • @doifeellucky Your solution will work fine. Just keep in mind that the order of the if statements will determine which template is used (because a post can have both 'casestudies' and 'news' as categories). Don't forget to mark your question as answered if everything is clear :)

            – Daniel Post
            Jan 3 at 21:24











          • I did think about the fact that they might use multiple categories. Is there a better way expanding on your original solution? I'd expect there to be multiple categories like case studies, news, blog, etc. Thanks.

            – doifeellucky
            Jan 4 at 9:33





















          • Hi Daniel. That's great, works perfectly, much appreciated. If I wanted to add a second category what's the syntax for that? If I repeat the third line and replace 'casestudies' with 'news' that works fine but then case studies no longer works and falls back to the defauly single.twig.

            – doifeellucky
            Jan 3 at 8:29













          • Hi Daniel, in the end I was able to achieve this using if, elseif and else. Thanks again for your help.

            – doifeellucky
            Jan 3 at 9:18











          • @doifeellucky Your solution will work fine. Just keep in mind that the order of the if statements will determine which template is used (because a post can have both 'casestudies' and 'news' as categories). Don't forget to mark your question as answered if everything is clear :)

            – Daniel Post
            Jan 3 at 21:24











          • I did think about the fact that they might use multiple categories. Is there a better way expanding on your original solution? I'd expect there to be multiple categories like case studies, news, blog, etc. Thanks.

            – doifeellucky
            Jan 4 at 9:33



















          Hi Daniel. That's great, works perfectly, much appreciated. If I wanted to add a second category what's the syntax for that? If I repeat the third line and replace 'casestudies' with 'news' that works fine but then case studies no longer works and falls back to the defauly single.twig.

          – doifeellucky
          Jan 3 at 8:29







          Hi Daniel. That's great, works perfectly, much appreciated. If I wanted to add a second category what's the syntax for that? If I repeat the third line and replace 'casestudies' with 'news' that works fine but then case studies no longer works and falls back to the defauly single.twig.

          – doifeellucky
          Jan 3 at 8:29















          Hi Daniel, in the end I was able to achieve this using if, elseif and else. Thanks again for your help.

          – doifeellucky
          Jan 3 at 9:18





          Hi Daniel, in the end I was able to achieve this using if, elseif and else. Thanks again for your help.

          – doifeellucky
          Jan 3 at 9:18













          @doifeellucky Your solution will work fine. Just keep in mind that the order of the if statements will determine which template is used (because a post can have both 'casestudies' and 'news' as categories). Don't forget to mark your question as answered if everything is clear :)

          – Daniel Post
          Jan 3 at 21:24





          @doifeellucky Your solution will work fine. Just keep in mind that the order of the if statements will determine which template is used (because a post can have both 'casestudies' and 'news' as categories). Don't forget to mark your question as answered if everything is clear :)

          – Daniel Post
          Jan 3 at 21:24













          I did think about the fact that they might use multiple categories. Is there a better way expanding on your original solution? I'd expect there to be multiple categories like case studies, news, blog, etc. Thanks.

          – doifeellucky
          Jan 4 at 9:33







          I did think about the fact that they might use multiple categories. Is there a better way expanding on your original solution? I'd expect there to be multiple categories like case studies, news, blog, etc. Thanks.

          – doifeellucky
          Jan 4 at 9:33






















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54008031%2fdisplay-a-custom-single-twig-for-specific-categories%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          MongoDB - Not Authorized To Execute Command

          in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

          Npm cannot find a required file even through it is in the searched directory