Wordpress Shortcodes Appear In Wrong Place - php

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 :)

Related

While Loop displays arguments inside the first argument

I have a while loop created to display all my wordpress posts. The issue is that all the posts (except the first) appear inside the first post.
I've check my div structures and I couldn't find any issues.
The loop is working, but the div isn't showing in the right place.
<div class="slider-vertical">
<!-- This is the main loop for the posts -->
<?php
$count = 0;
while ( $all_posts->have_posts() ) : $all_posts->the_post();
$count++;
// Variabled for the specific fields (color, 3D objects and so on...)
$fond_couleur = get_field('fond_de_couleur_hex');
$visuel_3D = get_field('source_iframe_3d');
$visuel_3D_2 = get_field('source_iframe_3d_2');
// get_template_part( 'template-parts/content', 'portfolio' );
$content = get_the_content();
$content = apply_filters( 'the_content', $content );
$content = str_replace( ']]>', ']]>', $content );
$gallerieArray = get_field('image_galerie');
$title = get_the_title($post);
?>
<!-- HTML structure for the posts - this is displayed, but only the first one is at the right place -->
<?php
echo '<div class="post-numero';
if($count == 1){
echo ' actif';
}
echo '">';
?>
<!-- DIV that contains the 3D object -->
<div class="visuel-3d" style="background:<?php echo $fond_couleur;?>;">
<!-- Injecting the 3D Object via an iframe -->
<div class="slider-horizontal">
<?php echo '<span class="post-horizontal actif">'. $visuel_3D .'</span>';?>
<?php if($visuel_3D_2){
echo '<span class="post-horizontal">'. $visuel_3D_2 .'</span>';
} ?>
</div>
<h2 class="titrePost"><?php echo $title; ?></h2>
</div>
<!-- Cross used to close the rightside menu -->
<span class="arrow-right-sidebar">x</span>
<!-- HTML structure for the rightside menu -->
<div class="right-sidebar-content foo">
<div class="right-top">
<h2 style="text-align: center;"><?php echo $title; ?></h2>
</div>
<div class="right-bottom">
<div><?php echo $content ?></div><!-- That is where the next posts of the while loop get printed -->
<div class="gallerie-sidebar-full">
<?php // loop for the image gallery at the bottom of the rightside menu
if ($gallerieArray == true){
foreach($gallerieArray as $key => $val){
$gallerieImages[] = $val["url"];
}
$idg = 0;
foreach ($gallerieImages as $val){
echo '<img id="id-'. $title .'-'. $idg .'" class="gallerie-sidebar" src="'. $val .'">';
$idg += 1;
}
}
?>
</div>
</div>
</div>
</div>
<?php
endwhile;
?>
</div>
What is expected is 4 different posts inside the "slider-vertical" div displayed at the same level in the HTML structure.
The actual result is the first post at the correct level and then the next posts 2 levels deeper inside the first post.
this kind of problem occurs when there is a not properly closed HTML tag on the first execution of the loop, try to use a short statement syntax
echo '<div class="post-numero'. (($count == 1)? ' actif' : '').'">;
check also the content printed by <?php echo $content ?> of the first post, may contains an open HTML tag.
You can easily check the generated code using the souce view (CTRL+U) in Firefox that highlights in red the wrong HTML closing tags

Php Joomla show/hide pageheading if category or article view

