Php code between Tab Shortcode - php

I want to insert php code in between tabby tabs shortcodes.
I am using a plugin tabby tab for tab view and have added this code in my theme template:
<?php echo do_shortcode('[tabby title="Gallary Name"]
name content
[tabby title="Images"]
[tabbyending]'); ?>
I want to use a custom fields gallery under images tab using code like this:
<?php echo do_shortcode('[tabby title="Gallary Name"]
name content
[tabby title="Images"]
<?php
$i = 0;
$images = get_field('vil_pics');
if( $images ): ?>
<div>
<ul>
<?php foreach( $images as $image ): ?>
<li<?php if ( $i % 3 == 0 ) echo ' class="break"' ?>>
<a href="<?php echo $image['url']; ?>">
<img src="<?php echo $image['sizes']['thumbnail']; ?>" alt="<?php echo $image['alt']; ?>" />
</a><p>.</p>
</li>
<?php endforeach; ?>
</ul></div>
<?php endif; ?>
[tabbyending]'); ?>
This code is not working, it's showing a blank page. How can I fix this?

Tabby uses a global variable to track what's going on, so I think either one of these will work. The first one is a little more straightforward, but the second one will definitely work.
Option 1: output everything in order:
echo do_shortcode( '[tabby title="Gallery Name"] name content' );
echo do_shortcode( '[tabby title="Images"]' );
// your php code as-is
$i = 0;
$images = get_field('vil_pics');
if( $images ): ?>
<div>
<ul>
<?php foreach( $images as $image ):
$i++ ?>
<li<?php if ( $i % 3 == 0 ) echo ' class="break"' ?>>
<a href="<?php echo $image['url']; ?>">
<img src="<?php echo $image['sizes']['thumbnail']; ?>" alt="<?php echo $image['alt']; ?>" />
</a><p>.</p>
</li>
<?php endforeach; ?>
</ul>
</div>
<?php endif;
echo do_shortcode( '[tabbyending]' );
or Option 2: save everything to a variable and output it all at once:
$output = '';
$output .= '[tabby title="Gallery Name"] name content';
$output .= '[tabby title="Images"]';
$i = 0;
$images = get_field('vil_pics');
if ( $images ) {
$output .= '<div><ul>';
foreach( $images as $image ) {
$i++;
$li_class = ( $i % 3 == 0 ) ? ' class="break"' : '';
$output .= '<li' . $li_class . '>';
$output .= '<a href="' . $image['url'] . '">';
$output .= '<img src="' . $image['sizes']['thumbnail'] . '" alt="' . $image['alt'] . '" />';
$output .= '</a><p>.</p></li>';
}
$output .= '</div></ul>';
}
$output .= '[tabbyending]';
echo do_shortcode( $output );
Note that I didn't see anything increasing $i so I added that. Everything else is as-is.

Related

Wordpress Advanced Custom Fields - Cleaner Way To Code This

I have added advanced custom fields to woocommerce category pages to help with SEO.
Is there a cleaner way to code this as a lot of excess probably don't need
<div class="full top-cat-seo">
<?php
$queriedObject=get_queried_object();
echo get_field('product_category_top_section_seo','product_cat_'.$queriedObject->term_id);
$link1 = get_field('link_url_1','product_cat_'.$queriedObject->term_id);
$link1name = get_field('link_url_1_name','product_cat_'.$queriedObject->term_id);
$link2 = get_field('link_url_2','product_cat_'.$queriedObject->term_id);
$link2name = get_field('link_url_2_name','product_cat_'.$queriedObject->term_id);
$link3 = get_field('link_url_3','product_cat_'.$queriedObject->term_id);
$link3name = get_field('link_url_3_name','product_cat_'.$queriedObject->term_id);
$link4 = get_field('link_url_4','product_cat_'.$queriedObject->term_id);
$link4name = get_field('link_url_4_name','product_cat_'.$queriedObject->term_id);
if( $link1 ): ?>
<a class="button" href="<?php echo esc_url( $link1 ); ?>"><?php echo esc_html( $link1name );?></a>
<?php endif; ?>
<?php if( $link2 ): ?>
<a class="button" href="<?php echo esc_url( $link2 ); ?>"><?php echo esc_html( $link2name );?></a>
<?php endif; ?>
<?php if( $link3 ): ?>
<a class="button" href="<?php echo esc_url( $link3 ); ?>"><?php echo esc_html( $link3name );?></a>
<?php endif; ?>
<?php if( $link4 ): ?>
<a class="button" href="<?php echo esc_url( $link4 ); ?>"><?php echo esc_html( $link4name );?></a>
<?php endif; ?>
</div>
there are 8 custom fields to create 4 buttons as per here
https://onepoundcrisps.com/cat/brand/kp/
Using a loop, you build the name of the field from the loop key. So instead of hardcoding link_url_1, you can use 'link_url_' . $i where $i is the loop index.
You can also fetch the link and only fetch the linkname if there is a value.
This means you do 1 set of code and any changes will be reflected on all elements...
$queriedObject = get_queried_object();
echo get_field('product_category_top_section_seo', 'product_cat_' . $queriedObject->term_id);
for ($i = 1; $i < 5; $i++) {
$link = get_field('link_url_' . $i, 'product_cat_' . $queriedObject->term_id);
if ($link) {
$linkName = get_field('link_url_' . $i . '_name', 'product_cat_' . $queriedObject->term_id);
echo '<a class="button" href="' . esc_url($link) . '">' . esc_html($linkName) . '</a>';
}
}
I've converted the HTML to an echo, if you feel more comfortable with your current version, you should just need to change the field names (just remove the number).
Instead of using a separate conditional statement for each link, you can use a loop to iterate over all the links and display them. Here's how it can be done:
<div class="full top-cat-seo">
<?php
$queriedObject = get_queried_object();
echo get_field('product_category_top_section_seo', 'product_cat_' . $queriedObject->term_id);
?>
<?php
for ($i = 1; $i <= 4; $i++) {
$link = get_field("link_url_{$i}", "product_cat_{$queriedObject->term_id}");
$linkname = get_field("link_url_{$i}_name", "product_cat_{$queriedObject->term_id}");
if ($link) {
?>
<a class="button" href="<?php echo esc_url($link); ?>"><?php echo esc_html($linkname); ?></a>
<?php
}
}
?>
</div>
Instead of using the get_field() function repeatedly, you can use the get_fields() function to retrieve all the fields for a specific product category in a single call, and then access the individual fields as an array. Here's how it can be done:
<div class="full top-cat-seo">
<?php
$queriedObject = get_queried_object();
$fields = get_fields("product_cat_{$queriedObject->term_id}");
echo $fields['product_category_top_section_seo'];
?>
<?php
for ($i = 1; $i <= 4; $i++) {
$link = $fields["link_url_{$i}"];
$linkname = $fields["link_url_{$i}_name"];
if ($link) {
?>
<a class="button" href="<?php echo esc_url($link); ?>"><?php echo esc_html($linkname); ?></a>
<?php
}
}
?>
</div>

How do I execute php code within a variable containing html

I assembled a Wordpress shortcode but it throws an error in the block editor: "Updating failed. The response is not a valid JSON response." Notwithstanding, the edits are saved. I've been told the reason I get the error is my "shortcode handler function is generating output. Such functions must collect all output into a variable which is returned."
Below are (1) the code that works but causes the error message and (2) my pseudo code to fix the problem by assigning the 'a href' to a variable $html, but doesn't.
(1)
function make_header() {
$args = array(
'posts_per_page' => 1,
'category_name' => 'headlines',
);
$q = new WP_Query( $args);
if ( $q->have_posts() ) {
while ( $q->have_posts() ) {
$q->the_post();
$featured_img_url = get_the_post_thumbnail_url(get_the_ID(),'full');
?>
<a href="<?php the_permalink() ?>">
<div><img src="<?php echo $featured_img_url; ?>" width="100%" /></div>
<h2>
<?php the_title(); ?>
</h2></a>
<?php
}
wp_reset_postdata();
}
}
add_shortcode('make_header', 'make_header');
(2)
$html = '
<a href="<?php the_permalink() ?>">
<div><img src="<?php echo $featured_img_url; ?>" width="100%" /></div>
<h2>
<?php
the_title(); ?> </h2></a>';
}
wp_reset_postdata();
return $html;
Thanks for your help.
Try below code
$html = ' <div><img src="'. echo $featured_img_url .'" width="100%" /></div> <h2>'. the_title().' </h2>';
the concatenation operator (‘.‘), which returns the concatenation of its right and left arguments. 
You could to use concatenation like so:
$html = '<a href="' . esc_url(get_the_permalink()) . '">';
$html .= '<div>';
$html .= '<img src="' . $featured_img_url . '" width="100%" />';
$html .= '</div>';
$html .= '<h2>' . get_the_title() . '</h2></a>';
Also, use get_the_permalink() and get_the_title() as these functions are returning their result instead of outputting it.
The full code would then look something like this:
function make_header() {
$html = '';
$args = array(
'posts_per_page' => 1,
'category_name' => 'headlines',
);
$q = new WP_Query( $args);
if ( $q->have_posts() ) {
while ( $q->have_posts() ) {
$q->the_post();
$featured_img_url = esc_url(get_the_post_thumbnail_url(get_the_ID(),'full'));
$html = '<a href="' . esc_url(get_the_permalink()) . '">';
$html .= '<div>';
$html .= '<img src="' . $featured_img_url . '" width="100%" />';
$html .= '</div>';
$html .= '<h2>' . get_the_title() . '</h2></a>';
}
wp_reset_postdata();
}
return $html;
}
add_shortcode('make_header', 'make_header');

WP Loop Geo + jQuery .hide() not working correctly

I have a WP loop where each post has a collection of 4 country based images (using ACF).
I only would like to output 1 image per country, however it is displaying all of 4 images per post.
<?php
$args = array( 'post_type' => 'quick_links', 'posts_per_page' => 3 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$image_au = get_field("au_image");
$image_nz = get_field("nz_image");
$image_us = get_field("us_image");
$image_gl = get_field("global_image"); //default image
?>
<script type="text/javascript">
var image_au = <?php echo json_encode($image_au['url']); ?>;
var image_nz = <?php echo json_encode($image_nz['url']); ?>;
var image_us = <?php echo json_encode($image_us['url']); ?>;
var image_gl = <?php echo json_encode($image_gl['url']); ?>;
jQuery.get("http://ipinfo.io", function (response) {
if (response.country === "AU"){
jQuery("#resultQLAU").show();
jQuery("#resultQLNZ").hide();
jQuery("#resultQLUS").hide();
jQuery("#resultQLGlobal").hide();
} else if(response.country === "NZ"){
jQuery("#resultQLNZ").show();
jQuery("#resultQLAU").hide();
jQuery("#resultQLUS").hide();
jQuery("#resultQLGlobal").hide();
} else if(response.country === "US"){
jQuery("#resultQLUS").show();
jQuery("#resultQLNZ").hide();
jQuery("#resultQLAU").hide();
jQuery("#resultQLGlobal").hide();
} else {
jQuery("#resultQLGlobal").show();
jQuery("#resultQLNZ").hide();
jQuery("#resultQLUS").hide();
jQuery("#resultQLAU").hide();
}
if(image_au === "" && image_nz === "" && image_us === "" && image_gl !== ""){
jQuery("#resultQLGlobal").show();
}
}, "jsonp");
</script>
<?php
echo '<div class="col-lg-4 col-sm-6" style="padding:2px">';
echo '<a href="' . get_field('page_url') . '" class="portfolio-box">';
?>
<div id="resultQLAU">
<img class="img-responsive" src="<?php echo $image_au['url']; ?>" alt="<?php echo $image_au['alt']; ?>" />
</div>
<div id="resultQLNZ">
<img class="img-responsive" src="<?php echo $image_nz['url']; ?>" alt="<?php echo $image_nz['alt']; ?>" />
</div>
<div id="resultQLUS">
<img class="img-responsive" src="<?php echo $image_us['url']; ?>" alt="<?php echo $image_us['alt']; ?>" />
</div>
<div id="resultQLGlobal">
<img class="img-responsive" src="<?php echo $image_gl['url']; ?>" alt="<?php echo $image_gl['alt']; ?>" />
</div>
<?php
echo '<div class="portfolio-box-caption">';
echo '<div class="portfolio-box-caption-content">';
echo '<div class="project-category text-faded">' . get_the_title() . '</div>';
echo '<div class="project-name">' . get_the_content() . '</div>';
echo '</div>';
echo '</div>';
echo '</a>';
echo '<h6 class="news-title text-center">' . get_the_title() . '<span style=""> <i class="fa fa-angle-double-right"></i></span></h6>';
echo '</div>';
endwhile;
?>
I originally had the code e.g <div id="resultQLAU" style="display:none"> and just had jQuery("#resultQLAU").show(); in script which outputed only the first
GEO image of the first post (so GEO was working correct for that 1 post)
Not sure what problem is?
Your help would be much appreciated. Thanks
You are using ID inside your loop so all the block will have the same ids which isn't good as id need to be unique. You may change this by adding a suffix/prefix depending the iteration and use classes instead.
1) add a new var the increment inside your loop like this :
$i = 0
while ( $loop->have_posts() ) : $loop->the_post();
$i++;
2) for each id append the content of $i, for example :
jQuery(".resultQLAU_<?php echo $i; ?>").show();
do this everywhere you have the id.

