Correct HTML Structure inside PHP IF Condition - php

This is my modified Wordpress loop w/ PHP If and Else Condition code.
<?php if (have_posts()) : ?>
<?php
$i = 1;
while (have_posts()) {
the_post();
if ($i <= 1) {
echo '<div class="firstp">';
echo '<a href="'; the_permalink(); echo '"'; echo 'alt="'; the_title(); echo '"'; echo 'title="'; the_title(); echo '">';
the_post_thumbnail('article-oneimg', array( 'id' => "article-oneimg"));
echo '</a>';
echo '<div class="thedate1">';
the_date();
echo '</div>';
echo '<h1><a href="'; the_permalink(); echo '"'; echo 'alt="'; the_title(); echo '"'; echo 'title="'; the_title(); echo '">';;
the_title();
echo '</a></h1>';
echo '<p>';
the_excerpt();
echo '</p>';
echo '</div>';
} elseif ($i <= 10) {
echo '<div class="HOLDER-OF-secondp">';
include "secondp.php";
echo '</div>';
} else {
// There should be a bunch of HTML mixed in here too
the_title();
}
$i++;
}
?>
<?php else : ?>
<h2>No Posts Found</h2>
<?php endif; ?>
secondp.php contains this code:
<div class="secondp">
<a href="<?php the_permalink() ?>" alt="<?php the_title(); ?>" title="<?php the_title(); ?>">
<?php the_post_thumbnail('article-twoimg', array( 'id' => "article-twoimg")); ?>
</a>
<div class="thedate2">
<?php the_date(); ?>
</div>
<h1>
<?php the_title(); ?>
</h1>
</div>
What I'm trying to do here is just to add a holder/container of the secondp.php since it has several posts. But every time I use an echo '<div class="HOLDER-OF-secondp">'; before & after the include "second.php"; this happens (see image below taken using Firebug.)
What's wrong with the HTML structure inside the PHP IF condition? I trying to achieve is to put a container that holds all the divs class secondp.
In simple HTML, it's something like this:
<div class="holderofsecondp">
<div class="secondp">
a href="<?php the_permalink() ?>" alt="<?php the_title(); ?>" title="<?php the_title(); ?>">
<?php the_post_thumbnail('article-twoimg', array( 'id' => "article-twoimg")); ?>
</a>
<div class="thedate2">
<?php the_date(); ?>
</div>
<h1>
<?php the_title(); ?>
</h1>
</div>
</div>

Try this code:-
<?php global $wp_query;?>
<?php if (have_posts()) : ?>
<?php
$i = 1;
$start_hoder = true
$total_posts = $wp_query->found_posts;
while (have_posts())
{
the_post();
if ($i <= 1) {
echo '<div class="firstp">';
echo '<a href="'; the_permalink(); echo '"'; echo 'alt="'; the_title(); echo '"'; echo 'title="'; the_title(); echo '">';
the_post_thumbnail('article-oneimg', array( 'id' => "article-oneimg"));
echo '</a>';
echo '<div class="thedate1">';
the_date();
echo '</div>';
echo '<h1><a href="'; the_permalink(); echo '"'; echo 'alt="'; the_title(); echo '"'; echo 'title="'; the_title(); echo '">';;
the_title();
echo '</a></h1>';
echo '<p>';
the_excerpt();
echo '</p>';
echo '</div>';
} elseif ($i <= 10) {
if($start_hoder)
{
echo '<div class="HOLDER-OF-secondp">';
$start_hoder = false;
}
include "secondp.php";
if($total_posts == $i && $start_hoder == false)
{
echo '</div>';
}
} else {
// There should be a bunch of HTML mixed in here too
the_title();
}
$i++;
}
?>
<?php else : ?>
<h2>No Posts Found</h2>
<?php endif; ?>
May be this will help you to get your expected output.

Looks like you have a loop, an inside your loop you are repeatedly echoing the holder. You want to rearrange your code to not have the holder inside the loop.

<div class="holderofsecondp">
<?php while (have_posts()) { ?>
<div class="secondp"></div>
<div class="secondp"></div>
<div class="secondp"></div>
<div class="secondp"></div>
<div class="secondp"></div>
<div class="secondp"></div>
<div class="secondp"></div>
<?php } ?>
</div>
<div class="holderofsecondp"> this will be always out of the loop
or in your code Do this
<?php if($i==0) {?> <div class="HOLDER-OF-secondp"><?php } ?>
and add the div out of loop but its not good idea . Let me know if you need more help .

