I built a custom block using ACF acf_register_block_type(); function
it works fine in the WordPress dashboard (block editor), it shows the field data and all good..
but in the front-end (view page) the fields are NULL
any help on how to display field data in the front-end??
functions.php code:
add_action('acf/init', 'my_acf_init_block_types');
function my_acf_init_block_types()
{
// Check function exists.
if (function_exists('acf_register_block_type')) {
// register a hero block.
acf_register_block_type(array(
'name' => 'banner',
'title' => __('Banner'),
'description' => __('A custom hero block.'),
'render_callback' => 'my_acf_block_render_callback',
'category' => 'home-sections',
'icon' => 'cover-image',
'keywords' => array('basic', 'banner')
));
}
}
function my_acf_block_render_callback($block)
{
// convert name ("acf/hero") into path friendly slug ("hero")
$slug = str_replace('acf/', '', $block['name']);
// include a template part from within the "template-parts/block" folder
if (file_exists(get_theme_file_path("/template-parts/blocks/content-{$slug}.php"))) {
include(get_theme_file_path("/template-parts/blocks/content-{$slug}.php"));
}
}
function wpdocs_mytheme_block_styles()
{
wp_enqueue_style('admin-css', get_stylesheet_directory_uri() . '/build/css/styles.min.css', '', '0.0.3');
wp_enqueue_script('admin-js', get_template_directory_uri() . '/build/js/scripts.min.js', array('jquery'), '0.0.3', true);
}
add_action('enqueue_block_editor_assets', 'wpdocs_mytheme_block_styles')
banner.php code:
<?php
$title = get_field('main_title');
$img = get_field('background_image');
$button = get_field('button');
?>
<section id="banner-section" style=" background-image:linear-gradient( rgba(67, 74, 122, 0.5), rgba(67, 74, 122, 0.5) ), url('<?= $img['url']; ?>');">
<div class="container">
<div class="row justify-content-center">
<div class="col-12 col-lg-10 text-center">
<h1 class="basic-slider-heading"> <?php echo $title ?> </h1>
<?php
if ($button) :
$button_url = $button['url'];
$button_title = $button['title'];
$button_target = $button['target'] ? $button['target'] : '_self';
?>
<button class="btn btn-shadow-malibu btn-bg-malibu-gradient bg-malibu-accent">
<?= $button_title ?>
</button>
<?php endif; ?>
</div>
</div>
</div>
</section>
var_dump( get_field('main_title'));
Note: I'm using WordPress v5.7.2 and ACF PRO v5.9.8
This is the documentation of get_field: https://www.advancedcustomfields.com/resources/get_field/
Note that there is an optional second parameter
$post_id (mixed) (Optional) The post ID where the value is saved. Defaults to the current post.
So, if your function works at a place and not at the other, then there are environmental differences between the two pages. It is possible that on the UI no post is being technically selected, in which case you need to pass the id of the post whose field you intend to get.
Related
I have a wordpress shortcode to handle some PHP (for custom fields) for my sidebar. The shortcode is below:
add_shortcode('my_further_information_list','my_further_information_list_func');
function my_further_information_list_func(){
$additional_info_1 = get_field( 'additional_info_1');
$additional_info_2 = get_field( 'additional_info_2');
$additional_info_3 = get_field( 'additional_info_3');
if ( $additional_info_1) {
$html = '<li>'.the_field('additional_info_1').'</li>';
}
if ( $additional_info_2) {
$html = '<li>'.the_field('additional_info_2').'</li>';
}
if ( $additional_info_3) {
$html = '<li>'.the_field('additional_info_3').'</li>';
}
return $html;
}
For some reason, when I use this shortcode, it generates the html in completely the wrong place on the page (in the div above where the shortcode is). I haven't come across this before, so any ideas on why this might happen?
You can see the weird place they're appearing here:
shortcode generating in weird place...
This is the theme sidebar code:
<?php if ( x_get_content_layout() != 'full-width' ) : ?>
<aside class="x-sidebar nano" role="complementary">
<div class="max width nano-content">
<?php if ( get_option( 'ups_sidebars' ) != array() ) : ?>
<?php dynamic_sidebar( apply_filters( 'ups_sidebar', 'sidebar-main' ) ); ?>
<?php else : ?>
<?php dynamic_sidebar( 'sidebar-main' ); ?>
<?php endif; ?>
</div>
</aside>
<?php endif; ?>
This is the generated HTML. As you can see, the top widget (also using a shortcode whose function references custom fields (but in a different way - with a loop - as the fields it's referencing are different)) works fine, as does the bottom (just a simple followed by a button). But in the middle widget, the shortcode generates the link and text above the div that the widget is supposed to be in...
<div id="text-8" class="widget widget_text">
<h4 class="h-widget">Related Articles</h4>
<div class="textwidget">
<p>
<ul>
<li>Related Article 1</li>
<li>Related Article 2</li>
<li>Related Article 3</li>
</ul>
</p>
</div>
</div>
Further Information 1Further Information 2
<div id="text-9" class="widget widget_text">
<h4 class="h-widget">Further Information</h4>
<div class="textwidget"><p><ul><li></li><li></li></ul></p></div>
</div>
<div id="custom_html-4" class="widget_text widget widget_custom_html">
<h4 class="h-widget">Feedback</h4>
<div class="textwidget custom-html-widget">
<p>Please provide feedback!<p>
<button>PROVIDE FEEDBACK</button>
</div>
</div>
i think you code should be
add_shortcode('my_further_information_list','my_further_information_list_func');
function my_further_information_list_func(){
$additional_info_1 = get_field( 'additional_info_1');
$additional_info_2 = get_field( 'additional_info_2');
$additional_info_3 = get_field( 'additional_info_3');
$html = "<ul>";
if ( $additional_info_1) {
$html .= '<li>'.$additional_info_1.'</li>';
}
if ( $additional_info_2) {
$html .= '<li>'.$additional_info_2.'</li>';
}
if ( $additional_info_3) {
$html .= '<li>'.$additional_info_3.'</li>';
}
$html .= "</ul>";
return $html;
}
non valid html elements can cause this type of behavior depending on your css. If you require more assistance please provide template code, or at least the snippet around the_content()
edit: the_field is the same as echo get_field. it can't be used to build a string. that's why you use get_field on top :)
I've been following this post
http://legomenon.io/article/drupal-7-creating-theming-custom-block-content
so i made them accordingly,
i have these, btw name my my custom module is efq_story_list
HOOK_THEME
function efq_story_list_theme() {
return array(
'efq_story_list_function_items' => array(
'variables' => array('items' => NULL),
'template' => 'templates/efq_story_list--block'
),
);
}
FUNCTION GENERATING THE LIST
function __efq_story_list_block_content() {
#SOME CODE HERE and variable $items has the list
return theme("efq_story_list_function_items", array("items" => $items));
}
HOOK_BLOCK_VIEW
The name of the block is opinion
function efq_story_list_block_view($delta = ''){
switch ($delta) {
case 'opinion':
$block['content'] = __efq_story_list_block_content();
break;
}
return $block;
}
Then on my templates/efq_story_list--block i have the file efq_story_list--block.tpl.php.. then i flush all the cache,
but now I get WSOD. I tried this in local machine and its working, so I am wondering what else can I miss in my production site.. Thank you very much.. I am quite new to drupal so I dont know where to look anymore...
EDIT # 1
the content of the template file is
<?php $items = $variables['items']; ?>
<div class="row">
<?php foreach($items as $item): ?>
<div class="col-md-3 news_list panel">
<div class='thumbnail_wrapper'>
<img src="print render($item['image_url']);" />
</div>
<div class="meta small_sprite">
<span class="generic icon source_<?php print render($item['source_class']); ?>"></span>
<span><?php print render($item['source']); ?></span>
</div>
<div class="caption">
<?php print render($item['title']); ?>
</div>
</div>
<?php endforeach;
</div>
Hi I am trying to use the advanced layer slider in my wordpress post page. However my code pasted in the header.php file works only on the pages, not posts, I can see the shortcode showing up though in the top of the posts page. Is there anyway to include the code needed from the plugin easily?
Thanks in advanced,
James
Sorry about the formatting
Single.php
<?php
global $avia_config;
/*
* get_header is a basic wordpress function, used to retrieve the header.php file in your theme directory.
*/
get_header();
$title = __('Blog - Latest News', 'avia_framework'); //default blog title
$t_link = home_url('/');
$t_sub = "";
if(avia_get_option('frontpage') && $new = avia_get_option('blogpage'))
{
$title = get_the_title($new); //if the blog is attached to a page use this title
$t_link = get_permalink($new);
$t_sub = avia_post_meta($new, 'subtitle');
}
if( get_post_meta(get_the_ID(), 'header', true) != 'no') echo avia_title(array('heading'=>'strong', 'title' => $title, 'link' => $t_link, 'subtitle' => $t_sub));
?>
'>
<div class='container template-blog template-single-blog '>
<main class='content units <?php avia_layout_class( 'content' ); ?>' <?php avia_markup_helper(array('context' => 'content','post_type'=>'post'));?>>
<?php
/* Run the loop to output the posts.
* If you want to overload this in a child theme then include a file
* called loop-index.php and that will be used instead.
*
*/
get_template_part( 'includes/loop', 'index' );
//show related posts based on tags if there are any
get_template_part( 'includes/related-posts');
//wordpress function that loads the comments template "comments.php"
comments_template( '/includes/comments.php');
?>
<!--end content-->
</main>
<?php
$avia_config['currently_viewing'] = "blog";
//get the sidebar
get_sidebar();
?>
</div><!--end container-->
</div><!-- close default .container_wrap element -->
Page.php
<?php
global $avia_config;
/*
* get_header is a basic wordpress function, used to retrieve the header.php file in your theme directory.
*/
get_header();
if( get_post_meta(get_the_ID(), 'header', true) != 'no') echo avia_title();
?>
<div class='container_wrap container_wrap_first main_color <?php avia_layout_class( 'main' ); ?>'>
<div class='container'>
<main class='template-page content <?php avia_layout_class( 'content' ); ?> units' <?php avia_markup_helper(array('context' => 'content','post_type'=>'page'));?>>
<?php
/* Run the loop to output the posts.
* If you want to overload this in a child theme then include a file
* called loop-page.php and that will be used instead.
*/
$avia_config['size'] = avia_layout_class( 'main' , false) == 'entry_without_sidebar' ? '' : 'entry_with_sidebar';
get_template_part( 'includes/loop', 'page' );
?>
<!--end content-->
</main>
<?php
//get the sidebar
$avia_config['currently_viewing'] = 'page';
get_sidebar();
?>
</div><!--end container-->
</div><!-- close default .container_wrap element -->
Header.php
<?php
global $avia_config;
$style = $avia_config['box_class'];
$responsive = avia_get_option('responsive_layout','responsive');
$blank = isset($avia_config['template']) ? $avia_config['template'] : "";
$headerS = !$blank ? avia_header_setting() : "";
$headerMenu = $responsive ? avia_get_option('header_menu','mobile_drop_down') : "";
?>
class=" ">
" />
RSS2 Feed" href="" />
" />
';
?>
* tag of your theme, or you will break many plugins, which
* generally use this hook to add elements to such
* as styles, scripts, and meta tags.
*/
wp_head();
?>
'body')); ?>>
<div id='wrap_all'>
<?php if(!$blank){ ?>
<header id='header' class=' header_color <?php avia_is_dark_bg('header_color'); echo " ".$headerMenu; ?>' <?php avia_markup_helper(array('context' => 'header','post_type'=>'forum'));?>>
<?php
if($responsive && $headerMenu == 'mobile_slide_out')
{
echo '<a id="advanced_menu_toggle" href="#" '.av_icon_string('mobile_menu').'></a>';
echo '<a id="advanced_menu_hide" href="#" '.av_icon_string('close').'></a>';
}
$social_args = array('outside'=>'ul', 'inside'=>'li', 'append' => '');
//subheader, only display when the user chooses a social header
if(strpos($headerS,'social_header') !== false)
{
?>
<div id='header_meta' class='container_wrap container_wrap_meta'>
<div class='container'>
<?php
/*
* display the themes social media icons, defined in the wordpress backend
* the avia_social_media_icons function is located in includes/helper-social-media-php
*/
if(strpos($headerS,'bottom_nav_header') === false) avia_social_media_icons($social_args);
//display the small submenu
echo "<nav class='sub_menu' ".avia_markup_helper(array('context' => 'nav', 'echo' => false)).">";
$avia_theme_location = 'avia2';
$avia_menu_class = $avia_theme_location . '-menu';
$args = array(
'theme_location'=>$avia_theme_location,
'menu_id' =>$avia_menu_class,
'container_class' =>$avia_menu_class,
'fallback_cb' => '',
'container'=>'',
'echo' =>false
);
$nav = wp_nav_menu($args);
echo $nav;
$phone = avia_get_option('phone');
$phone_class = !empty($nav) ? "with_nav" : "";
if($phone) echo "<div class='phone-info {$phone_class}'><span>{$phone}</span></div>";
/*
* Hook that can be used for plugins and theme extensions (currently: the wpml language selector)
*/
do_action('avia_meta_header');
echo '</nav>';
?>
</div>
</div>
<?php } ?>
<div id='header_main' class='container_wrap container_wrap_logo'>
<?php
/*
* Hook that can be used for plugins and theme extensions (currently: the woocommerce shopping cart)
*/
do_action('ava_main_header');
?>
<div class='container'>
<?php
/*
* display the theme logo by checking if the default logo was overwritten in the backend.
* the function is located at framework/php/function-set-avia-frontend-functions.php in case you need to edit the output
*/
echo avia_logo(AVIA_BASE_URL.'images/layout/logo.png', false, 'strong');
if(strpos($headerS,'social_header') !== false && strpos($headerS,'bottom_nav_header') !== false) avia_social_media_icons($social_args);
/*
* display the main navigation menu
* modify the output in your wordpress admin backend at appearance->menus
*/
$extraOpen = $extraClose = "";
if(strpos($headerS,'bottom_nav_header') !== false){ $extraClose = "</div></div><div id='header_main_alternate' class='container_wrap'><div class='container'>"; }
echo $extraClose;
echo "<nav class='main_menu' data-selectname='".__('Select a page','avia_framework')."' ".avia_markup_helper(array('context' => 'nav', 'echo' => false)).">";
$avia_theme_location = 'avia';
$avia_menu_class = $avia_theme_location . '-menu';
$args = array(
'theme_location' => $avia_theme_location,
'menu_id' => $avia_menu_class,
'container_class' => $avia_menu_class,
'fallback_cb' => 'avia_fallback_menu',
'walker' => new avia_responsive_mega_menu()
);
wp_nav_menu($args);
echo '</nav>';
/*
* Hook that can be used for plugins and theme extensions
*/
do_action('ava_after_main_menu');
?>
<!-- end container-->
</div>
<!-- end container_wrap-->
</div>
<div class='header_bg'></div>
<!-- end header -->
</header>
<?php } //end blank check ?>
<div id='main'>
I have a working AJAX function within WordPress, There I'm looping through HTML to get my posts which then are accessible in my AJAX Success CB where they are appended to their div.
My question:
How would I format my PHP query func values (a while loop, an array and several separate pagination values) for proper use within JavaScript?
Here's what my function looks like as is;
<?php
function _custom_paginate(){
$paged = $_POST['count'];
$args = array(
'posts_per_page' => 12,
'category' => 3,
'paged' => $paged,
'post_status' => 'publish'
);
$query = new WP_Query($args);
if ( $query->have_posts() ) :
while ($query->have_posts()) : $query->the_post(); ?>
<div class="large-3 three columns box">
<div class="thumb">
<?php the_content();?>
<div class="thumb-foot">
<div class="thumbinfo">
<p>Posted on <?php the_time('m.d.Y') ?></p>
</div>
<div class="thumb-social">
<div class="thumb-twitter"></div>
<div class="thumb-facebook"></div>
</div>
</div>
</div>
</div>
<?php
endwhile;
endif;
//Several pagination values
$total_pages = $query->max_num_pages;
if ($total_pages > 1){
$current_page = max(1, get_query_var('paged'));
?>
<div class="pagi-contain">
<div id="pagi" class="large-12">
<!-- Append pagination here #pagi-->
<ul class="pagi">
<li id="ajaxprev" style="float:left;">prev</li>
<li id="pagenum"><?php echo $current_page; echo ' ... '.$total_pages; ?></li>
<li id="total_pages"></li>
<li id="ajaxnext" style="float:right;">next</li>
</ul>
</div>
</div>
<?php
}
exit;
}
You don't have to rewrite this but a lesson in formatting each value into an accessible array(?) for JS to work with each value separately. I found this person seems to have done what I am trying but the subject here is not PHP side...
Here is an example that is hopefully more clear on what I'm attempting to make work. http://jsfiddle.net/BenRacicot/LRmc3/
I'm not 100% clear what data you want to format, but just do this:
json_encode($variable_you_want_as_json);
To format the entire page's output, use this:
ob_start(); // Start output buffering (echos after this go into the buffer, not to the client)
// all the code and output you want formatted
// example: echo "Some HTML I want to store in a JSON-encoded variable";
$html_data = ob_get_contents(); // Gets the contents of the buffer
ob_end_clean(); // empties the buffer
echo json_encode($html_data); // encodes and echoes whatever you did between ob_start() and ob_get_contents()
It sounds like you want multiple values, so you can just replace the variable in the last line with an array, like this:
$my_array = array('html_data'=>$html_data,
'max_num_pages' => $query->max_num_pages,
'foo'=>$foo,
'bar'=>$bar,
'some_other_var' => $some_other_var);
echo json_encode($my_array);
I created a config file for a template I am making. I am trying to make the social icons only appear if the variable is set so if they don't enter information for that icon it does not show in the line of icons.
I know that I can do it with an if statement but I would prefer not to have to make 12 if statements (one per icon)
here is the config section for the icons
/* ==========================================================================
Social Media Acct. Information Below
========================================================================== */
/*** INLCUDE FULL URL FOR ALL FIELDS ***/
// Facebook
define('FACEBOOK_URL', '#');
// Twitter
define('TWITTER_URL', '#');
// Linkedin
define('LINKEDIN_URL', '#');
// Pinterest
define('PINTEREST_URL', '#');
// Google Plus
define('GOOGLE_URL', '#');
// Dribbble
define('DRIBBBLE_URL', '#');
// Youtube
define('YOUTUBE_URL', '#');
// Vimeo
define('VIMEO_URL', '#');
// Flickr
define('FLICKR_URL', '#');
// Yelp
define('YELP_URL', '#');
// Stumble Upon
define('STUMBLE_URL', '#');
//Specify icon color below (white or dark)
define('ICON_COLOR', 'white');
and here is the html for each icon
<div id="social_icons">
<div class="icon_container" id="facebook_icon"></div>
<div class="icon_container" id="twitter_icon"></div>
<div class="icon_container" id="linkedin_icon"></div>
<div class="icon_container" id="google_icon"></div>
<div class="icon_container" id="pinterest_icon"></div>
<div class="icon_container" id="dribble_icon"></div>
<div class="icon_container" id="youtube_icon"></div>
<div class="icon_container" id="vimeo_icon"></div>
<div class="icon_container" id="flickr_icon"></div>
<div class="icon_container" id="yelp_icon"></div>
<div class="icon_container" id="stumbleupon_icon"></div>
</div>
I want to be able to accomplish this without placing an if statement around each link. Is there a way? Thanks!
EDIT Here is the new code snippets based on #thtjuan's answer if this helps answer the full question.
PHP
$social_links = array(
'facebook' => '#',
'twitter' => '#',
'linkedin' => '#',
'google' => '#',
'pinterest' => '#',
'dribbble' => '#',
'youtube' => '#',
'vimeo' => '#',
'flickr' => '#',
'yelp' => '#',
'stumbleupon' => '#'
);
HTML
<div id="social_icons">
<?php foreach( $social_links as $icon => $url ): ?>
<div class="icon_container" id="<?php echo $icon?>_icon"></div>
<?php endforeach;?>
</div>
Create an array of links and then use a for loop to print them all:
e.g.
$links = array(
'facebook' => '#',
'twitter' => '#'
);
and then:
<div id="social">
<?php foreach( $links as $icon => $url ): ?>
<div class="icon_container" id="<?php echo $icon?>_icon"></div>
<?php endforeach;?>
</div>
Note that any links that are not in the $links array will not show up at all on your page. If you must have all the entries in the array, then you can keep the ones without a url hidden by doing this:
<div id="social">
<?php foreach( $links as $icon => $url ): ?>
<?php if( strlen($url) && $url != '#' ): ?>
<div class="icon_container" id="<?php echo $icon?>_icon"></div>
<?php endif; ?>
<?php endforeach;?>
</div>