Show elements with row "lugar" value 1

I have a table with a row called lugar that have the value 1 or 2.
When I do foreach, i want to filter the elements that have the value 1 in the row lugar and the elements that have the value 2.
$descuento->lugar is the variable for the row lugar.
Actually, my code is:
<ul id="slider">
<?php foreach ( $results['descuentos'] as $descuento ) {
$titulo = htmlspecialchars( $descuento->title );
$title_url = limpiarCaracteresEspeciales($titulo);
?>
<li>
<?php if ( $imagePath = $descuento->getImagePath( IMG_TYPE_FULLSIZE ) ) { ?>
<img src="<?php echo $imagePath?>" alt="<?php echo htmlspecialchars( $descuento->title )?>" />
<?php } else {?> <img src="http://lorempixel.com/400/300/sports/" alt="<?php echo htmlspecialchars( $descuento->title )?>" /> <?php } ?>
<div>
<strong><span><?php echo htmlspecialchars( $descuento->title )?></span></strong>
<p><?php echo $descuento->content ?></p>
</div>
</li>
<?php
}
?>
</ul>
Then you could try this:
foreach ( $results['descuentos'] as $descuento )
{
if( $descuento->lugar == 1)
{
$titulo = htmlspecialchars( $descuento->title );
$title_url = limpiarCaracteresEspeciales($titulo);
echo '<li>';
if ( $imagePath = $descuento->getImagePath( IMG_TYPE_FULLSIZE ) )
{
echo '<img src="'. $imagePath. '" alt="'. $titulo. '" />';
}
else
{
echo '<img src="http://lorempixel.com/400/300/sports/" alt="'. $titulo. '" />';
}
echo '<div>'.
'<strong><span>'. $titulo. '</span></strong>'.
'<p>'. $descuento->content. '</p>'.
'</div>'.
'</li>';
}
}

