Wordpress post query php custom field conditional - wordpress

Here's the situation:
In wordpress I'm trying to reset a post WP_Query so that I can rewrite the post link based on whether or not a custom field exists in the post. I'm trying to give the post a NEW link in the custom field.
All I've managed to do here is kill the link entirely. Any and all help is greatly appreciated, I'm pretty green to php.
Here's my WP_Query:
<?php
$recentPosts = new WP_Query();
$recentPosts->query('showposts=3');
?>
<?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<?php
$attribute = the_title_attribute();
$title = the_title();
$key = 'NewPostLink';
$newLink = get_post_meta( $post->ID, $key, TRUE );
if ($newLink != '') {
$theLink = get_permalink ($post->ID );
if (has_post_thumbnail()) {
$image = get_the_post_thumbnail( $post->ID );
echo '<div class="thumbnailbox"><div class="thumbnail">'.$image.'</div></div>';
echo '<h2>'.$title.'</h2>';
} else {
echo '<h2>'.$title.'</h2>';
}
} else {
$theLink = $newLink;
if (has_post_thumbnail()) {
$image = get_the_post_thumbnail( $post->ID );
echo '<div class="thumbnailbox"><div class="thumbnail">'.$image.'</div></div>';
echo '<h2>'.$title.'</h2>';
} else {
echo '<h2>'.$title.'</h2>';
}
}
?>
<small><?php the_time('F jS, Y') ?></small>
<div class="entry">
<?php the_excerpt(); ?>
</div>
</div>
<?php endwhile; ?>

I think this is what you need. It's hard to tell. I suppose that the first part of the if statement is what runs if there is no custom post meta? I couldn't tell. Here's what the problem was. The if statement ran the first part if there IS a value returned for the custom post meta, otherwise it ran the second part, using the empty string as the href. (The first part runs if the custom value either doesn't exist or is anything but an empty string). Changing the if statement to check if it's empty is better because it will catch it if it doesn't exist (returns false), or if it does exist but is an empty string (declared but not defined).
I've marked what I edited with comments (just one line).
<?php
$recentPosts = new WP_Query();
$recentPosts->query('showposts=3');
?>
<?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<?php
$attribute = the_title_attribute();
$title = the_title();
$key = 'NewPostLink';
$newLink = get_post_meta( $post->ID, $key, TRUE );
/* EDITED */ if (empty($newLink)) {
$theLink = get_permalink ($post->ID );
if (has_post_thumbnail()) {
$image = get_the_post_thumbnail( $post->ID );
echo '<div class="thumbnailbox"><div class="thumbnail">'.$image.'</div></div>';
echo '<h2>'.$title.'</h2>';
} else {
echo '<h2>'.$title.'</h2>';
}
} else {
$theLink = $newLink;
if (has_post_thumbnail()) {
$image = get_the_post_thumbnail( $post->ID );
echo '<div class="thumbnailbox"><div class="thumbnail">'.$image.'</div></div>';
echo '<h2>'.$title.'</h2>';
} else {
echo '<h2>'.$title.'</h2>';
}
}
?>
<small><?php the_time('F jS, Y') ?></small>
<div class="entry">
<?php the_excerpt(); ?>
</div>
</div>
<?php endwhile; ?>

Related

WordPress last_modified what was changed

