I am having some trouble trying to add an if statement to hide an ACF field inside of an HTML echo.
<?php
$link = get_permalink();
$availability = get_field('availability');
$delivery_date = get_field('delivery_date');
$url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
if ( has_post_thumbnail() ) {
echo '<a href="' .$link. '">
<div class="thumbnail" style="background: url('.$url.')">
<div class="tags one">
<span class="availability">' .$availability. '</span>
'if( get_field('delivery_date') ):' <span class="delivery-date">' .$delivery_date. '</span> 'endif;'
</div>
</div>
</a>';
}
?>
Please would someone be able to advise on where I'm going wrong with the if statement to hide the field if it's empty? At the moment it just errors the page.
Break the PHP code out of the text string, this is one way
<?php
$link = get_permalink();
$availability = get_field('availability');
$delivery_date = get_field('delivery_date');
$url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
if ( has_post_thumbnail() ) {
echo '<a href="' .$link. '">
<div class="thumbnail" style="background: url('.$url.')">
<div class="tags one">
<span class="availability">' .$availability. '</span>';
if( get_field('delivery_date') ):
echo '<span class="delivery-date">' .$delivery_date. '</span>';
endif;
echo '</div>
</div>
</a>';
}
?>
Related
Right now the code below generates the content seen here:
Currently the title has to be clicked for the post page to be loaded, but I want to allow the entire background image to be clickable. Is this possible? I've tried surrounding the card div with the tag from the title code, but that still did not let me click the background image like a link.
<article id="post-<?php the_ID(); ?>" <?php post_class('card-box col-lg-4 col-md-6 col-sm-12 col-xs-12'); ?>>
<div class="card" data-background="image" data-src="<?php esc_url( the_post_thumbnail_url( 'large' ) ); ?>">
<div class="header">
<?php
$categories = get_the_category();
if ( ! empty( $categories ) ) {
?>
<div class="category">
<h6>
<span class="category">
<?php echo '<a class="category" href="' . esc_url( get_category_link( $categories[0]->term_id ) ) . '">' . esc_html( $categories[0]->name ) . '</a>'; ?>
</span>
</h6>
</div>
<?php } ?>
</div>
<div class="content">
<?php the_title( '<h4 class="entry-title">', '</h4>' ); ?>
<span class="date"><?php echo esc_html( get_the_date() ); ?></span>
</div>
<div class="filter"></div>
</div> <!-- end card -->
</article>
Try jQuery approach like below
$('.card').on('click', function(event){
var elementTag = event.target.tagName.toLowerCase();
if(elementTag === 'div') {
var pageurl = $(this).find('.entry-title a').attr('href');
window.location.href = pageurl;
}
});`
The following function works by adding it to >the_content() , however, I want to display it at a custom location in my html markup via a custom tag, when I tried this I got a PHP error, I'm sure this is something very simple but I for the life of me cannot see the issue.
// code from crunchify
function supersun_social_sharing_buttons($content) {
global $post;
if(is_singular() || is_home()){
// Get current page URL
$supersunURL = urlencode(get_permalink());
// Get current page title
$supersunTitle = str_replace( ' ', '%20', get_the_title());
// Get Post Thumbnail for pinterest
$supersunThumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
// Construct sharing URL without using any script
$twitterURL = 'https://twitter.com/intent/tweet?text='.$supersunTitle.'&url='.$supersunURL.'&via=Crunchify';
$facebookURL = 'https://www.facebook.com/sharer/sharer.php?u='.$supersunURL;
$googleURL = 'https://plus.google.com/share?url='.$supersunURL;
$whatsappURL = 'whatsapp://send?text='.$supersunTitle . ' ' . $supersunURL;
$pinterestURL = 'https://pinterest.com/pin/create/button/?url='.$supersunURL.'&media='.$supersunThumbnail[0].'&description='.$supersunTitle;
// Add sharing button at the end of page/page content
$variable .= '<div class="supersun-social">';
$variable .= '<a class="supersun-social-link supersun-twitter" href="'. $twitterURL .'" target="_blank" title="Share on Twitter"><i class="fa fa-twitter" aria-hidden="true"></i></a>';
$variable .= '<a class="supersun-social-link supersun-facebook" href="'.$facebookURL.'" target="_blank" title="Share on Facebook"><i class="fa fa-facebook" aria-hidden="true"></i></a>';
$variable .= '<a class="supersun-social-link supersun-whatsapp" href="'.$whatsappURL.'" target="_blank" title="Share on Whatsapp"><i class="fa fa-whatsapp" aria-hidden="true"></i></a>';
$variable .= '<a class="supersun-social-link supersun-googleplus" href="'.$googleURL.'" target="_blank" title="Share on Google+"><i class="fa fa-google-plus" aria-hidden="true"></i></a>';
$variable .= '<a class="supersun-social-link supersun-pinterest" href="'.$pinterestURL.'" target="_blank" title="Share on Pinterest"><i class="fa fa-pinterest" aria-hidden="true"></i></a>';
$variable .= '</div>';
return $variable.$content;
}else{
// if not a post/page then don't include sharing button
return $variable.$content;
}
};
add_filter( 'the_content', 'supersun_social_sharing_buttons');
THIS ERROR MESSAGE APPEARS WHEN I ADD
<?php supersun_social_sharing_buttons(); ?>
TO MY THEME.
Thank you so much for your time. Sorry the site is not currently live, I think this should be an easy fix for someone with an experienced eye.
<?php
/**
* The template for displaying all single posts and attachments
*
* #package FoundationPress
* #since FoundationPress 1.0.0
*/
get_header(); ?>
<div class="post-container">
<div class="blog-single-header-background" id="blog-single-header-background">
</div>
<?php supersun_social_sharing_buttons($content); ?>
<div class="container post-content-container">
<div class="columns medium-10 medium-centered">
<div itemscope itemtype="http://schema.org/Article" class="single-blog-post" role="main">
<?php while ( have_posts() ) : the_post(); ?>
<article <?php post_class('main-content') ?> id="post-<?php the_ID(); ?>">
<header class="post-header">
<h1 itemprop="name" class="entry-title"><?php the_title(); ?></h1>
<div class="post-author-details">
<a href="<?php get_the_author_link(); ?>" class="post-author-avatar image-circle float-left">
<?php echo get_avatar( get_the_author_meta( 'ID' ), 64 ); ?>
</a>
<div class="post-author-meta media-body margin-left">
<h4 class="author-post-byline">
<span> By </span>
<span itemprop="author" itemscope itemtype="http://schema.org/Person">
<span itemprop="name" class="author-post-link"><?php the_author_posts_link(); ?></span>
</span>
<span class="post-date">
on <?php echo the_time("M j, Y");?>
</span>
</h4>
<span class="post-meta-wrap">
<span class="post-category">
<span class="pre-cat"> In
</span>
<?php $categories = get_the_category();
if ( ! empty( $categories ) ) {
echo '' . esc_html( $categories[0]->name ) . '';
}?>
</span>
</span>
</div>
</div>
<div class="clear"></div>
</header>
<div class="post-content">
<span itemprop="articleBody">
<div class="post-image">
<?php echo the_post_thumbnail( 'large' ); ?>
</div>
<?php the_content(); ?>
</span>
</div>
<footer>
<div class="post-author-about-section">
<?php
global $post;
// Detect if it is a single post with a post author
if ( is_single() && isset( $post->post_author ) ) {
// Get author's display name
$display_name = get_the_author_meta( 'display_name', $post->post_author );
// If display name is not available then use nickname as display name
if ( empty( $display_name ) )
$display_name = get_the_author_meta( 'nickname', $post->post_author );
// Get author's biographical information or description
$user_description = get_the_author_meta( 'user_description', $post->post_author );
// Get author's website URL
$user_website = get_the_author_meta('url', $post->post_author);
// Get link to the author archive page
$user_posts = get_author_posts_url( get_the_author_meta( 'ID' , $post->post_author));
if ( ! empty( $user_description ) )
// Author avatar and bio
$author_details = '' . get_avatar( get_the_author_meta('user_email') , 90 ) . '';
$author_details .= '<div class="media-body margin-left">';
if ( ! empty( $display_name ) )
// $author_details = '<p class="author-about-name">About ' . $display_name . '</p>';
$author_details .= '<h4 class="author-post-byline author-about-name">';
$author_details .= '<span class="pre-author-header"> About </span>';
$author_details .= '<span itemprop="author" itemscope itemtype="http://schema.org/Person">';
$author_details .= '<span itemprop="name" class="author-post-link">' . $display_name . '</span>';
$author_details .= '</span>';
$author_details .= '</h4>';
$author_details .= '<p class="author-about-details">' . nl2br( $user_description ). '</p>';
// $author_details .= '<p class="author-about-links">View all posts by ' . $display_name . '';
// Check if author has a website in their profile
if ( ! empty( $user_website ) ) {
// Display author website link
$author_details .= ' | Website</p>';
} else {
// if there is no author website then just close the paragraph
$author_details .= '</p>';
}
$author_details .= '</div">';
// Pass all this info to post content
$content = $content . $author_details;
}
echo $content;
?>
</div>
</footer>
</article>
</div>
<?php endwhile;?>
<div class="related-posts">
<?php $orig_post = $post;
global $post;
$tags = wp_get_post_tags($post->ID);
if ($tags) {
$tag_ids = array();
foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
$args=array(
'tag__in' => $tag_ids,
'post__not_in' => array($post->ID),
'posts_per_page'=>2, // Number of related posts that will be shown.
'caller_get_posts'=>1
);
$my_query = new wp_query( $args );
if( $my_query->have_posts() ) {
echo '<div id="relatedposts"><h3>Related Posts</h3>';
while( $my_query->have_posts() ) {
$my_query->the_post(); ?>
<div class="columns medium-6 large-6 collapse margin-bottom">
<a href="<?php the_permalink()?>" rel="bookmark" title="<?php the_title(); ?>">
<div class="related-post-item content-card">
<div class="related-thumb">
<?php the_post_thumbnail(); ?>
</div>
<div class="related-content">
<div class="related-category-title">
<?php $categories = get_the_category();
if ( ! empty( $categories ) ) {
echo esc_html( $categories[0]->name );
}?>
</div>
<h2>
<?php the_title(); ?>
</h2>
</div>
</div>
</a>
</div>
<?php }
echo '</div>';
}
}
$post = $orig_post;
wp_reset_query(); ?>
</div>
<div class="clear"> </div>
<!-- <h3> Comments </h3> -->
<div class="comments">
<?php do_action( 'foundationpress_post_before_comments' ); ?>
<?php comments_template(); ?>
<?php do_action( 'foundationpress_post_after_comments' ); ?>
</div>
</div>
</div>
</div>
<?php get_footer();
You need pass a parameter when you call your function directly, not in add_filter. Like this <?php supersun_social_sharing_buttons($parameter); ?>
I'm building an archive page in WordPress that is pulling content from certain page IDs. Is there a way to create a variable that would go in place of 266 in the code below so I don't have to recreate this code for each ID?
<div class="col-md-4">
<div class="ih-item square effect6 from_top_and_bottom">
<a href="<?php echo get_the_permalink( 266 ); ?>">
<div class="img">
<img src="<?php the_field('photo', 266); ?>" />
</div>
<div class="info">
<h4><?php echo get_the_title( 266 ); ?></h4>
</div>
</a>
</div>
</div>
Wrap you code in a function and place that function to functions.php
and just call that function instead of recreating code again.
E.g.
functions.php
<?php
function your_function_name( $page_id ) {
if ( ! $page_id ) {
return;
}
$content = '<div class="col-md-4">
<div class="ih-item square effect6 from_top_and_bottom">
<a href="' . get_the_permalink( $page_id ) . '">
<div class="img">
<img src="' . the_field('photo', $page_id) . '" />
</div>
<div class="info">
<h4>' . get_the_title( $page_id ) . '</h4>
</div>
</a>
</div>
</div>';
return $content
}
?>
archive.php
<?php echo your_function_name( 266 ); ?>
I have this code:
<div class="col-md-4 col-sm-4 col-xs-12 mob">
<?php
$args = array('tag_slug__and' => array('testtag'));
$loop = new WP_Query( $args );
while ($loop->have_posts() ) : $loop->the_post();
?>
<a style="color:#333; text-decoration:none;" href="<?php echo get_permalink(); ?>">
<?php
if(has_post_thumbnail()) {
$image_src = wp_get_attachment_image_src( get_post_thumbnail_id(),'full' );
echo '<img src="' . $image_src[0] . '" width="100%" />';
}
?>
<h4><?php the_title(); ?></h4>
<?php the_excerpt(); ?>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
</a>
Which gets a post which has the tag 'testtag'.
Instead of copying this code, and using 'testtag1', 'testtag2'
How can I just grab ALL the posts which have the tag 'testtag' and just keep adding them into 4 columns?
Any links/help
Nevermind, simple fix!!
<div class="row" style="margin-top:20px;">
<?php
$args = array('tag_slug__and' => array('testtag'));
$loop = new WP_Query( $args );
while ($loop->have_posts() ) : $loop->the_post();
?>
<div class="col-md-4 col-sm-4 col-xs-12 mob">
<a style="color:#333; text-decoration:none;" href="<?php echo get_permalink(); ?>">
<?php
if(has_post_thumbnail()) {
$image_src = wp_get_attachment_image_src( get_post_thumbnail_id(),'full' );
echo '<img src="' . $image_src[0] . '" width="100%" />';
}
?>
<h4><?php the_title(); ?></h4>
<?php $trimexcerpt = get_the_excerpt();
$shortexcerpt = wp_trim_words( $trimexcerpt, $num_words = 10, $more = '… <br/> Read More ...' );
echo '<a style="color:#333; text-decoration:none;" href="' . get_permalink() . '"><p>' . $shortexcerpt . '</p></a>';
?>
</div>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
</a>
</div>
I have this code for displaying a shortcode.
<?php
function recent_posts_function() {
$mypost = array( 'post_type' => 'gallery_pictures', );
$loop = new WP_Query( $mypost );
?>
<div id="boxhover">
<?php while ( $loop->have_posts() ) : $loop->the_post();?>
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
<!--Fade-->
<?php $ddd = '<div class="mosaic-block fade">
<a href="'. $image[0] . '" data-fancybox-group="gallery" target="_blank" class="mosaic-overlay preview fancybox" title="' . the_title . '">
<div class="details">
<h4>' . the_title() . '</h4>
<p>' . the_content_rss('', TRUE, '', 30) . '</p>
<br/>
<div class="btt">VIEW</div>
</div>
</a>
<div class="mosaic-backdrop"><img src="' . $image[0] . '" alt="gallery thumbnail" /></div>
</div>';
endwhile; ?>
</div>
<?php
return $ddd;
}
function register_shortcodes(){
add_shortcode('gallery', 'recent_posts_function');
}
add_action( 'init', 'register_shortcodes');
as you can see from the above codes, the 'return $ddd' should return all the output from the loops that the 'while' process done but its display only one.
Im currently looking for a solution and would love to hear any suggestion, recommendations and ideas on how to do it. Thank in advance.
You need to add a [dot] before your = [equal] on the loop while.
This will cause each loop add content current with the previous.
<?php function recent_posts_function() {
$ddd = ''; //First declare the string var ?>
...
<?php while ( $loop->have_posts() ) : $loop->the_post();?>
<?php $ddd .= '<div class="mosaic-block fade">'; // Put a [dot] before sign symbol ?>
<?php endwhile; ?>
...
return $ddd;
...
<? php } ?>