PHP if statement inside foreach to check WordPress pages - php

I have a foreach statement that loops through all of the pages of a WordPress installation and lists them inside a div (using the get_pages() function).
It currently looks like this:
<?php
$pages = get_pages();
foreach ( $pages as $page ) {
$linkPage = '<a class="order" href="' . get_page_link( $page->ID ) . '">';
$linkPage .= $page->post_title;
$linkPage .= '</a>';
echo $linkPage;
}
?>
What I need to do now is to add an if statement that inserts the string "current" after class="order... if the link is the one from the current page.
It may seem like a silly question, but I'm a front-end developer with little PHP experience, and every time I tried to add the if I got a syntax error, claiming there was an unexpected if statement.
I hope I made myself clear.
If any help can be provided, it will be very much appreciated.

<?php
//get id of your current page
$post_id = $post[0]->ID;
$pages = get_pages();
foreach ( $pages as $page ) {
$current = $post_id == $page->ID ? ' current' : '';
$linkPage = '<a class="order '.$current.'" href="' . get_page_link( $page->ID ) . '">';
$linkPage .= $page->post_title;
$linkPage .= '</a>';
echo $linkPage;
}
?>
Check this, I'm not sure if $post_id = $post[0]->ID works in your wp version, but the if-statement logic is correct and will work. Try to echo $post_id and $current if something wrong.

Try this Code :
<?php
//get id of your current page
$post_id = get_the_ID();
$pages = get_pages();
foreach ( $pages as $page ) {
//Condition statement to add the class current
$current = $post_id == $page->ID ? 'current' : '';
$linkPage = '<a class="order '.$current.'" href="' . get_page_link( $page->ID ) . '">';
$linkPage .= $page->post_title;
$linkPage .= '</a> <br> ';
echo $linkPage;
}
?>
Hope this will help you...

Related

How do I call post objects (products) custom fields using shortcodes on posts

I'm trying to show 'featured products' on blog posts. These featured products are to be selected with custom fields post objects on the back end for each post.
I have written up what I think the PHP should be - where am I going wrong? When I try to use the shortcode no code appears (but the shortcode text doesn't show so it's definitely added). Thanks :)
<?php
add_shortcode('featuredproducts' , 'printfeaturedprod');
function printfeaturedprod(){
$html = '';
$instruments = get_field('featuredprod');
if( $instruments ):
$html .= '<div class="featuredproducts">';
$html .= '<h2 style="font-size:18px; font-family:poppins;">Featured in this video</h2>';
foreach( $instruments as $instruments ):
$permalink = get_permalink( $instruments->ID );
$title = get_the_title( $instruments->ID );
$product = wc_get_product( $instruments->ID );
$price = $product->get_price();
$featured_img_url = get_the_post_thumbnail_url($instruments->ID, 'full');
$html .= '<div class="featuredproduct">';
$html .= '<img class="featuredproductimg" src="' . $featured_img_url . '">';
$html .= '<div class="proddetails">';
$html .= '<a class="producttitle" href="' . $permalink . '"><?php echo esc_html( $title ); ?></a>';
$html .= '<br><span class="productprice">£' . $price . '</span>';
$html .= '</div>';
$html .= '</div>';
endforeach;
$html .= '</div>';
endif;
}
You have built your HTML in the $html variable but then you don’t do anything with it. The shortcode doesn’t automatically know that you want to display the $html variable so you need to return ( or echo) it at the end before the function finishes:
add_shortcode('featuredproducts' , 'printfeaturedprod');
function printfeaturedprod(){
$html = '';
/* your code here... */
return $html;
}
Reference: See the add_shortcode WP documentation

How to correctly wrap a php variable inside html tags?

I am not really expert in php, what I want to do is wrap the $duration variable in the last line inside a span tag, so I can style it.
<?php
$duration = '';
if( function_exists( 'cbc_get_video_data' ) ){
global $post;
$video = cbc_get_video_data( $post );
if( $video ) {
$duration = $video->get_human_duration();
}
}
$title_content .= '<div class="post-title-wrapper"><h1 class="post-title">' . $duration . get_the_title() . '</h1>';
?>
when I do something like this the site breaks
<?php
$duration = '';
if( function_exists( 'cbc_get_video_data' ) ){
global $post;
$video = cbc_get_video_data( $post );
if( $video ) {
$duration = $video->get_human_duration();
}
}
$title_content .= '<div class="post-title-wrapper"><h1 class="post-title">' . '<span class="video-duration">'$duration'</span>' . get_the_title() . '</h1>';
?>
Any suggestions?
Thanks
In your 2nd example you are missing a . either side of $duration. You also aren't closing off a <div> and don't need the additional concatenation between the opening span.
To refine this code, try the following:
$title_content .= '<div class="post-title-wrapper"><h1 class="post-title"><span class="video-duration">' . $duration . '</span>' . get_the_title() . '</h1></div>';

Why wp_video_shortcode inside shortcode echoes when calling the wrapping shortcode?

