Displaying Custom Content On Different Pages All Using The Same Template - php

This is my first Wordpress build so bear with me whilst I try to explain the issue.
I'm building a site for a hotel. 3 of the pages of the site all feature the same content (a heading, a paragraph of text and a gallery) and all feature a 'box', the content of which differs slightly from page to page. For example, on the 'accommodation' page the box will simply have paragraph text in, but on the 'restaurant' page the box will have links to downloadable PDF menus.
I've built a custom template file for these pages called 'standard-page'. My question is, what would be the best way to tackle this box issue? I'm guessing there is an easier way than creating a separate template for every page? The box does need to be content editable, so for each page the backend fields will differ (the restaurant page will require a file upload facility for example).
I've been following a Treehouse tutorial so far, but this isn't really covered in it. I've been using Custom Fields if that helps.
The only idea I can think of is having the box as a separate file (e.g. content-box) and having a WP_Query which calculates and displays different fields based on the page.
Here is an example of the different style boxes. The one with the offer is for the homepage.

using a content-box.php would probably be your best bet and inside it - target the pages by name.
inside of the content-box.php:
<?php if(is_page('accommodation')) {//show accommodation box}?>
<?php elseif(is_page('restaurant')) {//show restaurant box }?>
etc.

Related

Wordpress - Show sections of content

I have a problem with my custom Wordpress theme and need a solution.
The goal is to create an about us page which has multiple sections. Let's say the page has to have 4 sections.
Short info about the company
Short info about the company history
Info about how the company works with clients
Section about the team.
I want this info to come from the Wordpress "page" itself.
The dream scenario for the "client" would be that in the "pages" -> editor, there would be 4 different sections with content that can be dynamically changed at any point. How do i do that?
There has to be content between the different sections. Different HTML containers to each section so they can be seperately styled with CSS. But at this point all i see is the_content(), which can show the content on the "page" itself.
But obviously the code can't be like this. But you get the idea i hope =D
<div class="section1">
<?php the_content();?>
</div>
<div class="between-sections"></div>
<div class="section2">
<?php the_content();?>
</div>
So how do i show different sections of the content on the page itself on specific places. So the only thing an admin has to do is go to "pages" -> the page -> edit the different sections.
I know a solution would be to make the pages empty and instead show the content with customize settings and sections. I could add a Section in Wordpress with the name "About us" and there have the different content / images that can be changed at any point. The problem might be that it's not located in the pages where people might expect all the content to be.
Another solution is different meta boxes where you would show the different meta boxes under the editor and have them named after their places on the page. Then show the different meta boxes on the page. But i feel like meta boxes is more for shorter stuff like email / phone number / or smaller one sentenced data.
Another solution might be custom fields but again i feel like it should be smaller stuff instead of large amount of text. But this is the solution i am leaning towards.
So is there any way of doing this in the custom theme without page builders? Or maybe a whole other solution. I am not 'new' to Wordpress but this has been on my mind for a while now and i can't seem to find a solution that makes sense.
The only sensible solution in my head would be to create custom fields to the pages with first_section, second_section etc. and then post the content there.
Follow the following steps:
1. create the Template and inside the template create the four different section http://prntscr.com/pzi0io
Use ACF to create four Content Editor for the different section https://www.advancedcustomfields.com/
By default wordpress supports only one content edit block for pages, articles etc. I'd say the easiest solution is to add the other edit blocks as custom fields. And the easiest tool for this is Advanced Custom Fields plugin which has long list of different content type fields (including WYSIWYG like default edit block) and you can define where to add them, ie. in this case for the default page template.
To make it dynamic I would use the ACF Repeater field with predefined classes-dropdown and a WYSIWYG. So the end user can add as many content blocks as they want with different predefined styles. You just need to loop through the repeater field in the template and add the selected style class & content to the page.
End user view on the edit page:
Default edit block
Repeater field >> Add row
-- Select class (like "black", "white", "blue") & type content
The idea on the page template:
foreach($repeater as $row) {
echo '<div class="content-block content-'.$row->class.'">'.$row->content.'</div>';
}
And the result code would be like (with two rows created):
<div class="content-block content-black">
Test text, images, links, stuff
</div>
<div class="content-block content-blue">
Test text2, images2, links2, stuff2
</div>
It's also possible to create the custom fields with code by yourself on the theme but with ACF it's so much easier so I'd recommend using it.

How to make a dynamic wordpress page (custom control/page template box)

I have completed numerous wordpress template tutorials focusing on implementing multiple pages, and a custom dynamic navigation. The only issue I have is finding a source on creating a custom template box. For example, my goal is to customize the page template box following the mock up. The content placement is done in php, and designed in css. That way I dont have to add content in php but instead add it in wordpress based on my custom design. I have not found any source to achieve this. Does anyone have a source that displays how to create custom template boxes(the dynamic part)?
Links below is what I used to create my wordpress site
https://www.smashingmagazine.com/2013/05/migrate-existing-website-to-wordpress/
custom navbar for wordpress using bootstrap
I tried another source called Dynamic Page Templates in WordPress, Part 2 which suppose to explain how to create custom controls/template boxes. Following the link was unclear, resulting in not achieving the goal.
Thanks,
There are many ways to create a dynamic page in WordPress. I am sharing one of it. Install Advanced Custom Field Plugin and read its documentation. It allows page content to be changed from back-end (Its great for text/images). For areas where you want to user to choose whatever custom widget she wants to choose, add a widget area following any custom widget tutorial.

What is the best way to bypass Wordpress's HTML sanitization so I can use bootstrap html in Wordpress?

I'm trying to get on developing websites on Wordpress for easier management for the future admins.
I have created a bootstrap theme on my own. I got all the pages smoothly into Wordpress and are now easy to edit. However I have this one page which has tables, buttons, glyphicons and once I click the "graphic" view while editing the page it breaks it.
So I was thinking that that won't do and I decided to create a new plugin to fix this problem. A simple plugin which has few textboxes on the admin settings page and then just prints the texts on the page where I want them to go.
However I tried to look for tutorials and I'm just overwhelmed. I have gotten my plugin to work somewhat. It has a admin setup page and it can print customized text to a page where my shortcode is. Thanks to this post!
However, I still don't know what to do from this point on.
My question is: Could you help me by giving me an example code to get the same kind of textbox to my plugin's admin page as I have while editing/adding new pages (with graphic view) which saves the text and displays it on a page.
I have no idea if this is the way to go but I can't think of anything else right now. I was thinking that the settings page has a couple of textboxes and the text in them goes where I want it to go on a page. So the future admin won't see the unnecessary html code and therefore can't break it.
I would abandon the "plugin" idea. There are several more elegant ways to do this besides writing a plugin.
By far the easiest to implement is this:
1. Store the html in a "Custom Field" https://codex.wordpress.org/Custom_Fields
2. Display the contents in your page (or post) template https://codex.wordpress.org/Custom_Fields#Displaying_Custom_Fields

How to make WordPress pages where only certain (multiple) areas of content are editable

It has been a few years since I properly worked with WordPress. Now I have a proposal to build a WordPress site where every page has a custom design and only some areas of each page is editable.
The reason for this is to build a bespoke layout on each page which cannot be messed up by someone non-technical editing it in the CMS, except for small areas which they can customise.
e.g. A page contains one div which has some text in it, which can be edited in the WP admin backend, but the rest of the page cannot be edited.
Can this be done? How?
Edit: There needs to be multiple editable areas not just one. I know how to make custom pages/templates.
One method may be to create new page templates. Just create a new file in your main theme folder (or the templates folder if there is one). As long at the top of that file contains the line:
/*
Template Name: <your template name>
*/
You can design the page however you want. The data pulled from the admin section will go wherever you invoke
the_content();
The rest of the page can be hardcoded.
Then on the post edit page, on the right side (usually), you can choose the template with your template name for that page. It may be a good idea to copy the current post.php or single.php into your custom file and work from there.
For restricting access you can look at setting up user levels and keep your content contributors as "Authors" instead of "admins" so they can't change themes or edit settings.
(See https://codex.wordpress.org/User_Levels)
For creating specific unique pages with an area that gets changed you should look into custom Page Templates. You can create a page template by dropping a php file with the right naming structure into your theme hierarchy and it will get picked up by the back-end as template option when you create a page.
(See https://developer.wordpress.org/themes/template-files-section/page-template-files/page-templates/)
I solved this problem using a plugin called Advanced Custom Fields which does exactly what I required.

Widget or Plug-in? Building a theme and unsure how to approach optional content

I have a question regarding how best to build a widget/plug-in for WordPress.
I have tried to code this thing out but unfortunately to no avail but that's because I'm increasingly thinking I'm going about it the entirely wrong way.
This is what I'm trying to achieve:
I have built a theme from scratch. In this theme I have two content areas. The larger of the two contains the main content and the smaller of the two (proposed widget/plug-in) contains supplementary information (opening times, Facebook feed). I don't want this widget/plug-in to appear on every page and would like the ability to turn it on or off within the usual WordPress admin area.
The impression I am getting is that I can build the widget that contains the content I want (with necessary forms to change said content) but it is either on for ALL pages or off for ALL pages, no sort-of page specific functionality. That's where I'm thinking a plug-in would extend the functionality and allow me to be more page specific with it. That is my current set-up with a theme I'm running and plug-in I added (neither of which were coded by me) but I would like to repeat it as best as possible.
Would it be right to assume that widgets are not really designed to have page specific functionality and that function should be left down to a plug-in?
I appreciate this question asks nothing really in the way specific advice but I would just like an opinion or two on the best approach before I go away and create it - this is my main struggle at the moment.
Thanks for looking.
This can be done with the Jetpack plugin. Once activated you can choose what widgets display on what pages:
The Widget Visibility module enables you to configure widgets to appear only on certain pages (or be hidden on certain pages) by using the Visibility panel.
Visibility is controlled by five aspects: page type, category, tag, date, and author. For example, if you wanted the Archives widget to only appear on category archives and error pages, choose “Show” from the first dropdown and then add two rules: “Page is 404 Error Page” and “Category is All Category Pages.”
Originally posted here
It is also possible with the Display Widgets plugin.
This is a common problem. The usual remedy is to use an existing plugin that allows widgets to be tied to pages.
The 3 most popular plugins for this are:
JetPack
Per Page Widgets
Widget Logic

Categories