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
Related
I´m trying to display a custom taxonomy image in the frontend on a single post.
Following situation:
Created a CPT for "Weine"
Added a taxonomy "winzer"
Added the following code to my functions.php to display the "winemaker" taxonomy with its description in the single post using a shortcode:
function wpb_catlist_desc() {
$string = '<div>';
$catlist = get_terms( 'winzer' );
if ( ! empty( $catlist ) ) {
foreach ( $catlist as $key => $item ) {
$string .= '<div>'. $item->name . '<br />';
$string .= '<em>'. $item->description . '</em></div>';
}
}
$string .= '</div>';
return $string;
}
add_shortcode('wpb_categories', 'wpb_catlist_desc');
Added an Image fieled to the "winzer" taxonomy using ACF. --> Here´s where I´m stuck now.
I need to add another line to the php snippet to also display the image using the shortcode.
Any hints how to get this done? :)
Cheers!!!!
function wpb_catlist_desc() {
$string = '<div>';
$catlist = get_terms( 'winzer' );
if ( ! empty( $catlist ) ) {
foreach ( $catlist as $key => $item ) {
$image = get_field('Your_image_field_ID');
if(!empty($image)) {
$imgurl = $image['url'];
}
$string .= '<div>';
$string .= '<img src="' . $imgurl . '" alt="' . $item->name . '"/><br />';
$string .= $item->name . '<br />';
$string .= '<em>'. $item->description . '</em></div>';
}
}
$string .= '</div>';
return $string;
}
add_shortcode('wpb_categories', 'wpb_catlist_desc');
Here's how I got it solved:
function wpb_catlist_desc() {
global $post;
$string = '<div>';
$catlist = wp_get_post_terms($post->ID, 'winzer');
if ( ! empty( $catlist ) ) {
foreach ($catlist as $item ) {
$image = get_field('winzer_bild', 'winzer_'.$item->term_id);
if(!empty($image)) {
$imgurl = $image['url'];
}
$string .= '<div>';
$string .= '<img class="winzer-bild-klein" src="' . $imgurl . '" alt="' . $item->name . '"style="width:100%;"/>';
$string .= '<h3 class="winzer-name-n">'. $item->name . '</h3>';
$string .= '<p>'. $item->description . '</p></div>';
}
}
$string .= '</div>';
return $string;
}
add_shortcode('wpb_categories', 'wpb_catlist_desc');
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
I am trying to output Wordpress' navigation menu items along with their CSS Classes.
I was able to get the menu items, but the classes are more complicated. the css classes come in an array but it's already inside a foreach() loop.
$menu_items = wp_get_nav_menu_items($menu->term_id);
$menu_list = '<ul id="menu-' . $menu_name . '">';
foreach ( (array) $menu_items as $key => $menu_item ) {
$title = $menu_item->title;
$url = $menu_item->url;
$classes = $menu_item->classes;
$menu_list .= '<li>';
$menu_list .= '<i class=""></i>';
$menu_list .= '' . $title . '';
$menu_list .= '</li>';
}
$menu_list .= '</ul>';
As you can see, inside the foreach() loop I am getting the $title and $url which are both strings. But then when I try to get the css classes associated with the menu item through $menu_item->classes it returns an array and I am not sure how to do this.
Because each menu item has 3 css classses fa fa-user fa-3x I can return the values using this
$classes[0] . $classes[1] . $classes[2]
But suppose I don't want the third class and I leave it out empty, I get a missing offset error.
I also tried doing a foreach within the original foreach
$menu_list .= '<li>';
foreach($classes as $class) {
$menu_list .= '<i class="'. $class .'"></i>';
}
$menu_list .= '' . $title . '';
$menu_list .= '</li>';
But this returns 3 separate <i> tags for each menu item, which is not what I want. The output looks like this
<i class="fa"></i>
<i class="fa-user"></i>
<i class="fa-3x"></i>
Simply use a nested Loop in place like so:
<?php
$menu_items = wp_get_nav_menu_items($menu->term_id);
$menu_list = '<ul id="menu-' . $menu_name . '">';
foreach ( (array) $menu_items as $key => $menu_item ) {
$title = $menu_item->title;
$url = $menu_item->url;
$classes = $menu_item->classes;
$comboClass = "";
// USING NESTED LOOP CONSTRUCT
foreach($classes as $class){
$comboClass .= $class . " "; // SEPARATE CLASS-NAME WITH A SPACE
}
$menu_list .= '<li>';
$menu_list .= '<i class="' .$comboClass . '"></i>';
$menu_list .= '' . $title . '';
$menu_list .= '</li>';
}
$menu_list .= '</ul>';
You can nest loops...
foreach(...) {
... html stuff ..
echo '<div class="';
foreach($classes as $class) {
echo $class, ' ';
}
echo '">...</div>';
}
I want two differents excerpts on my homepage, one for the posts inside the loop, the other one for the latest post published, outside the loop.
I managed to add the excerpt on the latest post outside of the loop like this
<?php $postslist = get_posts('numberposts=1&order=DESC&orderby=post_date');
foreach ($postslist as $post) :
setup_postdata($post); ?>
<?php wp_latest_excerpt('wp_latest', 'excerpt_more_latest'); ?>
<?php endforeach; ?>
While the excerpt for the posts inside the loop is
<?php wp_excerpt('wp_index'); ?>
For some reasons I am able to set the excerpt length differently for both of my excerpts, but the "more" stays the same. (both "more" have the class .view-article)
I thought I could create another excerpt with a different "more" function, but it does not work, here are my 2 different excerpts and more functions.
For the length
function wp_index($length)
{
return 21;
}
function wp_latest($length)
{
return 18;
}
For the "more"
function view_article($more)
{
return '... </br><a class="view-article" href="' . get_permalink($post->ID) . '">' . __('Continue Reading', '') . '</a>';
}
function latest_view_article($more)
{
return '... </br><a class="view-latest-article" href="' . get_permalink($post->ID) . '">' . __('Continue Reading', '') . '</a>';
}
add_filter('excerpt_more_latest', 'latest_view_article');
add_filter('excerpt_more', 'view_article');
And finally the excerpts
function wp_latest_excerpt($length_callback = '', $more_callback = '')
{
if (function_exists($length_callback)) {
add_filter('excerpt_length', $length_callback);
}
if (function_exists($more_callback)) {
add_filter('excerpt_more_latest', $more_callback);
}
$output = get_the_excerpt();
$output = apply_filters('wptexturize', $output);
$output = apply_filters('convert_chars', $output);
$output = '<p>' . $output . '</p>';
echo $output;
}
function wp_excerpt($length_callback = '', $more_callback = '')
{
if (function_exists($length_callback)) {
add_filter('excerpt_length', $length_callback);
}
if (function_exists($more_callback)) {
add_filter('excerpt_more', $more_callback);
}
$output = get_the_excerpt();
$output = apply_filters('wptexturize', $output);
$output = apply_filters('convert_chars', $output);
$output = '<p>' . $output . '</p>';
echo $output;
}
Is this a wrong way to achieve this or did I make some mistakes in my code ?
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...