I have a shortcode, which returns html created like this:
public function get_video_grid($the_query) {
$html = '';
if ( $the_query->have_posts() ) {
$count = 0;
while ( $the_query->have_posts()) : $the_query->the_post();
$attachment = get_attached_media( 'video' );
$post_ID = get_the_ID();
global $wpdb;
$video_url = wp_get_attachment_url($post_ID);
$video_attr = array(
'src' => $video_url
);
$item = $wpdb->get_row(
"
SELECT video_start_time
FROM " . $wpdb->prefix . "lc
WHERE attachment_id = $post_ID
",
ARRAY_A
);
$time = $item['video_start_time'];
$html .= '<div class="video-thumb-wrapper">';
$html .= '<h4 class="grid-title">' . get_the_title() . '</h4>';
$html .= '<span class="grid-timestamp">' . gmdate('d.m.Y H:i', strtotime($time)) . '</span>';
$html .= '<div class="aspect-ratio">';
$html .= wp_video_shortcode( $video_attr );
$html .= '</div>';
$html .= '</div>';
$count++;
endwhile;
}
return $html;
} // get_video_grid
However, if I am using the shortcode either in article or in do_shortcode(), the video elements are rendered outside of the containers on some pages. On some WP installations this works just fine. The version of WP does not seem to affect.
If I try to store the html string to variable like this:
<?php
/**
* Template Name: Modular Page
*/
get_header(); ?>
<?php if ( have_posts() ) while ( have_posts() ) : the_post();
if ( post_password_required() ) {
echo get_the_password_form();
} else {
lobo_modular_content( $post );
}
endwhile;
$content = do_shortcode("[lilicast_list]");
get_footer(); ?>
the videos are still rendered. Notable here is that nothing should be echoed at this point, yet still I have the elements in my html. As far as I know, all I am asking is the wp to return me a string.
Why is the wp_video_shortcode() echoed before anything else? Why the behaviour is not consistent with just adding the shortcode inside of the post?

wp_list_pages Title attribute and Order

I found a post on this site to add a custom "mytheme_list_pages" to the functions.php file in order to add the title attribute to a link. While this works to add the title attribute to the "href" output, it no longer preserves the order of the menu as wp_list_pages does. Can someone tell me how to order the output of the custom code below?
I'm calling the function from my page.php file like this:
<?php mytheme_list_pages('exclude=819&title_li='); ?>
The custom function below:
<?php
function mytheme_list_pages($param) {
$pages = get_pages($param);
foreach ( $pages as $page ) {
$li = '<li><a href="' . get_page_link( $page->ID ) . '" title="';
$li .= esc_attr($page->post_title);
$li .= '">';
$li .= $page->post_title;
$li .= '</a></li>';
echo $li;
}
}
?>
Many thanks!
Here is updated code,
<?php mytheme_list_pages('exclude=819&title_li=&sort_column=menu_order'); ?>
<?php
function mytheme_list_pages($param) {
$pages = get_pages($param);
foreach ( $pages as $page ) {
$li = '<li><a href="' . get_page_link( $page->ID ) . '" title="';
$li .= esc_attr($page->post_title);
$li .= '">';
$li .= $page->post_title;
$li .= '</a></li>';
echo $li;
}
}
?>
for more information http://codex.wordpress.org/Function_Reference/get_pages

Wordpress post->ID Problem

This is a wordpress question. I am trying to use a bit of code that works just fine on my home page on my inner page templates:
query_posts('cat=4');
// The Loop
echo '<div id="cal_details"><ul>';
while ( have_posts() ) : the_post();
$cal_date_j = date('j', intval(get_post_meta($post->ID, 'date_value', true)));
$cal_date_n = date('n', intval(get_post_meta($post->ID, 'date_value', true)));
$my_array[] = date('j, n', intval(get_post_meta($post->ID, 'date_value', true)));
$issetdate = get_post_meta($post->ID, 'date_value', true);
if (isset($issetdate)) {
echo '<li class="cal_event_li list_item_' . $cal_date_j . '_' . $cal_date_n . '">';
echo '<a href="' . get_permalink() . '">';
the_title();
echo '</a></li>';
}
endwhile;
echo '</ul></div>';
However, this doesn't seem to work on the inner-pages. All the titles links are being outputted correctly but it won't print the get_post_meta part correctly.
The list items all display something like <li class="cal_event_li list_item_1_1">
I think there is perhaps some issue with the way I have tried to use $post->ID but Im not sure whats going on here. Any ideas?
When you use query_posts you have to call global $post to get the post_meta. If you're only calling one category why don't you just use an archive template?
Also if you're going to use query_posts make sure you reset the query afterwords so plugins, sidebars, etc can still interact with the loop for conditionals etc..
global %post;
query_posts('cat=4');
// The Loop
//more stuff
endwhile;
wp_reset_query();
try replacing $post->ID with the_ID() in the inner pages. something like this
query_posts('cat=4');
// The Loop
echo '<div id="cal_details"><ul>';
while ( have_posts() ) : the_post();
$cal_date_j = date('j', intval(get_post_meta(the_ID(), 'date_value', true)));
$cal_date_n = date('n', intval(get_post_meta(the_ID(), 'date_value', true)));
$my_array[] = date('j, n', intval(get_post_meta(the_ID(), 'date_value', true)));
$issetdate = get_post_meta(the_id(), 'date_value', true);
if (isset($issetdate)) {
echo '<li class="cal_event_li list_item_' . $cal_date_j . '_' . $cal_date_n . '">';
echo '<a href="' . get_permalink() . '">';
the_title();
echo '</a></li>';
}
endwhile;
echo '</ul></div>';

Categories