I need to edit this code in a Joomla template so that it shows the page heading (id="jf_page_heading") only when I'm in category blog view and hide it when I'm inside the article.
<?php
$menu = JFactory::getApplication()->getMenu();
$active = $menu->getActive();
if (is_object($active)) {
$pageHeading = $active->params->get('page_heading');
$show_pageHeading = $active->params->get('show_page_heading');
// CALL
if($pageHeading != ''){ // or - if($pageHeading != '' && $show_pageHeading){
?>
<div id="jf_page_heading">
<div class="rt-container">
<div class="rt-block">
<h1><?php echo $pageHeading; ?></h1>
<?php echo $gantry->displayModules('jf-page-heading','basic','basic'); ?>
<div class="clear"></div>
</div>
</div>
</div>
<?php } } ?>
How should I edit it?
To show page heading in category blog view, you don't need to edit the code. Rather you can simply enable the show page heading parameter in your category blog menu. For hiding the page heading in article view, you need create a override for it in your template folder.
Article path: site/templates/html/com_content/article/default.php
Comment the page heading html section you want to hide.
Alternative way, if you want to do it through code as you are using gantry template in category blog view if the code is not present -
You should not check $pageheading in if condition but check the show_pageHeading variable.
Category-blog path: site/templates/html/com_content/category/blog.php
<?php $menu = JFactory::getApplication()->getMenu();
$active = $menu->getActive();
if (is_object($active)) {
$pageHeading = $active->params->get('page_heading');
$show_pageHeading = $active->params->get('show_page_heading'); // returns 1 or 0 if set to Yes or no in menu item
// check if showpageheading is set in menu item
if($show_pageHeading){ ?>
<div id="jf_page_heading">
<div class="rt-container">
<div class="rt-block">
<h1><?php echo $pageHeading; ?></h1>
<?php echo $gantry->displayModules('jf-page-heading','basic','basic'); ?>
<div class="clear"></div>
</div>
</div>
</div>
<?php } } ?>
Follow point(1) for hiding page-heading in article view.
Hope this helps.
Try something like
$input = JFactory::getApplication()->input;
if (
$input->getCmd('option') == 'com_content' &&
$input->getCmd('layout') == 'blog' &&
$input->getCmd('view') == 'category'
) {
// Show title
}

Count how many rows are in sub_field

I am using advanced custom field repeater to load some sub_fields which you can see in the below code:
<?php
if( get_field('who_made_it') ): ?>
<div id="role-wrapper">
<?php while( has_sub_field('who_made_it') ): ?>
<div class="role-item">
<?php the_sub_field('job_role'); ?>
<?php the_sub_field('description'); ?>
</div>
<?php endwhile; ?>
</div>
<?php endif; ?>
I would like to count how many .row-item's there are and then print that number as a class on the container #role-wrapper .
So as a HTML demo of how it would look:
<div id="role-wrapper" class"roleitems-3">
<div class="role-item">
content in here
</div>
<div class="role-item">
content in here
</div>
<div class="role-item">
content in here
</div>
</div>
As specified by the docs, get_field() returns an array() of sub fields in this case. As a result, you can do a simple count():
<div id="role-wrapper" class"roleitems-<?php echo count( get_field('who_made_it') ); ?>">
I am unfamiliar with has_sub_field and the advanced custom field repeater, but it seems a simple answer would be to add a counter.
<?php
$counter = 0;
while( has_sub_field('who_made_it') ):
//do stuff
$counter++;
endwhile;
//output the counter however you like
echo('<div class="counter">Total: ' . $counter . '</div>');
?>

Fishpig Wordpress Repeater Fields

So I've got a Magento install, with Fishpig Wordpress Integration and the ACF plugin to pull in meta values. I'm also using the repeater field here which pulls in the metadata as an array (as I understand it). The Fishpig documentation is non- existent so alot of this is guess work really but here's my code:
<?php
/**
* #category Fishpig
* #package Fishpig_Wordpress
* #license http://fishpig.co.uk/license.txt
* #author Ben Tideswell <help#fishpig.co.uk>
*/
?>
<?php $page = $this->getPage() ?>
<?php if ($page): ?>
<?php $helper = $this->helper('wordpress') ?>
<?php $author = $page->getAuthor() ?>
<div class="page-title">
<h1><?php echo $this->escapeHtml($page->getPostTitle()); ?></h1>
</div>
<?php
$lookbooks = $page->getMetaValue('lookbooks');
if($lookbooks):
foreach ($lookbooks as $lookbook) {
$title = $lookbook['title'];
$content = $lookbook['content'];
$images = array($lookbook['images']);?>
<h2><?php echo $title;?></h2>
<div class="connected-carousels">
<div class="stage">
<ul>
<?php foreach($images as $image) { ?>
<li>
<img src="<?php echo $image['image'];?>" alt="<?php echo $image['alt'];?>" />
</li>
<?php } ?>
</ul>
‹
<a href="#" class="next next-stage" >›</a>
</div>
<div class="navigation">
‹
<a href="#" class="next next-navigation" >›</a>
<div class="carousel carousel-navigation">
</div>
</div>
</div>
<?php echo $content ; ?>
<?php }
else : ?>
<div class="post-view">
<div class="entry std">
<?php if ($page->isViewableForVisitor()): ?>
<?php if ($featuredImage = $page->getFeaturedImage()): ?>
<div class="featured-image left"><img src="<?php echo $featuredImage->getAvailableImage() ?>" alt=""/></div>
<?php endif; ?>
<?php echo $page->getPostContent() ?>
<br style="clear:both;"/>
<?php else: ?>
<?php echo $this->getPasswordProtectHtml() ?>
<?php endif; ?>
</div>
</div>
<?php endif;?>
<?php endif; ?>
What I'm trying to do is use the repeater field to make a carousel using jcarousel, I'm fine with my jquery but there's some sort of PHP error here preventing the page from loading.
Here's my ACF structure with the labels:
lookbook (repeater)
--title
--content
--images (repeater)
----image
----alt
I can't see any php errors in the servers error log, nor is the page displaying any errors. It's just not echoing the $image array although it is repeating the loop the right amount of times.
Maybe I'm miles away, maybe I'm nearly there I just can't see anything wrong with it.
Thanks in advance
There is an issue in version 1.2.1.0 of the ACF extension that breaks repeater fields that are embedded inside a repeater field. I have just released version 1.2.2.0 that fixes this issue and allows you to use repeater fields inside other repeater fields.