Related

Wrap every 2 posts on a row

I'm using Bootstrap, I have a list of posts and I want to wrap every 2 posts on a row. Each post is wrapped on a <div col>. You can see live here.
I tried with this but it wrap the row each one post:
<?php
$num = 0;
// Check if there are any posts to display
if ( have_posts() ) : ?>
<?php
// The Loop
while ( have_posts() ) : the_post();
if($num%2) {
echo "<div class='row' style='margin-bottom: 2.3em;''>";
}
?>
<div class="col-xs-12 col-sm-6 col-md-6">
<h2 class="category-encabezado">
<a href="<?php the_permalink() ?>" rel="bookmark" title="Enlace a <?php the_title_attribute(); ?>">
<?php the_title(); ?>
</a>
</h2>
<small>Hace
<?php echo human_time_diff( get_the_time('U'), current_time('timestamp') ) . ''; ?>
</small>
<div class="entry">
<p>
<?php the_excerpt(); ?>
</p>
<?php
$my_shortcode = get_field('audio-field');
echo do_shortcode( $my_shortcode );
?>
</div>
</div>
<?php
if($num %2) {
echo '</div>';
}
$num++
?>
<?php endwhile; // End Loop
?>
</div>
<?php
You have to put div.row Out of the Loop while

Wordpress convert php and html template to shortcode

I want to make a shortcode out of this php code that displays recent blog posts with css grid. I'm displaying the image and other meta-data about each blog.
<div class="wrapper">
<?php $q = new WP_Query( 'posts_per_page=3' ); ?>
<div class="recent-blogs">
<?php while ( $q->have_posts() ) : $q->the_post(); ?>
<div class="blog-item">
<h3 class="title"><?php the_title(); ?></h3>
<?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID), 'thumbnail' ); ?>
<img class="image" src="<?php echo $url ?>" />
<div class="avatar"><?php echo get_avatar( get_the_author_meta('ID'), 40); ?></div>
<div class="author"><?php the_author_posts_link(); ?></div>
<div class="time"><?php the_time('m.d.y'); ?></div>
<div class="category"><?php the_category(); ?></div>
<div class="comments"><?php comments_number(); ?></div>
<?php the_excerpt(); ?>
</div>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
</div>
</div>
Basically the template is now being displayed in page.php after all the content and i want to have more control with my page builder so i can place it where i want.
My php skills are bad and i tried concatenating the html into a single string but i always screw up because of the loop and all these php variables. Also tried using ob_start(), ob_get_clean() and ob_get_contents() but for some reason i end up with an infinite loop.
function recent_blogs_grid() {}
add_shortcode('recent_blogs_grid', 'recent_blogs_grid');
I solved it in the end and i didn't have to do this the hard and stupid way like its done with the first code here bellow.
function recent_blogs_grid() {
$q = new WP_Query( 'posts_per_page=3' );
echo '<div class="recent-blogs-wrapper">';
echo '<div class="recent-blogs-gallery">';
while ( $q->have_posts() ) : $q->the_post();
echo '<div class="recent-blogs-item">';
echo '<h3 class="blog-title-meta"><a href="';
echo the_permalink();
echo '">';
echo the_title();
echo '</a></h3>';
$url = wp_get_attachment_url( get_post_thumbnail_id($post->ID), 'thumbnail' );
echo '<a href="';
echo the_permalink();
echo '"><img class="blog-image-meta" src="';
echo $url;
echo '" /></a>';
echo '<div class="blog-metadata-wrapper">';
echo '<div class="blog-avatar-meta">';
echo get_avatar( get_the_author_meta('ID'), 40);
echo '</div>';
echo '<span class="blog-author-meta">';
echo the_author_posts_link();
echo '</span>';
echo '<span class="blog-datetime-meta"><i class="fa fa-clock-o"></i>';
echo the_time('m.d.y');
echo '</span>';
echo '<span class="blog-category-meta"><i class="fa fa-tags"></i>';
echo the_category();
echo '</span>';
echo '<span class="blog-comments-meta"><i class="fa fa-commenting"></i>';
echo comments_number();
echo '</span>';
echo '</div>';
echo the_excerpt();
echo '<a class="blog-read-more" href="';
echo the_permalink();
echo '">Read More</a>';
echo '</div>';
endwhile;
echo '</div>';
echo '</div>';
wp_reset_query();
}
I just pasted the original code in another file and imported it inside my function where i am creating the shortcode. Since i can't edit the original question i asked, this is the original html stuff in its own file "recent-blogs-grid-shortcode.php".
<div class="recent-blogs-wrapper">
<?php $q = new WP_Query( 'posts_per_page=3' ); ?>
<div class="recent-blogs-gallery">
<?php while ( $q->have_posts() ) : $q->the_post(); ?>
<div class="recent-blogs-item">
<h3 class="blog-title-meta">
<?php the_title(); ?>
</h3>
<?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID), 'thumbnail' ); ?>
<a href="<?php the_permalink(); ?>">
<img class="blog-image-meta" src="<?php echo $url ?>" />
</a>
<div class="blog-metadata-wrapper">
<div class="blog-avatar-meta"><?php echo get_avatar( get_the_author_meta('ID'), 40); ?></div>
<span class="blog-author-meta"><?php the_author_posts_link(); ?></span>
<span class="blog-datetime-meta"><i class="fa fa-clock-o"></i><?php the_time('m.d.y'); ?></span>
<span class="blog-category-meta"><i class="fa fa-tags"></i><?php the_category(); ?></span>
<span class="blog-comments-meta"><i class="fa fa-commenting"></i><?php comments_number(); ?></span>
</div>
<?php the_excerpt(); ?>
<a class="blog-read-more" href="<?php the_permalink(); ?>">Read More</a>
</div>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
</div>
</div>
And i just used require pretty much to import it inside my function
function recent_blogs_grid() {
require_once('recent-blogs-grid-shortcode.php');
}
add_shortcode('recent_blogs_grid', 'recent_blogs_grid');

