Create entire content block in Advanced Custom Fields - php

I am new to ACF and am trying to understand how to create entire sections of content.
I can't seem to find any answers to this, though I am perhaps not phrasing the question correctly because of my limited understanding.
I have created my first website using ACF and Gutenberg where I have a separate php template for each block of content, which is then registered as a Gutenberg block. This works perfectly in that the content blocks can contain entire blocks of html/php and are always available but only appear on the page when they are chosen, making it very user friendly.
What I am struggling to understand is how you can insert an entire section WITHOUT Gutenberg - say using a shortcode.
At the moment if I have a structure like so:
<section class="reusable-block">
<div class="title-box">
<?php if( get_field('main_heading') ): ?>
<h1 class="hero-heading"><?php the_field('main_heading'); ?></h1>
<?php endif; ?>
</div>
</section>
If I use the shortcode [acf field="main_heading"] It will obviously output the contents of that field but how do I enter a shortcode that would enter the entire section on a needs basis across any page?
If I hard code it into the page template then the fields are always visible on the WordPress backend page, regardless fo wether they are being used, though if that's the way it has to be then I'll work with.
Unfortunately I need to use ACF with a page builder for a current project (I do not have a choice on this). This is the reason I cannot use Gutenberg and register blocks and will need to use shortcodes.
In short, is there a way to create an entire ACF block that would then contain html and acf fields, and even better that could be built in a separate PHP file as you would when using this in conjunction with Gutenberg.
I have watched about 3 courses online and read lots of resources, but this one thing still seems to allude me.

You already mentioned one possibility yourself: creating a custom shortcode for this.
Therefore, you either need to create a new plugin, or, append this to your (child) theme's functions.php:
function shortcodeHeadingBlock($attr, $content = null) {
if (! class_exists('ACF') ) { // will not work without ACF
return '';
}
$opts = shortcode_atts([
'field' => 'main_heading',
], $attr);
ob_start();
?>
<section class="reusable-block">
<div class="title-box">
<?php if( get_field($opts['field']) ): ?>
<h1 class="hero-heading"><?php the_field($opts['field']); ?></h1>
<?php endif; ?>
</div>
</section>
<?php
return ob_get_clean();
}
function setupShortcodes() {
add_shortcode('headingblock', 'shortcodeHeadingBlock');
}
add_action('plugins_loaded', 'setupShortcodes');
After this, you can use the shortcode
[headingblock]
anywhere across your page where shortcodes are interpreted. The shortcode first checks if the ACF-class is available which implies that the ACF functions should be available, too.
The shortcode also has an optional field attribute that defaults to 'main_heading', meaning you can also do
[headingblock field="another_acf_field_name"].
It then returns your code template and inserts the desired ACF field.
Just in case your .title-box CSS isn't available in the admin interface, you might have to enqueue it separately using the admin_enqueue_scripts hook. Regarding CSS, you will also have to make sure the full selector for .title-box doesn't contain additional classes/objects that might no longer apply when the shortcode places it into other places on the page.

Related

Drupal - Clean up exisiting code that converts text into an image

I've created the following snippet of code within a custom content.tpl.php template
<?php
$field = field_get_items('node', $node, 'field_youtube_video_link');
if(!empty($field)): ?>
<div class="youtube-popup">
<?php
print "<a class=youtube-play cboxElement href=".$content['field_youtube_video_link']['#object']->field_youtube_video_link['und'][0]['url'].">".$img."</a>";?>
</div>
<?php endif; ?>
I'm really wanting to clean this up and rather than do all of this in the content-type.tpl.php I would prefer to do this through a preprocess hook and then create a custom theme template for it, but I'm unsure of how to achieve this?
You can style by fields. Check out:
https://www.drupal.org/docs/7/theming/template-theme-hook-suggestions
Theme hook suggestions are made based on these factors, listed from
the most specific template to the least. Drupal will use the most
specific template it finds:
field--field-name--content-type.tpl.php
field--content-type.tpl.php
field--field-name.tpl.php
field--field-type.tpl.php

Styling separate elements on a page in Wordpress

