WordPress show posts within while() loop based on condition - php

I have 8 posts for example.
Here 3 posts out of 8 posts is sticky and 2 posts has paid_placement meta key out of those 3 posts
Now, within while loop I want to show:
1) those posts on top which has is_sticky() === true and don't have any paid_placement meta key.
2) After that those posts which has is_sticky() === true and have paid_placement meta key.
2) After that custom post type posts ( pro_event as post type).
3) after that all other posts
Now, I am very much confused how to make it happend in WordPress while() loop. Can you guys help me?
My code:
while ( have_posts() ) {
the_post();
if ( is_sticky() && false === ( 'paid' == strtolower( get_post_meta( get_the_ID(), 'paid_placement')[0] ) ) ) {
the_title();
echo '<hr/>';
continue;
}
the_title();
echo '<hr/>';
}
Updated:
while ( have_posts() ) {
the_post();
if ( is_sticky() && false === ( 'paid' == strtolower( get_post_meta( get_the_ID(), 'paid_placement')[0] ) ) ) {
//the_title();
var_dump('paid' == strtolower( get_post_meta( get_the_ID(), 'paid_placement')[0] ) );
echo '<hr/>';
continue;
}
//the_title();
var_dump('paid' == strtolower( get_post_meta( get_the_ID(), 'paid_placement')[0] ) );
echo '<hr/>';
}
output:
bool(true)
bool(false)
bool(true)
bool(false)
bool(false)
bool(false)
bool(false)

Related

2 Column DIV/Table for Taxonomies - Woocommerce

I am using 10 custom taxonomies for woocommerce in wordpress and would like to display them in a 2 column div or table on the product page. Not every taxonomy is completed for every item, some may have all and some may only have 1.
At the moment I have created the first column (to show the taxonomy name) using
echo '<div class="tasting-notes">';
if( false != get_the_term_list( $post->ID, 'classifications' ) ) { echo '<li>Classification:</li>';}
if( false != get_the_term_list( $post->ID, 'producers' ) ) { echo '<li>Producer:</li>';}
if( false != get_the_term_list( $post->ID, 'milk_source' ) ) { echo '<li>Milk Type:</li>';}
if( false != get_the_term_list( $post->ID, 'food_pairings' ) ) { echo '<li>Food Pairing:</li> ';}
if( false != get_the_term_list( $post->ID, 'drink_pairings' ) ) { echo '<li>Drink Pairing:</li>';}
if( false != get_the_term_list( $post->ID, 'distribution' ) ) { echo '<li>Distribution:</li> ';}
echo '</div>';
and the second column (to show taxonomy result(s) ) using
echo '<div class="tasting-notes">';
if( false != get_the_term_list( $post->ID, 'classifications' ) ) { echo '<li>' . get_the_term_list($post->ID,'classifications', '', ', ' );}
if( false != get_the_term_list( $post->ID, 'producers' ) ) { echo '<li>' . get_the_term_list($post->ID,'producers', '', ', ' );}
if( false != get_the_term_list( $post->ID, 'food_pairings' ) ) { echo '<li>' . get_the_term_list($post->ID,'food_pairings', '', ' | ' );}
if( false != get_the_term_list( $post->ID, 'drink_pairings' ) ) { echo '<li>' . get_the_term_list($post->ID,'drink_pairings', '', ' | ' );}
if( false != get_the_term_list( $post->ID, 'distribution' ) ) { echo '<li>' . get_the_term_list($post->ID,'distribution', '', ' | ' );}
echo '</div>';
This seems to work ok, except my problem is when one of the taxonomies wraps into a second line, it wraps column 2 but not column 1. So the taxonomy headers and results do not align.
I also tried to return both the taxonomy header and name in the one query using the following
if( false != get_the_term_list( $post->ID, 'producers' ) ) { echo 'ProducerS:' . get_the_term_list($post->ID,'producers', '', ', ' );}
That also works except when one of the results is empty and I can see the blank div. I tried with tables and ran into the same errors.
Please explain how to show 2 columns with Taxonomy and matching results that is also fluid and allows for empty taxonomies.
Example image with food pairing not wrapping correctly ↓
This made more sense in my head.
Thanks,
Richard

Wordpress PHP dynamic sidebar generating a "1" after the code?

