Can i make a If Else-statement that finishes a div before doing "else"?
I want to cycle through my posts and put the posts from "specialCat" in one div.
<div id="the-div-i-want-to-close">
<?php if (in_category('specialCat')) { ?>
<h1><?php the_title(); ?></h1>
<?php } else { ?>
<h1><?php the_title(); ?></h1>
<?php } ?>
Thanks a lot :)
Your best bet is to modify the query calling your special category then finishing the loop with the rest of the posts.
<?php
$args = array(
'category_name' => 'special-cat', // Use Category slug
'posts_per_page' => -1 // Get all the post in SpecialCat
);
$special_query = new WP_Query( $args );
?>
<div id="the-div-i-want-to-close">
<?php
while ( $special_query->have_posts() ) : $special_query->the_post();
$no_repeat[] = $post->ID ?>
<h1><?php the_title(); ?></h1>
<?php endwhile; ?>
</div> <?php // Close your special div after looping through all SpecialCat posts
// Start the loop again
if (have_posts() ) : while (have_posts() ) : the_post();
if (in_array( $post->ID, $no_repeat ) ) continue; // Exclude the posts we already got above ?>
<h1><?php the_title(); ?></h1>
<?php endwhile; endif; ?>
You can include HTML within a for each loop, so long as you close the PHP tags (or include the HTML in an echo statement).
Would this do it for you?
<div id="the-div-i-want-to-close">
<?php $isOpen = true; ?>
<?php if (in_category('specialCat')) { ?>
<?php the_title(); ?>
<?php } else { ?>
<?php if($isOpen) { ?>
</div>
<?php $isOpen = false; ?>
<?php } ?>
<?php the_title(); ?>
<?php } ?>
Related
I've stuck in dev WP theme, please help me.
The problem is when I wrote code for displaying post in front-page.php, it display me the_title() and the_content() from page, not from post.
There is code:
<?php if(have_posts()): ?>
<?php while(have_posts()) : the_post(); ?>
<h2 class="blog-post-title"><?php the_title( ); ?> </h2>
<?php the_content(); ?>
<?php endwhile; else: ?>
<p><?php __('No post found') ?></p>
<?php endif; ?>
It displays me post only if I go to post location e.g. localhost/2017/10/16/first-post/.
There is picture how it correctly shows but on another location.
http://prntscr.com/gy922y
Since you are displaying in a custom page, You'll need to declare and specify post_type as post
Here is a working example:
<?php
$args = array(
'post_type' => 'post'
);
$post_query = new WP_Query($args);
if($post_query->have_posts() ) {
while($post_query->have_posts() ) {
$post_query->the_post();
?>
<h2 class="blog-post-title"><?php the_title( ); ?> </h2>
<?php the_content(); ?>
<?php
}
}
?>
We have ACF Pro for WP and we have created an ACF which show a location which is a select.
When trying to output we are getting this:
Notice: Trying to get property of non-object in
/home/cwplantactiveint/public_html/wp-content/themes/cwplant/loop-jobs.php
on line 66
Which is this
<?php $location = get_field('job_location'); echo $location->post_title; ?>
Now oddly, it outputs another custom field which was createdto output the date:
<?php if(get_field('closing_date')) { ?>
<?php the_field('closing_date'); ?>
<?php } else { ?>
Ongoing
<?php } ?>
The whole code block looks like this:
<?php while ( have_posts() ) : the_post(); ?>
<?php /* Check closing date is not past. */
$today = strtotime("now");
$closedate = strtotime(get_field('closing_date'));
if ($today < $closedate || !get_field('closing_date')) {
?>
<div class="singlepost infobox info-job content cfix">
<h2><?php the_title(); ?></h2>
<p><span class="red first">Location:</span> <?php $location = get_field('job_location'); echo $location->post_title; ?>
<span class="red">Closing Date:</span>
<?php if(get_field('closing_date')) { ?>
<?php the_field('closing_date'); ?>
<?php } else { ?>
Ongoing
<?php } ?>
</p>
<?php if ( is_archive() || is_search() || is_home()) : // Only display excerpts for archives and search. ?>
<?php the_excerpt(); ?>
<a class="button" href="<?php the_permalink(); ?>">View Details</a>
<?php else : ?>
<?php the_content( __( 'Continue reading →', 'twentyten' ) ); ?>
<?php endif; ?>
</div>
<?php $jobstrue = 'true'; ?>
<?php } else { ?>
<?php $jobsfalse = 'true'; ?>
<?php } ?>
<?php endwhile; // End the loop. Whew. ?>
I think your problem is that you haven't reset the $post object with wp_reset_postdata() so your query is returning the last thing in $post (likely createdto in your case).
Whenever I handle any object in WP, I always set it up and then reset it like this:
<?php
// YOUR CUSTOM FIELD
// 0- Reset post object from last query (this should really be part of your last query)
wp_reset_postdata();
// 1- Put custom field into post object and check for content
$post_object = get_field('location');
// 2- Check to make sure object exists and setup $post (must use $post variable name)
if ($post_object) {
$post = $post_object;
setup_postdata($post);
// 3- Do some magic
echo get_field('closing_date');
// 4- reset postdata so other parts of the page can use it
wp_reset_postdata();
}
?>
I am trying to write a custom default loop for my blog that will display the first post in full, then have the following posts appear in truncated format with a thumbnail of featured image. I have tried everything I can think of, but cannot figure out how to properly parse the loop. I tried the code from the Wordpress forums, but neither work.
<?php
if (is_front_page() && ++$count == 1) {
the_content();
} else {
$excerpt = get_the_excerpt(); echo string_limit_words($excerpt,80); ?>
}
and
<?php
$count = 0;
if ($count > 0) {
$excerpt = get_the_excerpt(); echo string_limit_words($excerpt,80);
} else {
the_content();
}
$count++;
?>
I have been working on this for weeks. I would greatly appreciate if anyone can help me with this.
As you are using a "default" loop I'm sure you can use this variable, $current_post, which is defined inside the Loop.
<?php
if (is_front_page() && $wp_query->current_post === 1) {
the_content();
} else {
$excerpt = get_the_excerpt(); echo string_limit_words($excerpt,80);
}
?>
Try This :
<?php $i=0; ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php $i+=1; ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<div class="post-date"><?php the_time('F j, Y') ?></div>
<h2><?php the_title(); ?></h2>
<?php if($i<2): the_content(); ?>
<?php else: ?>
<?php the_excerpt(); ?>
...
I need to display separate post with category id after 5th and 10th place. I searched for a query and the query shows,
<?php $postnum++;
if ($postnum == 5) { ?>
<div class="block">
<h2>Blog</h2></div>
<?php }
if ($postnum == 10) { ?>
<div class="block"><h2>References</h2></div>
<?php }
After putting the above code before endwhile, i can see the Blog and References after 5th and 10th place. But while inserting,
<?php $postnum++;
if ($postnum == 5) { ?>
<div class="block">
<?php
query_posts( array(cat=>3, orderby=>post_date, order=>desc) );
while ( have_posts() ) : the_post();
?>
<?php the_title();
<?php endwhile; ?>
<?php wp_reset_query(); ?>
</div>
<?php }
if ($postnum == 10) { ?>
<div class="block"><?php
query_posts( array(cat=>4, orderby=>post_date, order=>desc) );
while ( have_posts() ) : the_post();
?>
<?php the_title();
<?php endwhile; ?>
<?php wp_reset_query(); ?>
</div>
<?php } </div>
<?php }
The above code doesnot seems to work correctly. Please anyone help me..
Could be a few things. Check your server logs, but at the very least
<?php the_title();
<?php endwhile; ?>
is wrong - either you need to add ?> after the_title(); or remove <?php before endwhile;
Similarly, this:
<?php } </div>
Looks like it's missing a ?>.
I'd probably add quotes around the query_posts() parameters as well, for clarity:
query_posts( array('cat'=>3, 'orderby'=>'post_date', 'order'=>'desc') );
And I'd read this too, just so you're confident query_posts is the right approach for you.
I'm still pretty new to PHP, and I'm having trouble getting this to work. What I want to do, is make the slide linked (if link is available). Otherwise, print the post thumbnail without the
Here's my code so far:
<?php // START SLIDER ?>
<div class="slider">
<ul class="rslides">
<?php $args = array( 'posts_per_page' => 0, 'post_type' => 'slide'); $alert = new WP_Query( $args ); ?>
<?php if( $alert->have_posts() ) { while( $alert->have_posts() ) { $alert->the_post(); ?>
<li><?php the_post_thumbnail('full'); ?><div class="caption"><p class="captiontitle"><?php the_title(); ?></p><p class="caption"><?php the_content(); ?></p></div></li>
<?php } } ?>
</ul>
</div>
<?php wp_reset_query(); ?>
<?php // END SLIDER ?>
I've done this before using the WP Custom fields, but I'm not sure how to apply it to my custom post type (called slider). Here's what I did for my Custom Field script:
<?php $slider_url = get_post_meta($post->ID, 'Slider_URL', true);
if ($slider_url) { ?>
LINKED SLIDE HERE
<?php } else { ?>
UNLINKED SLIDE HERE
<?php } ?>
<?php endwhile; ?>
<?php endif; // have_posts() ?>
Here's what I tried (when combining the two), but there's an error somewhere:
<?php // START SLIDER ?>
<div class="slider">
<ul class="rslides">
<?php $args = array( 'posts_per_page' => 0, 'post_type' => 'slide'); $alert = new WP_Query( $args ); ?>
<?php if( $alert->have_posts() ) { while( $alert->have_posts() ) { $alert->the_post(); ?>
<?php $slide_url = get_post_meta($post->ID, 'Slide_URL', true);
if ($slide_url) { ?>
<li><?php the_post_thumbnail('full'); ?><div class="caption"><p class="captiontitle"><?php the_title(); ?></p><p class="caption"><?php the_content(); ?></p></div></li>
<?php } else { ?>
<li><?php the_post_thumbnail('full'); ?><div class="caption"><p class="captiontitle"><?php the_title(); ?></p><p class="caption"><?php the_content(); ?></p></div></li>
<?php } } ?>
<?php endwhile; ?>
<?php endif; // have_posts() ?>
</ul>
</div>
<?php wp_reset_query(); ?>
<?php // END SLIDER ?>
If I'm correct in thinking, you just want to check if the link is there, before outputting, otherwise, just show the image. Try the following:
<?php // START SLIDER ?>
<div class="slider">
<ul class="rslides">
<?php $args = array( 'posts_per_page' => 0, 'post_type' => 'slide'); $alert = new WP_Query( $args ); ?>
<?php if( $alert->have_posts() ) { while( $alert->have_posts() ) { $alert->the_post(); ?>
<!-- Get a link -->
<?php $theLink = get_post_meta($post->ID, "_location", true); ?>
<li>
<!-- Check for a link -->
<?php if($theLink != ''): ?>
<a href="<?php echo $theLink; ?>" title="More Info">
<?php endif; ?>
<?php the_post_thumbnail('full'); ?>
<div class="caption">
<p class="captiontitle">
<?php the_title(); ?>
</p>
<p class="caption">
<?php the_content(); ?>
</p>
</div>
<!-- Close the link -->
<?php if($theLink != ''): ?>
</a>
<?php endif; ?>
</li>
<?php } } ?>
</ul>
</div>
<?php wp_reset_query(); ?>
<?php // END SLIDER ?>