Wordpress: Wrapping two posts into one div

I'm using Foundation for a wordpress theme and I need to wrap two posts into one div with class of 'row'. The thing is I need to put div class="row" before the first post closing the the second post with div and it should repeat with every new posts.
Here is my code:
<?php query_posts( 'cat=2&showposts=9&orderby=date&order=DESC' ); ?>
<div <?php post_class('small-12 medium-6 large-6 columns') ?> id="post-<?php the_ID(); ?>">
<?php echo '<a href="', get_permalink(), '">';
if ( has_post_thumbnail() ) {the_post_thumbnail();}
else { echo '<img src="', get_template_directory_uri( 'template_directory' ), '/images/thumb-default.png','" alt="" />'; }
echo '</a>';
?>
<h3><?php the_title(); ?></h3>
<p><?php echo get_excerpt(); ?></p>
</div>
something like this i think
<?php
$count = 1;
$outputstring = "";
ur while loop
if ( $count % 2 != 0 )
{
$outputstring .= " <row div>";
}
$outputstring .= "<div" . post_class('small-12 medium-6 large-6 columns'). ' id="post-'. the_ID() .'>';
YOUR OUTPUT CODE HERE
if ( $count % 2 == 0 )
{
$outputstring .= " </end row div>";
}
$count++;
END your while loop
echo $outputstring; /// here is where u output the WHOLE thing outside of your loop
?>

Categories