I am using the following code to display widgets by permalinks...
<?php
global $post;
$permalink = get_permalink( $post->ID );
if ( strpos( $permalink, '/one/' ) !== false || strpos( $permalink, '/one-two/' ) !== false ) {
echo dynamic_sidebar( 'disclaimer_cop' );
} elseif ( strpos( $permalink, '/two/' ) !== false || strpos( $permalink, '/two-one/' ) !== false ) {
echo dynamic_sidebar( 'disclaimer' );
} ?>
This code is dynamically displaying the widgets on the correct pages but when viewing the page the widget position is being followed by a random "1" in the HTML, like this.
<div class="widget_text disclaimer"> <div class="textwidget custom-html-widget">*Disclaimer text.</div>
1
Any ideas as to what is wrong?
Remove the echo. Looks like you're outputting the result as well as the content.
<?php
global $post;
$permalink = get_permalink( $post->ID );
if ( strpos( $permalink, '/one/' ) !== false || strpos( $permalink, '/one-two/' ) !== false ) {
dynamic_sidebar( 'disclaimer_cop' );
} elseif ( strpos( $permalink, '/two/' ) !== false || strpos( $permalink, '/two-one/' ) !== false ) {
dynamic_sidebar( 'disclaimer' );
}
?>
I am not sure how you are implentening on your case the code you specified. If you do it via add_shortcode() and then you call your shortcode on a widget you will just need to change the echo with a return.
Hope that helps.
Cheers!

Add CSS Style to Wordpress Sidebar on Article Page

I need to add a unique CSS style to the articles and single pages sidebar on my site to diferenciate from the home page. The sidebar already has a style called ".sidebar" and it works when I change it, but it affects all the site sidebars.
I've been looking at the sidebars.php file and see there are "if" lines for home or single pages, but don't know what to change. Keep in mind I'm not a programmer.
The code I think should be modified is (sidebar.php):
<aside class="sidebar">
<?php
wp_reset_query();
if ( is_home() ){
$sidebar_home = tie_get_option( 'sidebar_home' );
if( $sidebar_home )
dynamic_sidebar ( sanitize_title( $sidebar_home ) );
else dynamic_sidebar( 'primary-widget-area' );
}elseif( is_page() ){
global $get_meta;
$tie_sidebar_pos = $get_meta["tie_sidebar_pos"][0];
if( $tie_sidebar_pos != 'full' ){
$tie_sidebar_post = sanitize_title($get_meta["tie_sidebar_post"][0]);
$sidebar_page = tie_get_option( 'sidebar_page' );
if( $tie_sidebar_post )
dynamic_sidebar($tie_sidebar_post);
elseif( $sidebar_page )
dynamic_sidebar ( sanitize_title( $sidebar_page ) );
else dynamic_sidebar( 'primary-widget-area' );
}
}elseif ( is_single() ){
global $get_meta;
$tie_sidebar_pos = $get_meta["tie_sidebar_pos"][0];
if( $tie_sidebar_pos != 'full' ){
$tie_sidebar_post = sanitize_title($get_meta["tie_sidebar_post"][0]);
$sidebar_post = tie_get_option( 'sidebar_post' );
if( $tie_sidebar_post )
dynamic_sidebar($tie_sidebar_post);
elseif( $sidebar_post )
dynamic_sidebar ( sanitize_title( $sidebar_post ) );
else dynamic_sidebar( 'primary-widget-area' );
}
}elseif ( is_category() ){
$category_id = get_query_var('cat') ;
$cat_sidebar = tie_get_option( 'sidebar_cat_'.$category_id ) ;
$sidebar_archive = tie_get_option( 'sidebar_archive' );
if( $cat_sidebar )
dynamic_sidebar ( sanitize_title( $cat_sidebar ) );
elseif( $sidebar_archive )
dynamic_sidebar ( sanitize_title( $sidebar_archive ) );
else dynamic_sidebar( 'primary-widget-area' );
}else{
$sidebar_archive = tie_get_option( 'sidebar_archive' );
if( $sidebar_archive ){
dynamic_sidebar ( sanitize_title( $sidebar_archive ) );
}
else dynamic_sidebar( 'primary-widget-area' );
}
?>
</aside>
Thanks in advance for any help solving this.
your article should have a unique page or post id, have a look at the page/post source code to get the id. Your css will look something like #post_455 .sidebar {
/* your unique article styles */
}
ps - you shouldn't need to play around with sidebar.php

Show post custom field from function template