Get Checkout Steps In Magento

I need to give a div a certain height according to the checkout step. here's what I've got:
<ol class="opc" id="checkoutSteps">
<?php $i=0; foreach($this->getSteps() as $_stepId => $_stepInfo): ?>
<?php if (!$this->getChild($_stepId) || !$this->getChild($_stepId)->isShow()): continue; endif; $i++ ?>
<li id="opc-<?php echo $_stepId ?>" class="section<?php echo !empty($_stepInfo['allow'])?' allow':'' ?><?php echo !empty($_stepInfo['complete'])?' saved':'' ?>">
<div class="step-title">
<span class="number"><?php echo $i ?></span>
<h2><?php echo $_stepInfo['label'] ?></h2>
<?php echo $this->__('Edit') ?>
</div>
Here's how what I'm trying to do:
<ol class="opc" id="checkoutSteps-<?php echo $_stepId ?>">
<?php $i=0; foreach($this->getSteps() as $_stepId => $_stepInfo): ?>
<?php if (!$this->getChild($_stepId) || !$this->getChild($_stepId)->isShow()): continue; endif; $i++ ?>
<li id="opc-<?php echo $_stepId ?>" class="section<?php echo !empty($_stepInfo['allow'])?' allow':'' ?><?php echo !empty($_stepInfo['complete'])?' saved':'' ?>">
<div class="step-title">
<span class="number"><?php echo $i ?></span>
<h2><?php echo $_stepInfo['label'] ?></h2>
<?php echo $this->__('Edit') ?>
</div>
It doesn't work. How can I call this like this somehow? Thanks

If and else "==" not working

