I'm using ACF on my wordpress site to display HTML text in the search bar next to post titles based on that posts array values.
Right now the script only displays up to 1 of the html values, but I want it to include all of the values if they exist.
add_filter( 'asp_results', 'asp_custom_field_to_results', 10, 1 );
function asp_custom_field_to_results( $results ) {
$custom_field = 'trade_status';
foreach ($results as $k=>&$r) {
if ($r->content_type != 'pagepost') continue;
if ( function_exists('get_field') )
$trade_status = get_field( $custom_field, $r->id, true ); // ACF support
else
$trade_status = get_post_meta( $r->id, $custom_field, true );
// Modify the post title to add the meta value
if ( !empty($trade_status) ) {
if ( in_array('30', $trade_status) ) {
$html = '<span class="new">New</span>';
} else if ( in_array('20', $trade_status) ) {
$html = '<span class="active">Active</span>';
} else if ( in_array('10', $trade_status) ) {
$html = '<span class="closed">Closed</span>';
} else {
$html = '';
}
$r->title = $html . $r->title;
}
}
return $results;
}
So, it looks like right now you're overwriting the value of $html for each matching iteration in the loop. My guess is that you'd want to concatenate (.=) rather than overwrite (=) when you're doing this.
Related
I am trying to create a list of all links in primary navigation on my WordPress site. I am trying to output the link as URL: in a script tag using a foreach loop.
I am stuck trying to list of of the navitems links using $navItem->url in my foreach loop. It doesn't return anything / false.
Instead of $locations[$menuLocations] I have tried simply just write primary menu name as well: $locations['primary'] but no luck so far.
function get_nav_menu_items_by_location( $location, $args = [] ) {
$html = '<script type="application/ld+json" alt="hejtest">';
// Get all locations
$menuLocations = get_nav_menu_locations();
// Get object id by location
$object = wp_get_nav_menu_object( $locations[$menuLocations] );
// Get menu items by menu name
$menu_items = wp_get_nav_menu_items($object);
foreach ( $menu_items as $navItem ) {
$html .= '"url":"' . $navItem->url . '"';
}
$html .= "</script>";
echo $html;
}
add_action('wp_head', 'get_nav_menu_items_by_location');
Well... I did a refactor of your code and it's works ;)
Try with this way:
function get_nav_items() {
$menu_slug_to_retrieve = 'main'; // This can be main, primary, what ever...
$locations = get_nav_menu_locations();
$menu = wp_get_nav_menu_object( $locations[ $menu_slug_to_retrieve ] );
$menu_items = wp_get_nav_menu_items( $menu->term_id );
$menu_items_json = array(); // Prepare the array to convert to json
// Loop it
if ( $menu_items ) {
foreach ( $menu_items as $item ) {
$menu_items_json[] = array( 'url' => $item->url );
}
$html = sprintf(
'<script type="application/ld+json" id="custom-json">%s</script>',
json_encode( $menu_items_json )
);
echo $html;
}
}
add_action( 'wp_head', 'get_nav_items' );
Regards!
I got a ACF Reaper field with a couple of rows I am trying to show. However, I only want the show row if it has a checkbox checked (Checkbox is a subfield within the repeater). I am trying to accomplish that by using if in_array as described in ACF documentation under "Conditional logic":
if( in_array( "bestyrelsevalg", get_sub_field( 'bestyrelse' ) ) )
I am outputting the result in a WordPress shortcode. For now, my code kinda works, except it shows all results within the repeater field (also those that are unchecked). What am I missing ??
My code:
function investor_bestyrelse_shortcode() {
$rows = get_field('budgetter_og_nyhedsbreve');
if( $rows ) {
echo '<ul class="slides">';
foreach( $rows as $row ) {
if( in_array( "bestyrelsevalg", get_sub_field( 'bestyrelse' ) ) ) {
$image = $row['upload_dokument'];
echo '<li>';
echo get_field( 'upload_dokument' );
echo '</li>';
}
}
echo '</ul>';
}
}
add_shortcode( 'investor_bestyrelse', 'investor_bestyrelse_shortcode' );
Managed to solve issue and with the help from #maggiathor's answer. For some reason echo was causing issue. I had to use return insted:
function investor_bestyrelse_shortcode() {
$rows = get_field('budgetter_og_nyhedsbreve');
if( $rows ) {
$content = '<ul class="dokumenter">';
foreach( $rows as $row ) {
if( !in_array( "bestyrelsevalg", $row['bestyrelse'] ) ) {
$pdf = $row['upload_dokument'];
$content = $content . '<li>' . $pdf . '</li>';
}
}
}
$content = $content . '</ul>';
return $content;
}
add_shortcode( 'investor_bestyrelse', 'investor_bestyrelse_shortcode' );
You cannot use get_sub_field() within the foreach loop, either you need to use a have_rows-while-loop or access it from the associative array:
function investor_bestyrelse_shortcode() {
$rows = get_field('budgetter_og_nyhedsbreve');
if( $rows ) {
echo '<ul class="slides">';
foreach( $rows as $row ) {
if( in_array( "bestyrelsevalg", $row['bestyrelse'] ) ) {
$image = $row['upload_dokument'];
echo '<li>';
echo $row['upload_dokument'];
echo '</li>';
}
}
echo '</ul>';
}
}
add_shortcode( 'investor_bestyrelse', 'investor_bestyrelse_shortcode' );
I am trying to return an string that I can use in a function (programatically adding terms in WordPress).
My function that generates my string is basically looping through html meta tags that match a certain criteria and is as follows:
function getYouTubeTags( $post_id ) {
$video_id = get_post_meta( get_the_ID(), 'rfvi_video_id', true );
$tag_url = "http://www.youtube.com/watch?v=" . $video_id;
$sites_html = file_get_contents($tag_url);
$html = new DOMDocument();
#$html->loadHTML($sites_html);
$meta_og_tag = null;
foreach( $html->getElementsByTagName('meta') as $meta ) {
if( $meta->getAttribute('property')==='og:video:tag' ){
$meta_og_tag = $meta->getAttribute('content');
print_r ($meta_og_tag . ",");
}
}
}
When I simply execute this (getYouTubeTags();), it returns the string:
supra vs lambo,tt lambo,twin turbo,street race,texas streets,underground racing,supra,turbo supra,1200hp,nitrous,superleggera,gallardo,
In my function to add terms to a post, the following DOES NOT work:
function rct_save_post_terms( $post_id ) {
$terms = getYouTubeTags();
wp_set_post_terms( $post_id, $terms, 'post_tag', true );
}
If I manually add the string as outputted from the first function, it DOES work:
function rct_save_post_terms( $post_id ) {
$terms = 'supra vs lambo,tt lambo,twin turbo,street race,texas streets,underground racing,supra,turbo supra,1200hp,nitrous,superleggera,gallardo,';
wp_set_post_terms( $post_id, $terms, 'post_tag', true );
}
Also, according to WordPress, $terms in wp_set_post_terms: Can be an array or a comma separated string.
I know I must be missing something simple here but cannot seem to figure it out. Thank in advance for some help!
Since you want to get those string to be reused, why not return those:
function getYouTubeTags( $post_id ) {
$out = null;
$video_id = get_post_meta( get_the_ID(), 'rfvi_video_id', true );
$tag_url = "http://www.youtube.com/watch?v=" . $video_id;
$sites_html = file_get_contents($tag_url);
$html = new DOMDocument();
#$html->loadHTML($sites_html);
$meta_og_tag = null;
foreach( $html->getElementsByTagName('meta') as $meta ) {
if( $meta->getAttribute('property')==='og:video:tag' ){
// i seriously doubt this checking i think this should be
// if($meta->getAttribute('property') == 'og:video') {
$meta_og_tag = $meta->getAttribute('content');
// print_r ($meta_og_tag . ",");
$out[] = $meta_og_tag; // gather them inside first
}
}
return implode(',', $out); // return implode comma delimited strings
}
And then utimately, then you could use them:
function rct_save_post_terms( $post_id ) {
$terms = getYouTubeTags(); // strings are in here
wp_set_post_terms( $post_id, $terms, 'post_tag', true );
}
You don't seem to be returning a value in your original function. You need to use;
return $meta_og_tag;
at the end of your function to return a value back to an assigned variable.
Also, you need to append strings to the end of your returned variable using .=;
$meta_og_tag .= $meta->getAttribute('content');
OR you can save each attribute in an array and implode for the return;
// inside loop
$meta_og_tag[] = $meta->getAttribute('content');
// outside loop
return implode(', ',$meta_og_tag);
print_r will simply echo the contents of the variable, not return a value.
Hope this helps.
I'm trying to display some divs, the amount of divs that are displayed depends on how many have values.
So, i've managed to echo the amount of divs that have values, now I need to know how to display that amount depending on the value.
i.e if there are 3 values, it should show 3 divs
Any ideas?
Here's some code.
<?php
$count=count($hero_options);
$totalimg=$count-1;
for($i=1;$i<=$totalimg;$i++){
if( isset( $hero_options['upload_image_link_'.$i.''] ) && $hero_options[ 'upload_image_link_'.$i.'' ] ) {
$html = '<li><span>Image '.$i.'</span></li>';
$html .= '<li><span>Image '.$i.'</span></li>';
}
}
echo $html;
?>
Final working code below.
<?php
$count=count($hero_options);
$totalimg=$count-1;
$html = '<li><span>Image '.$i.'</span></li>';
for($i=1;$i<=$totalimg;$i++){
if( isset( $hero_options['upload_image_link_'.$i.''] ) && $hero_options[ 'upload_image_link_'.$i.'' ] ) {
echo $html;
}
}
?>
You're resetting the value of $html everytime you go through the for loop. This should work for you.
<?php
$count=count($hero_options);
$totalimg=$count-1;
$html = "";
for($i=1;$i<=$totalimg;$i++){
if( isset( $hero_options['upload_image_link_'.$i.''] ) && $hero_options[ 'upload_image_link_'.$i.'' ] ) {
$html .= '<li><span>Image '.$i.'</span></li>';
}
}
echo $html;
?>
This seems like a simple enough problem but I'm a novice at PHP and I've been working on this for hours. I'm looping through posts in an archive and showing a different logo for each post based on a certain attribute. Here's my existing function in functions.php:
function show_logo() {
global $post;
$attribute_names = array( 'pa_product-type'
);
foreach ( $attribute_names as $attribute_name ) {
$taxonomy = get_taxonomy( $attribute_name );
if ( $taxonomy && ! is_wp_error( $taxonomy ) ) {
$terms = wp_get_post_terms( $post->ID, $attribute_name );
$terms_array = array();
if ( ! empty( $terms ) ) {
foreach ( $terms as $term ) {
if ( $term->name == 'L1' ) {
// Show L1 Logo
}
elseif ( $term->name == 'M1' ) {
// Show M1 Logo
}
elseif ( $term->name == 'H1' ) {
// Show H1 Logo
}
else {
$full_line = '<span>'. $term->name . '</span>';
}
array_push( $terms_array, $full_line );
}
echo implode( $terms_array );
}
}
}
}
All I want to do is do show a different logo if the post matches multiple terms (e.g. 'L1' AND 'M1'). I have tried many very different things but I have no idea if I'm even on the right track. Any help would be greatly appreciated.
This should be fairly easy, I'm just not exactly sure the full context of all the data involved.
Assuming you are only showing one logo per post, here is one approach:
Right before foreach ( $terms as $term ) { create three boolean variables:
$hasL1 = false;
$hasM1 = false;
$hasH1 = false;
Then when one of your term names matches, instead of just showing the logo set the appropriate variable equal to true, ie $hasL1 = true;
After the foreach is complete, either before or after echo implode( $terms_array ); depending on what makes sense, setup a new if/elseif/else block to decide what logo to show as follows:
if ($hasL1 && $hasM1 && $hasH1) { // Pick logo for all 3 }
elseif ($hasL1 && $hasM1) { // Pick logo for pair }
elseif ($hasL1 && $hasH1) { // Pick logo for pair }
elseif ($hasH1 && $hasM1) { // Pick logo for pair }
elseif ($hasL1) { // Pick logo }
elseif ($hasM1) { // Pick logo }
elseif ($hasH1) { // Pick logo }
else { // default logo }
Of course there are many other ways to implement this too.