Is it possible to find out if only the date/time of a post has been changed or something in the content? The the_modified_date() function covers both and I can't find a way to do a specific check.
The Background: I have a list of posts which will be published in the future and I want to have some feedback for the users if the date or time of an upcoming post changes.
This is my code so far:
<?php
$loop_ls = new WP_Query( $args_ls );
if($loop_ls->have_posts()) : while($loop_ls->have_posts()) : $loop_ls->the_post();
// set date/time variables
$live_date = get_the_date('j.n.');
$live_time = get_the_date('H:i');
?>
<?php if ( get_post_status() == 'future' ) : ?>
<div class="live live--upcoming">
<p><?php _e('Am', 'textdomain') ?> <span class="post-date"><?php echo $live_date; ?></span> <?php _e('um', 'textdomain') ?> <span class="post-time"><?php echo $live_time; ?></span> <?php _e('Uhr', 'textdomain') ?> <?php _e('zum Thema:', 'textdomain') ?>
<br/><span class="post-title"><?php echo get_the_title(); ?></span>
</p>
</div>
<?php elseif ( get_post_status() == 'publish' ) : ?>
<div class="live live--now">
<p><?php _e('Jetzt:', 'textdomain') ?> <span class="post-title"><?php echo get_the_title(); ?></span></p>
<a class="btn btn--default" href="<?php echo get_the_permalink(); ?>"><?php _e('zum LIVE Video', 'textdomain') ?></a>
</div>
<?php endif; ?>
<?php endwhile; endif; wp_reset_postdata(); ?>
OK, I figured out a way to save a creation date for posts published in the future (code below). But I still have the problem, that I can't check if only a posts date/time is modified (not the content).
function wx_add_creation_date_to_live_videos( $post ) {
$post_id = get_the_ID();
$post_type = get_post_type($post_id);
if( $post_type == 'video' && has_term('live', 'video_category') ) {
// If this is a revision, do nothing.
if ( wp_is_post_revision( $post_id ) )
return;
$creation_date = get_the_modified_date('Y-m-d H:i:s', $post_id);
update_post_meta( $post_id, 'creation_date', $creation_date );
}
}
add_action( 'draft_to_future', 'wx_add_creation_date_to_live_videos', 10, 3 );
EDIT:
sometimes you just think too complicated. To check a change on post date/time I changed the code above to this
$orig_publish_date = get_the_date('Y-m-d H:i:s', $post_id);
update_post_meta( $post_id, 'orig_publish_date', $orig_publish_date );
And now I can compare the values of the orig_publish_date with the current publish_date. And that's it.

Get custom fields without _edit_last, _edit_lock, _wp_page_template and _visual-subtitle

I'm getting custom fields using
<?php $meta = get_post_custom($level_2->ID); ?>
<?php foreach ( $meta as $key => $value) { ?>
<?php echo $key.': '.$value[0].'<br />'; ?>
<?php } ?>
and it is showing
_edit_last: 1<br>
_edit_lock: 1483226440:1<br>
_wp_page_template: page-services.php<br>
Body Repair: ValueBodyRepair<br>
_visual-subtitle: <br>
I need only 4th row Body Repair: ValueBodyRepair<br>
If you would adjust the code this way, you would get only the one you need:
<?php $meta = get_post_custom($level_2->ID); ?>
<?php foreach ( $meta as $key => $value) { ?>
<?php if(substr($key, 0, 1) !== '_'): ?>
<?php echo $key.': '.$value[0].'<br />'; ?>
<?php endif; ?>
<?php } ?>

How to create wordpress shortcode by this code

Sir,
I have this code to show all post of category and thumbnail for 1st post of them.
<?php $recent = new WP_Query(); ?>
<?php $recent->query( 'cat=1&showposts=5' ); ?>
<?php $is_first_post = true; ?>
<?php while( $recent->have_posts() ) : $recent->the_post(); ?>
<ul>
<li>
<?php
if ( $is_first_post && has_post_thumbnail() ) {
the_post_thumbnail();
$is_first_post = false;
}
?>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</li>
</ul>
<?php endwhile; ?>
But I want show this using shortcode. which using category & post number
but I can not make shortcode. Please help me.
// Add Shortcode
function recentpost_shortcode_func() {
$recent = new WP_Query();
$recent->query( 'cat=1&showposts=5' );
$is_first_post = true;
$html = '';
while( $recent->have_posts() ) : $recent->the_post();
$html .='<ul><li>';
if ( $is_first_post && has_post_thumbnail() ) {
$html .=get_the_post_thumbnail();
$is_first_post = false;
}
$html .='<a href="'.get_the_permalink().'">';
$html .=get_the_title();
$html .='</a></li></ul>';
endwhile;
return $html;
}
add_shortcode( 'recentpost', 'recentpost_shortcode_func' );

WP: acf image fields & simple lightbox plugin integration

