How to concatenate if statement into a string? - php

Here is my code on one of my templates...
<?php while ( have_posts() ) :
the_post();
the_content();
query_posts( 'posts_per_page=5' );
endwhile; ?>
<ul id="on-going">
<?php $terms = get_terms(
'series', array(
'orderby' =>'name'));
foreach( $terms as $t ){
$t_id = $t->term_id; $series = get_option("taxonomy_$t_id");
if( $series['status_term_meta'] != 1 ) continue; ?>
<li>
<a title="See all <?php echo $t->name; ?> Episodes" href="<?php echo home_url(); ?>
/series/<?php echo $t->
slug; ?>">
<?php echo( strlen($t->
name) > 25)?substr( $t->name, 0, 25 ) . '...':$t->name; ?> </a>
<?php if(is_subbed($t->slug)) {
echo'<span class="subbedcolor small rightside"> SUBBED </span> ';
}
if(is_dubbed($t->slug)){
echo'<span class="dubbedcolor small rightside"> DUBBED </span> ';
}
?>
I want to take the part
<?php if(is_subbed($t->slug)) {
echo'<span class="subbedcolor small rightside"> SUBBED </span> ';
}
if(is_dubbed($t->slug)){
echo'<span class="dubbedcolor small rightside"> DUBBED </span> ';
}
and include it into my other template so it will show if an episode is subbed, or dubbed:
Here is the string for the other code:
$page = $page . "<li><a style='display:block;' title='" . $name . "' href='" . home_url(). "/series/" .$term->slug . "'>" . $name ."</a></li>";
How would I include the subbed or dubbed part after the closing li tag? I tried to concatenate the if statement, but I can't seem to get it to work.
Thanks so much if anyone can figure it out :D

You said that concatenation didn't work. What did you try and what didn't work?
Try this:
if(is_subbed($t->slug)) {
$page = $page.'<span class="subbedcolor small rightside"> SUBBED </span> ';
}
if(is_dubbed($t->slug)){
$page = $page.'<span class="dubbedcolor small rightside"> DUBBED </span> ';
}

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>

Php code between Tab Shortcode

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.

Links in repeater

I am using ACF on WordPress.
I have made a repeater field. Every fields work fine, except links.
The code below shows the name of URL, but the name has no link!
<?php if( have_rows('dl_box') ): ?>
<ul>
<?php while( have_rows('dl_box') ): the_row();
// vars
$content = get_sub_field('dl_link_name');
$link = get_sub_field('dl_url');
?>
<li>
<span class="link">
<?php if( $link ): ?>
<a href="<?php echo $url; ?>">
<?php endif; ?>
<?php if( $link ): ?>
</a>
<?php endif; ?>
<?php echo $content; ?>
</span>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
I think it because of this line
<a href="<?php echo $url; ?>">
but I do not know how to fix it.
Modify the markup as follows. You are attempting to access variables that haven't been declared, and the logic is out of sequence:
<li>
<span class="link">
<?php
// $link is the URL (from "dl_url")
// If there is a URL, output an opening <a> tag
if( $link ) {
echo '<a href="' . $link . '">';
}
// $content is the name (from "dl_link_name")
// always output the name
echo $content;
// If there is a URL, need to output the matching closing <a> tag
if( $link ) {
echo '</a>';
}
</span>
</li>
Note:
I have learned to dislike markup / logic like that - it doesn't make a lot of sense. I'd rather do something like this - it's simpler, easier to read, and more compact:
<li>
<span class="link">
<?php
// if there is a url, output the ENTIRE link
if ( $link ) {
echo '' . $content . '';
// otherwise just output the name
} else {
echo $content;
} ?>
</span>
</li>

Trying to apply a specific css class : php variable / foreach and echo

I am trying to modify a portfolio template in a Wordpress theme. I'd like to apply a specific css class to a specific category of the portfolio, using its ID.
I've tried many things thanks to the different answers already brought here about conditional css (How to apply a CSS class depending on a value contained in PHP variable?), php variables, or about the use of the " foreach", but I wasn't able to find a solution :
May be I didn't put my condition at the right place, or may be is it because of the use of the foreach, or may is there a misplaced tag??
The result remains the same, a full blank page.
I hope you'll have few time to have a look on this and give me a little help.
Here is the original code extract :
<?php $terms_array = array(); $id = $cat = null; ?>
<?php foreach ( $p_terms as $term ) { ?>
<?php $terms_array[$term->term_id] = array( 'name' => $term->name ); ?>
<?php } ?>
<?php if ( count( $terms_array ) > 0 ) { ?>
<div class="row">
<div class="col-md-12">
<ul id="ef-portfolio-filter" class="list-inline text-center">
<?php if ( count( $terms_array ) > 1 ) { ?>
<li class="cbp-filter-item-active"><a href="#" data-filter="*"><?php _e( 'All', EF_TDM ) ?>
<span class="cbp-filter-counter"></span>
</a>
</li>
<?php } ?>
<?php $cur = count( $terms_array ) == 1 ? ' class="cbp-filter-item-active"' : ''; ?>
<?php foreach ( $terms_array as $id => $cat ) { ?>
<?php echo '<li' . $cur . '><a data-filter=".cbp-filter-' . $id . '" href="' . esc_url( get_term_link( $id, EF_CPT_TAX_CAT ) ) . '">' . $cat['name'] . '<span class="cbp-filter-counter"></span></a></li>'; ?>
<?php } ?>
</ul><!-- #ef-portfolio-filter -->
</div><!-- .col-md-12 -->
</div><!-- .row -->
<?php } ?>
<?php } ?>
And here is one of my tries. I just paste an extract as I didn't change anything above it and just introduce my condition after the foreach :
<?php $cur = count( $terms_array ) == 1 ? ' class="cbp-filter-item-active"' : ''; ?>
<?php foreach ( $terms_array as $id => $cat ) { ?>
<?php
if ($id = myspecific id)
{echo '<li class="cbp-filter-item-custom"' . $cur . '><a data-filter=".cbp-filter-' . $id . '" href="' . esc_url( get_term_link( $id, EF_CPT_TAX_CAT ) ) . '">' . $cat['name'] . '<span class="cbp-filter-counter-custom"></span></a></li>';
}else{
echo '<li ' . $cur . '><a data-filter=".cbp-filter-' . $id . '" href="' . esc_url( get_term_link( $id, EF_CPT_TAX_CAT ) ) . '">' . $cat['name'] . '<span class="cbp-filter-counter"></span></a></li>' } ?>
</ul><!-- #ef-portfolio-filter -->

How to truncate wp_rss

does anyone know the best way to truncate an rss feed using wp_rss? I tried googling and couldn't seem to find anything. Any help would be appreciated!
Here is my code:
<?php
include_once(ABSPATH . WPINC . '/rss.php');
$feed = 'http://miletich2.blogspot.co.uk/feed/';
$rss = fetch_feed($feed);
?>
<div class="container">
<div class="right">
<div class="rss-container">
<div class="rss-heading">
<?php
$title = "Clashing With Life";
$description = 'This is a blog written by an autistic person for other autistic people about <br>some of the biggest issues in life, whether deplorable or marvelous.';
echo '<h3 class="text-center">' . $title . '</h3>';
echo '<p class="">' . $description . '</p>';
?>
</div>
<div class="text-center">
<?php
if (!is_wp_error( $rss ) ) :
$maxitems = $rss->get_item_quantity(3);
$rss_items = $rss->get_items(0, $maxitems);
if ($rss_items):
echo "<ul>\n";
foreach ( $rss_items as $item ) :
//instead of a bunch of string concatenation or echoes, I prefer the terseness of printf
//(http://php.net/manual/en/function.printf.php)
printf('<div class=""><li>%s<p>%s</p><p>%s</p></li></div>',$item->get_permalink(),$item->get_title(),$item->get_date(get_option('date_format')),$item->get_description() );
'<hr>';
endforeach;
echo "</ul>\n";
endif;
endif;
?>
</div>
</div>
</div>
</div>
You can use wp_trim_words() and wp_strip_all_tags()
Example:
if ( !is_wp_error($rss) ) :
$maxitems = $rss->get_item_quantity(3);
$rss_items = $rss->get_items(0, $maxitems);
if ($rss_items):
foreach ($rss_items as $item) :
$item_title = esc_attr( $item->get_title() );
$item_permalink = esc_url( $item->get_permalink() );
$item_post_date = $item->get_date( get_option('date_format') );
$item_excerpt = wp_trim_words( wp_strip_all_tags( $item->get_description(), true ), 55 );
echo sprintf('<div class="">%s<p>%s</p><p>%s</p></div>', $item_permalink, $item_title, $item_post_date, $item_excerpt);
endforeach;
endif;
endif;
Btw, include_once(ABSPATH . WPINC . '/rss.php'); is unnecessary. You should remove it.

Categories