Child page content

I have a parent page that acts as menu for my portfolio.
It pulls in thumbnail images from the child pages which i have been able to accomplish with magic fields and some code. It dumps the images into a grid layout. The thumbnails are pulled into one container div like so:
div id="folio-content">
<div class="thumb-container">
<div class="thumb"><img src="/images/pic.jpg"/>
</div>JCPenny</div>
... </div>`
when the div gets filled up with 2 thumbnails I want to create a new container div and fill it with 2 images again and so on after 2 images.
So, if you had 4 images it would look like this.
<div id="folio-content"><!--/Main Container/-->
<div class="thumb-container">
<div class="thumb"><img src="/images/pic1.jpg"/>
</div>JCPenny</div>
<div class="thumb-container">
<div class="thumb"><img src="/images/pic1.jpg"/>
</div>Champ Car</div></div>
<div id="folio-content"><!--/Main Container/-->
<div class="thumb-container">
<div class="thumb"><img src="/images/pic1.jpg"/>
</div>JCPenny</div>
<div class="thumb-container">
<div class="thumb"><img src="/images/pic1.jpg"/>
</div>Champ Car</div></div>
this is the code I am using in my page.php file.
<?php get_header(); ?>
<div id="folio-content">
<?php
$projectpage = get_pages('child_of='.$post->ID.'&sort_column=post_date&sort_order=desc');
$count = 0;
foreach($projectpage as $page)
{
$content = $page->post_content;
if(!$content)
continue;
if ($count == 10) --- this is geting 10 images now, but I want to get them all.
break;
$count++;
$content = apply_filters('the_content', $content);
?>
<div class="thumb-container">
<div class="thumb"><a href="<?php echo get_permalink($page->ID); ?>"<?php echo get_image ("thumbnail",1,1,1,$page->ID);?></a>
</div><?php echo $page->post_title ?>
</div>
<?php
}
?>
</div><!--/close set!-->
</div>
also, how can I get ALL child pages? I have it set to 10 now with this if ($count == 10)
any help? thanks a ton again!!!!
I'm not familiar with "get_pages" but since Wordpress treats posts and pages in an identical manner you could use this.
$projectpage = get_posts('numberposts=-1&post_type=page&child_of='.$post->ID.'&sort_column=post_date&sort_order=desc');
The -1 removes the limit and gets ALL the specified pages.
I have cobbled together some code, that sort of sounds right but does not work at all! Which I am not surprised. But it is a starting point - please take a look at this code, maybe it is step in the right direction?
<?php
$projectpage = get_posts('numberposts=-1&post_type=page&child_of='.$post->ID.'&sort_column=post_date&sort_order=desc');
if (have_posts()) :
$i=0; // counter
while(get_posts()) : the_post();
if($i%2==0) { // if counter is multiple of 3, put an opening div ?>
<!-- <?php echo ($i+1).'-'; echo ($i+2); ?> -->
<div>
<?php } ?>
<div class="single_item">
<h2><?php the_title(); ?></h2>
</div>
<?php $i++;
if($i%2==0) { // if counter is multiple of 3, put an closing div ?>
</div>
<?php } ?>
<?php endwhile; ?>
<?php
if($i%2!=0) { // put closing div here if loop is not exactly a multiple of 3 ?>
</div>
<?php } ?>
<?php endif; ?>

Categories