While Loop displays arguments inside the first argument - php

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

Related

Make first loop H1 tag and the remaining loop to H2 - PHP wordpress

Sorry, I'm just a complete novice in PHP
Background: This code is a loop of three slider as you can see here - http://rashelectrical.com.au/. I need to make the H1 tag only on the first slide and make the other two H2 tag
Question:
How can I make the 2nd and last loop to H2 and H1 only on the first
loop?
Thanks
Here is my code:
https://jsfiddle.net/4jm5vhbf/
<?php while(have_rows('banner_slider')) : the_row();?>
<div class="sl" >
<div class="header-content relative-block align-center text-center flex-container align-middle" style="background:url('<?php the_sub_field('image');?>') no-repeat">
<div class="row">
<div class="columns">
<div class="header-content-inner relative-block animate-children">
<h3><?php the_sub_field('subheading');?></h3>
<h1><?php the_sub_field('heading');?></h1>
<p><?php the_sub_field('description');?></p>
<div class="button-group align-center">
<?php $n = 1; while(have_rows('button_links')) : the_row();
$link_lr = get_sub_field('link');?>
<?php if($n == 2){
$n2 = 'hollow';
}else{
$n2 = '';
}?>
<a class="button <?php echo $n2; ?>" href="<?php echo $link_lr['url']; ?>"><span><?php echo $link_lr['title']; ?></span></a>
<?php $n++; endwhile;?>
</div>
</div>
</div>
</div>
</div>
</div>
<?php endwhile;?>
Its kinda hard to tell without seeing the full code, maybe something like that?
<!-- Instead of H3 tag here, use P tag with css class and change the font size with css -->
<h3><?php the_sub_field('subheading'); ?></h3>
<!-- count the heading elements -->
<?php $headings = count( get_sub_field( 'heading' ) ); ?>
<!-- Condition here if heading 1 echo H1 else echo H2 -->
<?php echo ($headings == 0) ? '<h1>' . the_sub_field('heading') . '</h1>' : '<h2>' . the_sub_field('heading') . '</h2>' ?>
<p><?php the_sub_field('description'); ?></p>
<!-- Your rest of code -->
Also for SEO and accessibility reasons, your HTML markup should considering the "Semantic" of your Heading tags .. so don't use H3 tag before H1 tag.

Wordpress Shortcodes Appear In Wrong Place

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

How to get each parent page, and display its children as separate divs in Wordpress

Right now I have the following PHP:
<?php
$pages = get_pages( array( 'sort_column'=>'menu_order', 'parent'=>'0') );
foreach ($pages as $page_data) {
$content = apply_filters('the_content', $page_data->post_content);
$title = $page_data->post_title;
$slug = $page_data->post_name;
$pageid=$page_data->ID;
echo '<div class="section'.(($title=='intro')?' intro-section':"").'">';
echo $content;
echo "</div>";
}
?>
There are 5 Pages, 2 of which have many child Pages. I want to take the above code further one level, so that each page will have its own wrapper, and, if a Page has children Pages, those pages will be placed within the same .section, but each within a separate div. Any advice? I'm a bit inexperienced in Wordpress so any help would be great. Below should give an example of what I want:
<div class="section">
<div class="page parent"></div>
</div>
<div class="section">
<div class="page parent"></div>
<div class="page child"></div>
<div class="page child"></div>
</div>
<div class="section">
<div class="page parent"></div>
</div>
Below is the code I'm working with right now.
<?php while (have_posts()) : the_post(); ?>
<?php
$pages = get_pages( array( 'sort_column'=>'menu_order', 'parent'=>'0') );
foreach ($pages as $page_data) {
$content = apply_filters('the_content', $page_data->post_content);
$title = $page_data->post_title;
$slug = $page_data->post_name;
$pageid=$page_data->ID;
//Put a container around every page's content
if (count(get_pages('child_of=' . $page_data->ID.''))) {
// Loop through $page_data array to print the div you want
echo '<div class="section">';
echo '<div class="slide">';
echo $content;
echo "</div>";
$children = get_pages('child_of=' . $page_data->ID.'');
foreach( $children as $child ) {
$childcontent = apply_filters('the_content', $child->post_content);
echo '<div class="slide">';
echo $childcontent;
echo "</div>";
}
echo "</div><!--end section-->";
} else {
echo '<div class="section'.(($title=='intro')?' intro-section':"").'">';
echo $content;
echo "</div>";
}
}
?>
<?php endwhile ?>
Use child_of parameter of get_pages() function inside the loop. This parameter lists the sub-pages of a single Page only. It uses the ID for a Page as the value.
Eg.
if (count(get_pages('child_of=' . $page_data->ID))) {
// Loop through $page_data array to print the div you want
}

