Tagged: get_template_part
- This topic has 22 replies, 2 voices, and was last updated 6 years, 6 months ago by
giec.
-
AuthorPosts
-
-
July 22, 2015 at 7:11 am #4045
giecMemberHi, i’ve followed the instructions and created the folder structure as follow:
twentytwelve-child –> piklist–>parts–>meta-boxes and this is my my-fields.php file (which is just a test to see if I can get the metabox):<?php /* Title: My custom fields Post Type: page */ piklist('field', array( 'type' => 'text' ,'field' => 'field_one' ,'label' => 'First Field' )); ?>What I want to do is to give users, when they create a page in wordpress, the possibility of adding one or more repeatable divs, but I don’t seem to be able to see this metabox
Any idea? -
July 22, 2015 at 7:45 am #4046
giecMemberOK I think I managed to get it to display, but how do I change the sample code to get a div with a class? I had a look at the field parameters page https://piklist.com/user-guide/docs/field-parameters/ but there is nothing about divs. Any idea?
-
July 22, 2015 at 11:44 am #4048
-
July 22, 2015 at 12:47 pm #4053
SteveKeymasterI just read your post on Stack Exchange, and I think I have a better understanding of what you want.
It would be best to use the WordPress function get_template_part() to bring in your divs. This is how I would do it.
YOUR PIKLIST PARTS FILE (my-fields.php)
This is where your users select the Divs they want and can reorder them.piklist('field', array( 'type' => 'select' ,'field' => 'my_divs' ,'label' => __('Select a Div') ,'add_more' => true ,'choices' => array( 'div_one' => 'Div One' ,'div_two' => 'Div Two' ,'div_three' => 'Div Three' ) ));YOUR THEME FILE (PAGE TEMPLATE)
Your page template should look something like this:ID, 'my_divs'); // Get the field data foreach ($my_divs as $my_div => $name) // loop through each add_more { get_template_part('custom-div', $name); // display the template } ?>Then you would name your divs (template parts) this, to match the dropdown values:
custom-div-div_one.php custom-div-div_two.php custom-div-div_three.phpDoes that make sense?
-
July 23, 2015 at 3:16 pm #4054
giecMemberHi steve, thanks for taking the time to write the code. I think it makes sense to an extent (the problem is also my limited understanding of PHP at this stage as I literally just started with it) in any case, I will apply the code you gave me and see how I get on with it, i’ll post back if I get seriously stuck!
thanks again -
July 23, 2015 at 4:12 pm #4056
SteveKeymasterFeel free to zip up your child theme and email to [email protected] if you want us to take a look. Always happy to help.
-
July 24, 2015 at 9:20 am #4061
giecMemberVery nice of you, I’ll see how I get on. One more question of a more general nature about the plugin if I may. As you gathered from my previous question, allowing user to add functional markup without knowing any HTML from the backend, is one of the main reasons why I downloaded the plugin. I’ll also need users to be able to change things like CSS styles (using always the same method of selecting/deselecting boxes/radio buttons at the backend)and perhaphs, if possible, make some changes to jquery functionalities, like er, change the speed of animations etc. Does this plugin allow this?
thanks -
July 24, 2015 at 9:47 am #4062
giecMemberOK, so I’ve done some work implementing your code. One thing I’m not too sure about – and sorry for being a complete noob – but you know the 3 divs you said they should match the dropdown values:
custom-div-div_one.php custom-div-div_two.php custom-div-div_three.phpThose presumably are partial templates, aren’t they? Should they go together with my custom template in the page-template folder? More importantly, what code goes in them? Presumably
<div class="infoBox"></div>but if so, I don’t need three separate partial templates because the divs inside will always be the same, I just want the users to have the possibility to choose how many they want to add if that makes sense
thanks -
July 24, 2015 at 10:02 am #4063
SteveKeymasterI thought you wanted different partials, but if you only have one, that’s fine too. If you ever want to add different partials this code is easy to expand.
Your partials can go in the root directory of your theme or in a folder if you like. You would just need to tell
get_template_partto look in a folder.ROOT:
get_template_part('custom-div', $name);
DIRECTORY NAMED PARTIALS:get_template_part('partials/custom-div', $name);Here’s a good tutorial on get_template_part().
Does that make sense.
-
July 24, 2015 at 10:10 am #4064
giecMemberThanks Steve, I think it makes sense yes, and thanks for the tutorial, I will read it with interest and i’ll keep implementing your solution. Thanks!
-
July 24, 2015 at 5:11 pm #4071
giecMemberHi Steve, I’ve just implemented your solution, and I do get the select box with the add/remove buttons as per screenshot http://s1.postimg.org/qxcifziz3/wordpress_backend.png but I don’t seem to be able to add any div to the editor. Here is the code:
my-fields.php<?php /* Title: My Metabox Description: My cool new metabox Post Type: page */ // Let's create a text box field piklist('field', array( 'type' => 'select' ,'field' => 'my_divs' ,'label' => __('Select a Div') ,'add_more' => true ,'choices' => array( 'div_one' => 'Div One' ) )custom-div-div_one.php
<?php <div class="infoBox one"></div> ?>test-templ.php
<?php /* Template Name: New Test page*/ ?> <?php get_header(); ?> <?php $my_divs = get_post_meta($post->ID, 'my_divs'); // Get the field data foreach ($my_divs as $my_div => $name) // loop through each add_more { get_template_part('custom-div', $name); // display the template } ?> <?php get_footer(); ?>Am I missing something, shouldn’t the plugin take care of that of do I have to write the code?
thanks
A -
July 24, 2015 at 9:07 pm #4072
SteveKeymasterYour code for
custom-div-div_one.phpis incorrect. You can’t wrap a div within php tags. just change it to this:<div class="infoBox one"></div> -
July 25, 2015 at 4:38 pm #4078
giecMemberAh, apologies for that. I’ve made the changes, but still nothing, in the sense that I can add and remove divs at the back end, but I can’t get the divs to appear dynamically (when I add one for example) in the text editor. Happy to zip up everything if it is easier, whatever you think it’s best.
cheers -
July 26, 2015 at 11:37 am #4079
SteveKeymasterDo you want these divs to appear on the front end or in the admin area?
-
July 27, 2015 at 3:13 pm #4082
giecMemberwell, both, sorry maybe I didn’t explain things correctly.
Basically I wanted to add, at the backend, the functionality to allow me to add HTML tags to the front end by using select boxes etc. So, say, I want to have two divs to be added to his page: I’ll use a select box (which I have added to the backend using this plugin) to allow me to add one or more divs to the text editor at the backend, and when I click “update” these divs will be added onto the page (front end).If you look at my screenshot here http://s1.postimg.org/qxcifziz3/wordpress_backend.png you’ll see the select boxes just below the text editor: when I’m happy with the number of divs I want to add to my page, there will be a mechanism – an “Add” button perhaps – that if pressed will add the number of specified divs to the text editor and consequently to the actual page.
In essence I want to use this plugin to customize the back end so that an hypothetical user, who knows no HTML, will be able to rely on my customization (select boxes and whatever else) to add code to his page, allowing wordpress to act as a proper CMS.
I hope it is clearer now, and sorry if it wasn’t before. -
July 27, 2015 at 3:44 pm #4083
SteveKeymasterThis code will add a repeatable textarea where the user can add code. Then pull this field, and loop through it to display:
piklist('field', array( 'type' => 'textarea' ,'field' => 'my_divs' ,'label' => __('Add your code') ,'add_more' => true )There is no need for an “Add” button.
-
July 27, 2015 at 4:20 pm #4084
giecMemberThanks Steve, I tried that,but it breaks my text editor, in that the tool bar that is supposed to appear on the top of the text editor has disappeared and the text I type in it is invisible, here is a screenshot http://postimg.org/image/m7mx5hnvv/
I hope what I said made some sense Steve, that’s how I envisaged to use the plugin, but obviously I don’t know whether it will allow me to customize the backend in the way described. I mean adding select boxes, radio buttons etc is exactly what I need, but I have to have the possibility to make them do something – mainly allow users to add content to the front end pages, a bit like drupal I presume
-
July 27, 2015 at 4:37 pm #4085
SteveKeymasterThe image link is broken. Please repost.
-
July 28, 2015 at 2:27 pm #4091
giecMemberI think the site was down yesterday, I checked the image again and it works OK now, thanks
-
July 28, 2015 at 9:02 pm #4092
-
July 29, 2015 at 4:41 am #4093
giecMemberI would have thought the metabox was there because I’ve added a metabox folder inside the pklist folder in my theme, as per the tutorial. Aside from that, does it make sense what I’m trying to achieve with this plugin?
-
July 29, 2015 at 7:36 am #4094
SteveKeymasterI’m still not clear. Happy to chat on Skype if you like. Email me at [email protected] to schedule a time.
-
July 30, 2015 at 4:44 pm #4097
giecMemberAh, OK cool, I will do in a few days as I’m away now
thanks
-
-
AuthorPosts
- You must be logged in to reply to this topic.