I'm trying to show different text on WordPress based on a value from a select box. I have written an "if and else" statement but it isn't working for some weird reason. Here is my code:
<?php global $wpUserStylesheetSwitcher;$wpUserStylesheetSwitcher->show_wp_user_stylesheet_switcher(array('list_title'=>'Change theme', 'show_list_title'=>'true', 'list_type'=>'blogstyle'));
$gdgt_theme = '<script>var yourSelect = document.getElementById( "gdgt-test" );document.write( yourSelect.options[ yourSelect.selectedIndex ].value )</script>';
echo $gdgt_theme;
if ($gdgt_theme == 0) { ?>
<article class="gdgt-article">
<div class="gdgt-loop-post">
<a href="<?php echo get_permalink(); ?>" title="<?php echo the_title(); ?>">
<div class="gdgt-loop-thumb">
<?php
if ( has_post_thumbnail() ) {
the_post_thumbnail('gdgt_loop_thumb');
}
else {
echo '<img src="http://for-b.tk/_ph/13/934023873.gif" title="Image not available yet" alt="image-coming-soon"/>';
}
?>
<div class="loop-overlay"></div>
</div>
<div class="gdgt-post-meta">
<h1><?php echo the_title_limit(70, '…'); ?></h1>
</a>
<div class="gdgt-article-meta">
by <?php the_author_meta( 'display_name' ); ?> - <?php echo the_time(); ?>
</div>
<div class="gdgt-post-excerpt">
<?php echo "test" ?>
</div>
</div>
</div>
</article>
<?php }
else {
?>
<article class="gdgt-article">
<div class="gdgt-loop-post">
<a href="<?php echo get_permalink(); ?>" title="<?php echo the_title(); ?>">
<div class="gdgt-loop-thumb">
<?php
if ( has_post_thumbnail() ) {
the_post_thumbnail('gdgt_loop_thumb');
}
else {
echo '<img src="http://for-b.tk/_ph/13/934023873.gif" title="Image not available yet" alt="image-coming-soon"/>';
}
?>
<div class="loop-overlay"></div>
</div>
<div class="gdgt-post-meta">
<h1><?php echo the_title_limit(70, '…'); ?></h1>
</a>
<div class="gdgt-article-meta">
by <?php the_author_meta( 'display_name' ); ?> - <?php echo the_time(); ?>
</div>
<div class="gdgt-post-excerpt">
<?php echo "else works"; ?>
</div>
</div>
</div>
</article>
<?php }
?>
Code Explanation: First I try to get the value from the select box using a simple JS code. I then echo it so that I can see it is returning the correct value. Then I run the if and else
The Problem: When the value == 0, the "test" text shows up, but when it isn't equal to 0, the text "test" still shows up. I'm not sure what's wrong with my code. Please help.

Wordpress Wrap Every 2 Post in a div

I'm trying to wrap every 2 post in a div "row-fluid" Right now my HTML looks like the following...
<div class="row-fluid">
<div class="odd-post span6"></div>
<div class="even-post span6"></div>
<div class="odd-post span6"></div>
<div class="even-post span6"></div>
</div>
I'd like it to look like this...
<div class="row-fluid">
<div class="odd-post span6"></div>
<div class="even-post span6"></div>
</div>
<div class="row-fluid">
<div class="odd-post span6"></div>
<div class="even-post span6"></div>
</div>
This is the php that I'm using to generate this but I think because I'm counting outside of my loop its not rendering the way I'd like it too.
<?php
$count = 0;
if(have_posts()) : while(have_posts()) : the_post();
$open = !($count%2) ? '<div class="row-fluid">' : '';
$close = !($count%2) && $count ? '</div>' : '';
echo $close.$open;
?>
<!--Custom Post Type Boilerplate-->
<?php
$args = array( 'post_type' => 'mysite_team', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
echo '<div ';
$even_odd = (++$j % 2 == 0) ? 'featured-image-wrapper-even-post span6' : 'featured-image-wrapper-odd-post span6'; post_class( $even_odd );
echo '>';
echo '<h1>';
echo '<a href="';
the_permalink();
echo '">';
the_title();
echo '</a></h1>';
echo '<div class="featured-image alignleft">';
echo '<a href="';
the_permalink();
echo '">';
the_post_thumbnail('team-thumbnail-size');
echo '</a>';
echo '</div>';
echo '<div class="entry-content">';
the_excerpt();
echo '<p><a href="';
the_permalink();
echo '"><i>Read More';
echo '</i></a></p>';
echo '</div>';
echo '</div>';
endwhile;
?>
<?php
$count++;
endwhile;
else :
?>
<?php endif; ?>
<?php echo $count ? '</div>' : ''; ?>
Any help would be appreciated as I'm starting to see circle and get confused.
Thank you!
I would avoid the extra div container, sorry. You can solve this with pure CSS.
.row-fluid:nth-child(odd) {
// Some code
}
.row-fluid:nth-child(even) {
// Some code
}
.span6 {
width:50%;
display:inline-cell;
float: left;
}

Categories