I'm using the Avada theme in wordpress. I'm trying to add some post custom fields into the scrollover (shown by mouse hover). I tried to edit the template file that controls the scrollover but without success. I got an error message invalid ">".
I tried to use this code to show the custom field:
<?php echo get_post_meta( $post_id,'the_price',); ?>
The Avada function template is as follows:
if ( ! function_exists( 'avada_render_rollover' ) ) {
function avada_render_rollover( $post_id, $post_permalink = '', $display_woo_price = false, $display_woo_buttons = false, $display_post_categories = 'default', $display_post_title = 'default', $gallery_id = '' ) {
// Retrieve the permalink if it is not set
if ( ! $post_permalink ) {
$post_permalink = get_permalink( $post_id );
}
// Check if theme options are used as base or if there is an override for post categories
if ( 'enable' == $display_post_categories ) {
$display_post_categories = true;
} elseif ( 'disable' == $display_post_categories ) {
$display_post_categories = false;
} else {
$display_post_categories = ! Avada()->settings->get( 'cats_image_rollover' );
}
// Check if theme options are used as base or if there is an override for post title
if ( 'enable' == $display_post_title ) {
$display_post_title = true;
} elseif ( 'disable' == $display_post_title ) {
$display_post_title = false;
} else {
$display_post_title = ! Avada()->settings->get( 'title_image_rollover' );
}
// Set the link on the link icon to a custom url if set in page options
$icon_permalink = ( fusion_get_page_option( 'link_icon_url', $post_id ) != null ) ? fusion_get_page_option( 'link_icon_url', $post_id ) : $post_permalink;
if ( '' == fusion_get_page_option( 'image_rollover_icons', $post_id ) || 'default' == fusion_get_page_option( 'image_rollover_icons', $post_id ) ) {
if( ! Avada()->settings->get( 'link_image_rollover' ) && ! Avada()->settings->get( 'zoom_image_rollover' ) ) { // link + zoom
$image_rollover_icons = 'linkzoom';
} elseif( ! Avada()->settings->get( 'link_image_rollover' ) && Avada()->settings->get( 'zoom_image_rollover' ) ) { // link
$image_rollover_icons = 'link';
} elseif( Avada()->settings->get( 'link_image_rollover' ) && ! Avada()->settings->get( 'zoom_image_rollover' ) ) { // zoom
$image_rollover_icons = 'zoom';
} elseif( Avada()->settings->get( 'link_image_rollover' ) && Avada()->settings->get( 'zoom_image_rollover' ) ) { // link
$image_rollover_icons = 'no';
} else {
$image_rollover_icons = 'linkzoom';
}
} else {
$image_rollover_icons = fusion_get_page_option( 'image_rollover_icons', $post_id );
}
// Set the link target to blank if the option is set
$link_target = ( 'yes' == fusion_get_page_option( 'link_icon_target', $post_id ) || 'yes' == fusion_get_page_option( 'post_links_target', $post_id ) ) ? ' target="_blank"' : '';
?>
<div class="fusion-rollover">
<div class="fusion-rollover-content">
<?php if ( 'no' != $image_rollover_icons && 'product' != get_post_type( $post_id ) ) : // Check if rollover icons should be displayed ?>
<?php if ( 'zoom' != $image_rollover_icons ) : // If set, render the rollover link icon ?>
<a class="fusion-rollover-link" href="<?php echo $icon_permalink; ?>"<?php echo $link_target; ?>>Permalink</a>
<?php endif; ?>
<?php if ( 'link' != $image_rollover_icons ) : // If set, render the rollover zoom icon ?>
<?php
// Get the image data
$full_image = wp_get_attachment_image_src( get_post_thumbnail_id( $post_id ), 'full' );
if ( ! is_array( $full_image ) ) {
$full_image = array();
$full_image[0] = '';
}
// If a video url is set in the post options, use it inside the lightbox
if ( fusion_get_page_option( 'video_url', $post_id ) ) {
$full_image[0] = fusion_get_page_option( 'video_url', $post_id );
}
?>
<?php if ( 'linkzoom' == $image_rollover_icons || '' === $image_rollover_icons ) : // If both icons will be shown, add a separator ?>
<div class="fusion-rollover-sep"></div>
<?php endif; ?>
<?php if ( $full_image[0] ) : // Render the rollover zoom icon if we have an image ?>
<?php
// Only show images of the clicked post
if ( 'individual' == Avada()->settings->get( 'lightbox_behavior' ) ) {
$lightbox_content = avada_featured_images_lightbox( $post_id );
$data_rel = sprintf( 'iLightbox[gallery%s]', $post_id );
// Show the first image of every post on the archive page
} else {
$lightbox_content = '';
$data_rel = sprintf( 'iLightbox[gallery%s]', $gallery_id );
}
?>
<a class="fusion-rollover-gallery" href="<?php echo $full_image[0]; ?>" data-id="<?php echo $post_id; ?>" data-rel="<?php echo $data_rel; ?>" data-title="<?php echo get_post_field( 'post_title', get_post_thumbnail_id( $post_id ) ); ?>" data-caption="<?php echo get_post_field( 'post_excerpt', get_post_thumbnail_id( $post_id ) ); ?>">Gallery</a><?php echo $lightbox_content; ?>
<?php endif; ?>
<?php endif; ?>
<?php endif; ?>
<?php if ( $display_post_title ) : // Check if we should render the post title on the rollover ?>
<h4 class="fusion-rollover-title"><a href="<?php echo $icon_permalink; ?>"<?php echo $link_target; ?>><?php echo get_the_title( $post_id ); ?></a></h4>
<?php endif; ?>
<?php
// Check if we should render the post categories on the rollover
if ( $display_post_categories ) {
// Determine the correct taxonomy
if ( 'post' == get_post_type( $post_id ) ) {
$post_taxonomy = 'category';
} elseif ( 'avada_portfolio' == get_post_type( $post_id ) ) {
$post_taxonomy = 'portfolio_category';
} elseif ( 'product' == get_post_type( $post_id ) ) {
$post_taxonomy = 'product_cat';
}
echo get_the_term_list( $post_id, $post_taxonomy, '<div class="fusion-rollover-categories">', ', ', '</div>' );
}
?>
<?php if ( $display_woo_price ) : // Check if we should render the woo product price ?>
<?php woocommerce_get_template( 'loop/price.php' ); ?>
<?php endif; ?>
<?php if ( $display_woo_buttons ) : // Check if we should render the woo "add to cart" and "details" buttons ?>
<div class="fusion-product-buttons">
<?php woocommerce_get_template( 'loop/add-to-cart.php' ); ?>
<span class="fusion-rollover-linebreak"></span>
<a class="fusion-show-details-button" href="<?php echo post_permalink(); ?>"><?php _e( 'Details', 'Avada' ); ?></a>
</div>
<?php endif; ?>
</div>
</div>
<?php
}
}
Usually to use $post_id you need first to declare global $post

