I'm using Foundation for a wordpress theme and I need to wrap two posts into one div with class of 'row'. The thing is I need to put div class="row" before the first post closing the the second post with div and it should repeat with every new posts.
Here is my code:
<?php query_posts( 'cat=2&showposts=9&orderby=date&order=DESC' ); ?>
<div <?php post_class('small-12 medium-6 large-6 columns') ?> id="post-<?php the_ID(); ?>">
<?php echo '<a href="', get_permalink(), '">';
if ( has_post_thumbnail() ) {the_post_thumbnail();}
else { echo '<img src="', get_template_directory_uri( 'template_directory' ), '/images/thumb-default.png','" alt="" />'; }
echo '</a>';
?>
<h3><?php the_title(); ?></h3>
<p><?php echo get_excerpt(); ?></p>
</div>
something like this i think
<?php
$count = 1;
$outputstring = "";
ur while loop
if ( $count % 2 != 0 )
{
$outputstring .= " <row div>";
}
$outputstring .= "<div" . post_class('small-12 medium-6 large-6 columns'). ' id="post-'. the_ID() .'>';
YOUR OUTPUT CODE HERE
if ( $count % 2 == 0 )
{
$outputstring .= " </end row div>";
}
$count++;
END your while loop
echo $outputstring; /// here is where u output the WHOLE thing outside of your loop
?>
Related
I want to insert php code in between tabby tabs shortcodes.
I am using a plugin tabby tab for tab view and have added this code in my theme template:
<?php echo do_shortcode('[tabby title="Gallary Name"]
name content
[tabby title="Images"]
[tabbyending]'); ?>
I want to use a custom fields gallery under images tab using code like this:
<?php echo do_shortcode('[tabby title="Gallary Name"]
name content
[tabby title="Images"]
<?php
$i = 0;
$images = get_field('vil_pics');
if( $images ): ?>
<div>
<ul>
<?php foreach( $images as $image ): ?>
<li<?php if ( $i % 3 == 0 ) echo ' class="break"' ?>>
<a href="<?php echo $image['url']; ?>">
<img src="<?php echo $image['sizes']['thumbnail']; ?>" alt="<?php echo $image['alt']; ?>" />
</a><p>.</p>
</li>
<?php endforeach; ?>
</ul></div>
<?php endif; ?>
[tabbyending]'); ?>
This code is not working, it's showing a blank page. How can I fix this?
Tabby uses a global variable to track what's going on, so I think either one of these will work. The first one is a little more straightforward, but the second one will definitely work.
Option 1: output everything in order:
echo do_shortcode( '[tabby title="Gallery Name"] name content' );
echo do_shortcode( '[tabby title="Images"]' );
// your php code as-is
$i = 0;
$images = get_field('vil_pics');
if( $images ): ?>
<div>
<ul>
<?php foreach( $images as $image ):
$i++ ?>
<li<?php if ( $i % 3 == 0 ) echo ' class="break"' ?>>
<a href="<?php echo $image['url']; ?>">
<img src="<?php echo $image['sizes']['thumbnail']; ?>" alt="<?php echo $image['alt']; ?>" />
</a><p>.</p>
</li>
<?php endforeach; ?>
</ul>
</div>
<?php endif;
echo do_shortcode( '[tabbyending]' );
or Option 2: save everything to a variable and output it all at once:
$output = '';
$output .= '[tabby title="Gallery Name"] name content';
$output .= '[tabby title="Images"]';
$i = 0;
$images = get_field('vil_pics');
if ( $images ) {
$output .= '<div><ul>';
foreach( $images as $image ) {
$i++;
$li_class = ( $i % 3 == 0 ) ? ' class="break"' : '';
$output .= '<li' . $li_class . '>';
$output .= '<a href="' . $image['url'] . '">';
$output .= '<img src="' . $image['sizes']['thumbnail'] . '" alt="' . $image['alt'] . '" />';
$output .= '</a><p>.</p></li>';
}
$output .= '</div></ul>';
}
$output .= '[tabbyending]';
echo do_shortcode( $output );
Note that I didn't see anything increasing $i so I added that. Everything else is as-is.
There is PHP code that when User click on the_title(); then the_content will be emerged.
My problem is that if there are unlimited the_title(); and the_content();, there is problem with generating ID that it must be unique.
How can i generate for href="#id" and id="id"???
<?php
// Start the loop.
while ( have_posts() ) : the_post();
echo '<a href="#id" data-toggle="collapse">';
the_title();
echo '</a>
<div id="id" class="collapse">';
the_content();
echo '</div>';
// End of the loop.
endwhile;
?>
You need to use like that example:
$i = 1; // counter start from 1
while ($i <= 10):
echo ' '.$i.'';
$i++; // increment 1
endwhile;
Example with your code:
$i = 1; // start counter from 1
while ( have_posts() ) : the_post();
echo '<a href="#'.$i.'" data-toggle="collapse">';
the_title();
echo '</a>
<div id="'.$i.'" class="collapse">';
$i++; // counter
the_content();
echo '</div>';
// End of the loop.
endwhile;
What i have changed?
Just add a counter in while loop initialize with 1
make an array which keeps on increment. use its index like this:
<?php echo $new=array('1','2','3','4');?>
foreach($new as $key =>variable){
echo '<a href="#id<?php echo $key+1;?>" data-toggle="collapse">';
the_title();
echo '</a>
<div id="id<?php echo $key+1;?>" class="collapse">';
the_content();
echo '</div>';
}
i have tried it. it worked for me. hope you get the idea too how it works.
i am increment the id with the help of key value of the foreach loop.
I'm trying to make my blog titles link to the full post from the preview area so the title should have the same function as the read more button. The blogs are in a masonry layout and I'm using a themeforest theme.
This is the blog page.
I believe this to be the php code that controls the layout - hope it helps.
Sorry php newbie here.
I have tried using <h5 class="post-title">'. get_the_title() .'</h5>'; but all this did was generate a broken url containing '.get_permalink().'" in it.
Thank you
<?php if ( '' != get_the_title() ): ?>
<h5 class="post-title"><?php the_title(); ?></h5>
<?php endif ?>
<?php if (has_post_format('link')): ?>
<?php echo __("Read more", TEMPNAME); ?><span class="icon-arrow-right9"></span>
<?php else: ?>
<?php echo __("Read more", TEMPNAME); ?><span class="icon-arrow-right9"></span>
<?php endif ?>
<?php endif; ?>
You just need to wrap the h5 title in an anchor tag <a> on line 37 of your snippet. The specific code to change is:
New Answer
<a href="<?php get_permalink(); ?>">
<h5 class="post-title"><?php the_title(); ?></h5>
</a>
or from you code, try:
<a href="<?php echo $nz_link_url; ?>" title="<?php echo __("Go to", TEMPNAME).' '.$nz_link_url; ?>">
<h5 class="post-title"><?php the_title(); ?></h5>
</a>
Old Answer
if ( '' != get_the_title() ){
$output .= '<h5 class="post-title">'. get_the_title() .'</h5>';
}
You may have to update your CSS to reflect the anchor tag in front of the H5
Full Code
while($recent_posts->have_posts()) : $recent_posts->the_post();
$output .= '<div class="post format-'.get_post_format().'" data-grid="ninzio_01">';
$output .= '<div class="post-wrap nz-clearfix">';
if (get_post_format() == 'image') {
$values = get_post_custom( $post->ID );
$nz_image_url = isset($values["image_url"][0]) ? $values["image_url"][0] : "";
if (!empty($nz_image_url)) {
$output .='<a class="nz-more" href="'.get_permalink().'">';
$output .= '<div class="nz-thumbnail">';
$output .= '<img src="'.$nz_image_url.'" alt="'.get_the_title().'">';
$output .= '<div class="ninzio-overlay"></div>';
$output .= '<div class="post-date"><span>'.get_the_date("d").'</span><span>'.get_the_date("M").'</span></div>';
$output .='</div>';
$output .='</a>';
}
} else {
if (has_post_thumbnail()) {
$output .='<a class="nz-more" href="'.get_permalink().'">';
$output .= '<div class="nz-thumbnail">';
$output .= get_the_post_thumbnail( $post->ID, $size );
$output .= '<div class="ninzio-overlay"></div>';
$output .= '<div class="post-date"><span>'.get_the_date("d").'</span><span>'.get_the_date("M").'</span></div>';
$output .='</div>';
$output .='</a>';
}
}
$output .= '<div class="post-body">';
if ( '' != get_the_title() ){
$output .= '<h5 class="post-title">'. get_the_title() .'</h5>';
}
if ($excerpt == "true") {
$output .= '<div class="post-excerpt">'.nz_excerpt(95).'</div>';
}
$output .=''.__("Read more", TEMPNAME).' <span class="icon-arrow-right9"></span>';
$output .= '</div>';
$output .= '</div>';
$output .= '</div>';
So basically I am building a wordpress gallery, I need to have the images output into 2 rows, each row is 3 images across so essentially I am just splitting the output every 3..
I have that functionality working with a for loop using the modulus operator, what I need is that every 2 rows I need to have them wrapped in an outer HTML container
<div class="wrappy">
but I cant seem to get this quite right having worked on it most of yesterday, Im not that great at PHP but I understand enough to get by so any help at all would be great... I am using wordpress advanced custom fields for storing the images etc so you can ignore that part, the magic Im working on starts at
<div class="wrappy">
<?php
$rows = get_field('staff_slides', 'options');
$countmax = count($rows) - 1;
//echo $row_count;
$ender = "";
$mainEnder = "";
$outer_wrapper = "";
if( have_rows('staff_slides', 'options') ):
// loop through the rows of data
while ( have_rows('staff_slides', 'options') ) : the_row();
$image_id = get_sub_field('slide_image', 'options');
$staff_members_name = get_sub_field('staff_members_name', 'options');
$staff_members_position = get_sub_field('staff_members_position', 'options');
$staff_image = wp_get_attachment_image_src( $image_id , 'homepage-staff');
?>
<?php
echo '<div class="wrappy">';
echo '<div class="row">';
if( $ender != "script-ended" ) {
for( $i=0; $i <= $countmax; )
{
?>
<div class="staff-img c3">
<div class="staff-caption">
<h3><?php echo $rows[$i]['staff_members_name']; ?></h3>
<span></span>
<h4><?php echo $rows[$i]['staff_members_position']; ?></h4>
</div>
<img src="<?php echo wp_get_attachment_image_src( $rows[$i]['slide_image'], 'homepage-staff')[0]; ?>">
</div>
<?php
if( $i % 3 == 2 )
{
echo '</div><div class="row">';
}
if( $i == $countmax )
{
$ender = "script-ended";
}
if( $i == 6){
$outer_wrapper = "set";
}
$i++;
}
}
?>
<?php
if( $outer_wrapper == "set" ){
echo '</div><div class="wrappy">';
}
?>
You should use the array_chunk function to split the array in smaller arrays.
I'm not familiar with the ACF functions but you can do something like:
<div class="wrappy">
<?php
if( have_rows('staff_slides', 'options') ) :
$slides = array_chunk(get_field('staff_slides', 'options'), 3);
foreach ($slides as $slides_row) :
?>
<div class="row">
<?php foreach ($slides_row as $slide_element) : the_row(); ?>
<div class="staff-img c3">
<div class="staff-caption">
<h3><?php the_sub_field('staff_members_name', 'options'); ?></h3>
<span></span>
<h4><?php the_sub_field('staff_members_position', 'options'); ?></h4>
</div>
<img src="<?php echo wp_get_attachment_image_src( get_sub_field('slide_image', 'options'), 'homepage-staff')[0]; ?>">
</div>
<?php endforeach; ?>
</div>
<?php endforeach; else : ?>
No slides do to show
<?php endif; ?>
</div>
This way you create a multidimensional array, divided in chunks of 3 elements, and iterate throught it.
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
}