Using Wordpress I am designing a theme for a client's website.
I have set the landing/home page to a static page, and I'm using front-page.php as the default template for this.
My question is how can I style individual elements on the page when all of the page content is called in at once using <?php get_template_part( 'template-parts/content', get_post_format() ); ?>
Currently I multiple <div>'s in the WP page content editor like so:
However, I'm concerned that my client or something else, may delete this content by accident and completely spoil the design. How can I separate content and style it accordingly in my template?
My question is how can I style individual elements on the page?
There are always body classes you can use for different pages/posts.
How can I separate content and style it accordingly in my template?
There are few options depend on what you need:
Turn off "Visual" editor mode for each user account. It sort of helps.
Move the custom HTML into the template files. If that's OK.
Use custom fields, preferably with Advanced custom fields plugin.
For every page,if you need the alone class then function :body_class() : is sufficient.Syntax :- < body < ?php body_class(); ?> > and you can also implement the another function : post_class : for the loop class.Syntax :- < body < ?php post_class(); ?> >

Show full post (not excerpt) for defined posts in wordpress

I'd like to have the possibility to define some posts in wordpress where the full post is shown and not an excerpt.
There are various solutions like:
Use a hidden html to declare this and code into the theme to either use the_content or the_excerpt
Hardcode into the theme (if postid == xx then the_content else the_excerpt)
Use post meta data and add "if" into theme to check for them
Create a plugin which adds the functionality automatically and also a checkbox "Always show full" into the post-editor.
The first one is easy but ugly, 2nd one should be doable with some googling but the 3rd one seems to be the most appealing to me but I have no idea how to achieve this.
Since usually in the template all i have is somewhere the_excerpt method from wordpress. I therefore should somehow inject some code there and check if the checkbox for the current post is set and then just use the_content instead. Is this possible at all or do I need to modify the theme anyway?
Thanks for your inputs.
I would use custom fields, it's more flexible, so you don't need to hard code the page ids.
<?php
$show_full_content = get_post_meta($post->ID, 'show_full_content', true);
if ($show_full_content == 'yes') {
the_content();
} else {
the_excerpt();
}
?>
You might be interested in ACF, it can make it more user friendly.

Adding custom fields to the very bottom of the page in WordPress

I've made a template for specific pages of our website that display similar content, yet are different form the rest of the pages of the website - here's an example. I have several custom fields that I use to include all of the items you see in the boxes that float to the left and right.
My problem is, I also have pricing that's included from a custom field - and because I want it at the bottom of the page, it actually appears outside the content to work (see the bottom of my example given above). Is there a way I can call the content and tell it to display these custom fields at after the content is displayed?
Obviously the following doesn't work, but just to give you an idea:
<?php the_content(get_post_meta($post->ID, "Pricing_Mexico", true); ?>
Additionally, there are actually a lot of custom fields that need to be displayed, so if it were possible to include them in bulk (including the markup which includes divs and what-not) that would be preferred. Thanks!
You could filter the_content() so in your functions file:
function contentFilter($content){
// Output the current content
echo $content;
// Output the meta field
echo get_post_meta($post->ID, "Pricing_Mexico", true);
}
add_filter('the_content', 'contentFilter');
You could echo as many meta fields as you need inside the function.

wordpress post/page content in code

i am trying to insert a post/page into one of my themes files and it wont display shortcodes or php
i have created a page called home in the wordpress admin-paned and inserted into my code the following:
<div id="home_page"> <!-- echos the content from the page "home" id:43 -->
<?php $home_id = 43;
$home_page = get_page( $home_id );
?>
<?php echo $home_page->post_content; ?>
</div> <!-- end #home_page -->
and non of the shortcodes that i have in the page work.
i installed a php in post or page and tried useing php and it doesnt work.
when i insert
echo do_shortcode('[youtube_sc url=http://www.youtube.com/watch?v=Db3XGpt6nNU]');
directly into the code it works.
does anyone know y this happens?
thank you.
I got an answer in wordpress.stackexchange.com
I Quote:
You need to apply the filter the_content e.g.:
<?php echo apply_filters('the_content',$home_page->post_content); ?>
Also, you don't need custom shortcodes for youtube, just put the URL in the content (but not a hyperlink), and it'll be swapped out for a youtube player at runtime. No plugins or extra code needed thanks to oembed.
Thank You Tom J Nowell
The $home_page->post_content value is the exact post content as stored in the database. Echoing this does not give any of your shortcodes a chance to run.
You should use a "WordPress Loop" to display the content, as this sets things up to allow you to use template tags such as the_title() and the_content() - this will call the processing functions for shortcodes and other functions like wpautop() that "massage" post content for output.
If you don't want to use a Loop, you could output the content using
echo do_shortcode($home_page->post_content);
This will run the post content through the shortcode processor, giving the shortcodes a chance to run.
For more on how WordPress "massages" post content, you can look here: http://codex.wordpress.org/How_WordPress_Processes_Post_Content

Categories