Insert featured image into post content

I am using a wordpress theme for my website and I'm trying to alter the php so that the featured image is displayed within the post content instead of before or after (the top of the post or the bottom)
Here's the original code:
<?php if ( ( has_post_thumbnail( $post_id ) || '' != get_post_meta( $post_id, 'Thumbnail', true ) ) && 'on' == et_get_option( 'origin_thumbnails' ) ) { ?>
<div class="post-thumbnail">
<?php
if ( has_post_thumbnail( $post_id ) ) the_post_thumbnail( 'full' );
else printf( '<img src="%1$s" alt="%2$s" />', esc_attr( get_post_meta( $post_id, 'Thumbnail', true ) ), the_title_attribute( array( 'echo' => 0 ) ) );
?>
</div> <!-- end .post-thumbnail -->
<?php } ?>
<?php the_content(); ?>
Here's how I tweaked it in my child theme:
<?php the_content(); ?>
<?php if ( ( has_post_thumbnail( $post_id ) || '' != get_post_meta( $post_id, 'Thumbnail', true ) ) && 'on' == et_get_option( 'origin_thumbnails' ) ) { ?>
<div class="post-thumbnail">
<?php
if ( has_post_thumbnail( $post_id ) ) the_post_thumbnail( 'full' );
else printf( '<img src="%1$s" alt="%2$s" />', esc_attr( get_post_meta( $post_id, 'Thumbnail', true ) ), the_title_attribute( array( 'echo' => 0 ) ) );
?>
</div>
I'm not very fluent in php and all that accomplished was moving the featured image to the bottom of the post :-/. I posted to my theme's forum and was given the following code which didn't seem to do anything.
add_filter( 'the_content', insert_featured_image, 20 );
function insert_featured_image( $content ) {
$content = preg_replace( "/<\/p>/", "</p>" . get_the_post_thumbnail($post->ID, 'post-single'), $content, 1 );
return $content;
}
Can anyone point me in the right direction?
TIA!
What is the reasoning behind attempting to move the featured image into the content? The Wordpress codex gives the following as an example:
// check if the post has a Post Thumbnail assigned to it.
if ( has_post_thumbnail() ) {
the_post_thumbnail();
}
the_content();
Basically, the_post_thumbnail(); will display the image that was selected as the featured image while the_content(); is simply displaying what was inputted into the wysiwyg for that post or page.
If you're attempting to do this so that it matches a design or layout, you might want to look at applying addition markup and styles to this area. If you require text before and after the featured image you should look into custom fields.

Categories