I'm trying to integrate the Simple Lightbox plugin into Advanced Custom Fields image fields.
Basically I want any images on a page (populated via acf image fields) to open in the lightbox when clicked, and also be grouped together with all other images within the lightbox so they can be navigated through like a slideshow. This is built-in functionality for the Simple Lightbox plugin, but I don't know how to integrate it with my template code for image fields.
For acf WYSIWYG fields I'm using this...
<?php
$content = get_field('wysiwyg');
if ( function_exists('slb_activate') )
$content = slb_activate($content);
echo $content;
?>
...which works great, but adapting that to image fields is eluding me.
Here's my template code for image fields (without any attempts at integrating Simple Lightbox):
<?php
$image = get_field( 'img' );
if( ! empty( $image ) ) {
$url = $image['url'];
$alt = $image['alt'];
$caption = $image['caption'];
$size = 'large';
$thumb = $image['sizes'][ $size ];
$width = $image['sizes'][ $size . '-width' ];
?>
<?php if( $caption ): ?>
<div class="wp-caption" style="width: <?php echo $width; ?>px">
<?php endif; ?>
<a href="<?php echo $url; ?>">
<img src="<?php echo $thumb; ?>" alt="<?php echo $alt; ?>" />
</a>
<?php if( $caption ): ?>
<p class="wp-caption-text"><?php echo $caption; ?></p>
</div>
<?php endif; ?>
<?php } ?>
Any suggestions for how to integrate Simple Lightbox into this? Thanks in advance!
Use php output buffering to store your template in a variable. Put ob_start() at the top, then at the bottom ob_get_clean() will stop the output buffering, and you can save the return value to a variable. Then pass that to slb_activate.
<?php
ob_start();
$image = get_field( 'img' );
if( ! empty( $image ) ) {
$url = $image['url'];
$alt = $image['alt'];
$caption = $image['caption'];
$size = 'large';
$thumb = $image['sizes'][ $size ];
$width = $image['sizes'][ $size . '-width' ];
?>
<?php if( $caption ): ?>
<div class="wp-caption" style="width: <?php echo $width; ?>px">
<?php endif; ?>
<a href="<?php echo $url; ?>">
<img src="<?php echo $thumb; ?>" alt="<?php echo $alt; ?>" />
</a>
<?php if( $caption ): ?>
<p class="wp-caption-text"><?php echo $caption; ?></p>
</div>
<?php endif; ?>
<?php } ?>
<?php $content = ob_get_clean(); ?>
<?php echo slb_activate($content); ?>

how to display portfolio category above the content in loop portfolio page

i want to display portfolio category above the content where all portfolio is displayed. i know some of the modification is required in loop portfolio.php but how put that so that it display above the content post. i want to display in ul li format how it possible?? i m using broadscope theme
my code for loop portfolio.php is given below
<?php
global $avia_config;
if(isset($avia_config['new_query'])) {
query_posts($avia_config['new_query']);
}
// check if we got a page to display:
if (have_posts()) :
$loop_counter = 1;
$extraClass = 'first';
//iterate over the posts
while (have_posts()) : the_post();
//get the categories for each post and create a string that serves as classes so the javascript can sort by those classes
$sort_classes = "";
$item_categories = get_the_terms( $id, 'portfolio_entries' );
if(is_object($item_categories) || is_array($item_categories))
{
foreach ($item_categories as $cat)
{
$sort_classes .= $cat->slug.'_sort ';
}
}
?>
<div class='post-entry one_third all_sort <?php echo $sort_classes.$extraClass; ?>'>
<?php echo avia_inc_display_featured_within_entry('portfolio', false); ?>
<?php the_title(); ?>
<span class='date_sort hidden'><?php the_time('Y m d H i s'); ?></span>
<div class="entry-content">
<!--<?php the_excerpt(); ?> -->
<!--<a class="more-link" href="<?php echo get_permalink(); ?>"><?php _e('Read more','avia_framework'); ?></a>
-->
</div>
<!-- end post-entry-->
</div>
<?php
$loop_counter++;
$extraClass = "";
if($loop_counter > 3)
{
$loop_counter = 1;
$extraClass = 'first';
}
endwhile;
else:
?>
<div class="entry">
<h1 class='post-title'><?php _e('Nothing Found', 'avia_framework'); ?></h1>
<p><?php _e('Sorry, no posts matched your criteria', 'avia_framework'); ?></p>
</div>
<?php
endif;
?>
i want to display portfolio category before
<a href="<?php echo get_permalink() ?>" rel="bookmark" title="<?php
_e('Permanent Link:','avia_framework')?> <?php echo avia_inc_display_featured_within_entry('portfolio', false); ?>">
inside
<div class='post-entry one_third all_sort <?php echo
$sort_classes.$extraClass; ?>'>
so please help me if anyone know this. hope friends you help me regarding this question
Try to insert the following code
<?php
$post_id = get_the_ID()
$item_terms = get_the_terms( $post_id, 'portfolio_entries' );
if($item_terms !== false && count($item_terms) > 0)
{
echo '<ul>';
foreach ($item_terms as $term)
{
echo '<li>'.$term->name.'</li>';
}
echo '</ul>';
}
?>
just after
<div class='post-entry one_third all_sort <?php echo $sort_classes.$extraClass; ?>'>

Resources