How to edit php file and return wordpress blog entries in multiple columns?

I am using the same theme as this site http://foreignpolicydesign.com/v3/. However, my test site does not distribute the blog entries in columns
What might be causing this problem? I am suspecting it is whatever sets the $col_class to set the x1 ~ xN, but I cannot find the source of this variable.
Here is the code:
<?php include (TEMPLATEPATH . '/tanzaheader.php');
// [grid column setting]
$col_w = 200; // width of grid column
$gap_w = 35; // padding + margin-right (15+15+5)
$max_col = 5; // max column size (style div.x1 ~ xN)
// * additional info *
// check also "style.css" and "header.php" if you change $col_w and $gap_w.
// - style.css:
// div.x1 ~ xN
// div.grid-item
// div.single-item
// ... and maybe #sidebar2 li.widget.
// - header.php:
// gridDefWidth in javascript code.
//
// if you want to show small images in main page always, set $max_col = 1.
// [grid image link setting]
$flg_img_forcelink = true; // add/overwrite a link which links to a single post (permalink).
$flg_img_extract = false; // in single post page, extract thumbnail link to an original image.
$flg_obj_fit = 'large-fit'; // none | small-fit | large-fit ... how to fit size of object tag.
// * additional info *
// if you use image popup utility (like Lightbox) on main index, set $flg_img_forcelink = false;
?>
<!-- <?php if (is_singular()) : $is_top_single = true; /* wide column for single post */ ?> -->
<?php /* make a new query for grid items (in single page) */
$new_query_arg = 'paged='.$paged;
// use this code if you want filter items by category.
$arr_catID = array(20);
foreach( get_the_category() as $cat) $arr_catID[] = $cat->cat_ID;
if ( count($arr_catID) ) $new_query_arg .= '&cat=' . join(',', $arr_catID);
query_posts($new_query_arg);
?>
<!-- <?php endif; /* end of if is_singular() */ ?> -->
<div id="grid-wrapper">
<?php if (have_posts()) :
if ( $is_top_single ) $GLOBALS['more'] = false; //important
while (have_posts()) : the_post(); ?>
<?php
$content = get_the_content('Details ยป');
$content = apply_filters('the_content', $content);
list($col_class, $grid_img) = adjust_grid_image(
$content,
$col_w,
$gap_w,
$max_col,
$flg_img_forcelink,
$flg_obj_fit
);
?>
<div <?php post_class('grid-item ' . $col_class); ?> id="post-<?php the_ID(); ?>">
<h2 class="post-title"><?php the_title(); ?></h2>
<?php if ($grid_img) echo '<div class="grid-image">' . $grid_img . '</div>'; ?>
<div class="post-body">
<?php
$content = preg_replace('/<img(?:[^>]+?)>/', '', $content); // remove img tags
$content = preg_replace('/<a([^>]+?)><\/a>/', '', $content); // remove empty a tags
$content = preg_replace('/<p([^>]*?)><\/p>/', '', $content); // remove empty p tags
$content = preg_replace('/<object(.+?)<\/object>/', '', $content); // remove object tags
echo $content;
?>
</div>
<p class="post-meta">
Published on <?php the_time( get_option('date_format') ); ?> <?php the_time(); ?>.<br />
Filed under: <?php the_category(', ') ?> <?php the_tags('Tags: ', ', '); ?>
<?php edit_post_link(__("Edit This"), '(', ')'); ?><br />
<?php /*comments_popup_link();*/ ?>
</p>
</div>
<?php endwhile; else : ?>
<div class="grid-item x1">
<h2>Not Found</h2>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
</div>
<?php endif; ?>
</div><!-- /grid-wrapper -->
<div class="pagination" id="grid-pagination">
<?php paginate_links2($is_top_single); ?>
</div>
<?php /* reset the query */
wp_reset_query();
?>
</div><!-- /container -->
<?php include (TEMPLATEPATH . '/tanzafooter.php'); ?>
The columns are not created by PHP code, they are created dynamically by a jQuery plugin.
Your theme is referencing it here:
http://wpbeta.nfshost.com/wp-content/themes/js/jquery.vgrid.0.1.4-mod.js
But that returns a 404 file not found error.

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