Custom Menu in Sidebar Wordpress - wordpress

I've a problem to show a Custom Menu in sidebar.
Now, I've created a Menu from WordPress back-end. this menu must be showed in sidebar of my template, with menu name (i.e. CUSTOM MENU) and the structure with parents and child.
My target is this.
At the moment, I've this part of code:
$meta_box_menu = array(
'id' => 'custom-meta-menu',
'title' => 'Menu Sidebar',
'page' => 'page',
'context' => 'side',
'priority' => 'high',
'fields' => array(
array(
'id' => 'custom-meta-menu-name',
'type' => 'select',
'std' => 'none'
),
),
);
/*
* This function will register the meta box with WordPress
*/
function custom_add_box() {
global $meta_box_menu;
add_meta_box($meta_box_menu['id'], $meta_box_menu['title'], 'custom_meta_menu_html',
$meta_box_menu['page'], $meta_box_menu['context'], $meta_box_menu['priority']);
}
add_action('admin_init', 'custom_add_box');
/*
* This function will produce the html needed to display our meta box in the admin area
*/
function custom_meta_menu_html() {
global $meta_box_menu, $post;
$output = '<p style="padding:10px 0 0 0;">'.__('Scegli il menu da mostrare nella Sidebar di questa pagina.', 'custom').'</p>';
$output .= '<input type="hidden" name="sf_meta_box_nonce" value="'.
wp_create_nonce(basename(__FILE__)). '" />';
$output .= '<table class="form-table">';
foreach ($meta_box_menu['fields'] as $field) {
$meta = get_post_meta($post->ID, $field['id'], true);
/*
* Get out all our menus using the function from functions.php
*/
$menus = custom_get_all_menus();
/*
* Grab out saved data for edit mode
*/
$meta = get_post_meta($post->ID, $field['id'], true);
$output .= '<select name="'.$field['id'].'" class="widefat">';
$output .= '<option value="none">- none -</option>';
if(is_array($menus)):
foreach($menus as $k => $v):
if($meta==$v->slug):
$output .= '<option selected="selected" value="' . $v->slug .'">' . $v->name . '</option>';
else:
$output .= '<option value="' . $v->slug .'">' . $v->name . '</option>';
endif;
endforeach;
endif;
$output .= '</select>';
}
$output .= '</table>';
echo $output;
}
/*
* This function will save our preferences into the database
*/
function custom_save_data($post_id) {
global $meta_box, $meta_box_menu;
foreach ($meta_box_menu['fields'] as $field) {
$old = get_post_meta($post_id, $field['id'], true);
$new = $_POST[$field['id']];
if ($new && $new != $old) {
update_post_meta($post_id, $field['id'], stripslashes(htmlspecialchars($new)));
} elseif ('' == $new && $old) {
delete_post_meta($post_id, $field['id'], $old);
}
}
}
add_action('save_post', 'custom_save_data');
function custom_get_all_menus() {
$menu_obj = get_terms( 'nav_menu' );
return $menu_obj;
}
/*
* Html Custom Sidebar Menu
*/
function custom_generate_menu_from_items($items) {
if(count($items)>0):
$menu_list = '<aside class="widget sidebar_menu"><h5 class="widget-title">' .'CUSTOM MENU'. '</h5><nav><ul class="sidebar-menu-items">';
foreach ( (array) $items as $key => $menu_item ) {
$title = $menu_item->title;
$url = $menu_item->url;
$menu_list .= '<li>' . $title . '</li>';
}
$menu_list .= '</ul></nav></aside>';
return $menu_list;
$menu_item->title = get_post_meta( $menu_item->ID, '_menu_item_title', true );
return $menu_item;
endif;
add_filter( 'wp_get_nav_menu_items', 'custom_generate_menu_from_items', null, 3 );
}
With this code, the menu is showed in output page but the pages are placed in the same level, without parent child relationship.
How can I keep this relationship?
Thank you your support.

There is no logic in there that checks the relationships, I'd say
var_dump($items);
somewhere and see if there is some field pertaining to the relationships of the menu items. Probably something looking like "parentId" or along those lines.
If it's not there, then you'll have to query them yourself.
Once you have them, you can check if it has a parentId, and handle the formatting that way. This will get a little tricky if it nest down more than one level, because you'd have to start checking if the parent has a parent and handle the formatting for all those different cases

Related

ob_start() combined with wp_query

I'm making my first plugin and I have a problem with displaying my shortcode.
It shows in the top all the time but I have read some about ob_start(); and trying to use it but the shortcode just returns nothing.
Im using the code below - it seems to have something to do with my posts. Anyone know why how to solve this problem?
My WP_Query:
$query = new WP_Query( array(
'category__in' => $categories,
'posts_per_page' => $whpost_stored_meta_shortcode['whpost_number_of_posts'][0]
));
The code i use to display it:
ob_start();
echo '<div class="whpost_content">';
while($query->have_posts()) : $query->the_post();
// Get the URL to the attached image.
$attached_image = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'large' );
echo '<div class="whpost_post" style="width: ' . $post_width .'; background-image: url(' . $attached_image[0] . ');">';
the_title();
echo '</div>';
endwhile;
echo '</div>';
return ob_get_clean();
The complete code for the shortcode function:
<?php
function cpt_content_func( $atts ) {
// Get the ID we putted in into [wh-postgrid id="THIS_ID"]
extract( shortcode_atts( array(
'id' => null
), $atts ) );
// Get stored meta data (For the categories - needed to be formatted in
// a certain way)
$categories = get_post_meta( $id, 'whpost_cats', true );
// Get meta data for settings and such.
$whpost_stored_meta_shortcode = get_post_meta( $id );
// Get the correct categories and use the settings we got.
$query = new WP_Query( array(
'category__in' => $categories,
'posts_per_page' => $whpost_stored_meta_shortcode['whpost_number_of_posts'][0]
));
// Set the styles
switch ( $whpost_stored_meta_shortcode['whpost_posts_per_line'][0] ) {
case 1:
$post_width = '100%';
$post_max_height = '';
break;
case 2:
$post_width = '50%';
$post_max_height = '';
break;
case 3:
$post_width = '33.333333%';
$post_max_height = '';
break;
case 4:
$post_width = '25%';
$post_max_height = '';
break;
default:
$post_width = '50%';
}
// Display the front-end
ob_start();
echo '<div class="whpost_content">';
while($query->have_posts()) : $query->the_post();
// Get the URL to the attached image.
$attached_image = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'large' );
echo '<div class="whpost_post" style="width: ' . $post_width .'; background-image: url(' . $attached_image[0] . ');">';
the_title();
echo '</div>';
endwhile;
echo '</div>';
return ob_get_clean();
}
add_shortcode('wh-postgrid','cpt_content_func');
I think problem with your Query, you can add this line to check
if (!$query->have_posts()) {
return 'Empty result';
} else {
return ob_get_clean();
}
The function must return the output.
Change your code like this and it should work:
$output = '<div class="whpost_content">';
while($query->have_posts()) : $query->the_post();
// Get the URL to the attached image.
$postId = get_the_ID();
$attached_image = wp_get_attachment_image_src( get_post_thumbnail_id( $postId ), 'large' );
$output .= '<div class="whpost_post" style="width: ' . $post_width .'; background-image: url(' . $attached_image[0] . ');">';
$output .= get_the_title( $postId );
$output .= '</div>';
endwhile;
$output .= '</div>';
return $output;

How to display thumbnails in Wordpress for related posts by author?

I have a related posts by author function using this code:
function get_related_author_posts() {
global $authordata, $post;
$authors_posts = get_posts( array( 'author' => $authordata->ID, 'post__not_in' => array( $post->ID ), 'posts_per_page' => 10 ) );
$output = '<div class="block">';
foreach ( $authors_posts as $authors_post ) {
$output .= '' . apply_filters( 'the_title', $authors_post->post_title, $authors_post->ID ) . '';
}
$output .= '</div>';
return $output; }
To output I use the following code inside the loop of my single.php file:
<?php echo get_related_author_posts(); ?>
Currently the function it displays only post titles as links.
How should look this wp code in order to display thumbnails for this related posts by author function?
Try:
foreach ( $authors_posts as $authors_post ) {
$output .= get_the_post_thumbnail($authors_post->ID);
$output .= '' . apply_filters( 'the_title', $authors_post->post_title, $authors_post->ID ) . '';
}
$output .= '</div>';
check get_the_post_thumbnail for more information like image size and attributes like extra classes.
Let me know if you need more help.

WordPress Shortcode - Change Layout Based On Attribute

I have the following Custom Shortcode that displays a Custom Post Type of staff members in a defined taxonomy of "type". I'm trying to add a second attribute that will allow the user to define the layout style of the output, for example columns or rows. Here is what I envision the shortcode looking like:
[staff type="international" style="rows"]
I think I have all the coding for getting the attributes, including setting the default values, but I can't seem to figure out how to get an if statement in there to alter the output. It seems like it should go right before the first "$output .=" line.
function get_staff($atts) {
extract( shortcode_atts( array(
'type' => 'international',
'style' => 'rows'
), $atts ) );
add_filter( 'posts_orderby' , 'posts_orderby_lastname' );
$loop = new WP_Query(
array (
'post_type' => 'staff',
'orderby' => 'title',
'staff-type' => $type,
'style' => $style
)
);
remove_filter( 'posts_orderby' , 'posts_orderby_lastname' );
if ($loop->have_posts()) {
$output = '<div class="staff">';
while($loop->have_posts()){
$loop->the_post();
$meta = get_post_meta(get_the_id());
// Attributes Array for Featured Image Below
$attr = array(
'title' => get_the_title(),
'alt' => get_the_title(),
'class' => 'img_frame'
);
$output .= '
<div class="row-fluid" style="border-bottom: 1px solid #EEE; margin-bottom: 16px; padding-bottom: 16px;">
<div class="span3">' . get_the_post_thumbnail($post->ID, 'small', $attr) . '</div>
<div class="span9"><h3>' . get_the_title() . '</h3>
' . get_the_content() . '
</div></div>
';
}
$output .= "</div>";
} else {
$output = 'No Staff Meet This Criteria Yet.';
}
return $output;
};
// Create Last Name Sort For Staff Custom Post Type
function posts_orderby_lastname ($orderby_statement)
{
$orderby_statement = "RIGHT(post_title, LOCATE(' ', REVERSE(post_title)) - 1) ASC";
return $orderby_statement;
}
add_shortcode('staff', 'get_staff');
There are a few ways, depending on your coding style. You can check for the style param before starting to loop over posts. Another way is to do it after starting to loop. Sure, there's some code duplication, but it's easier to read and you'll have all the control you need over the exact markup you want to generate.
$output = '<div class="staff">';
if ($loop->have_posts()) {
if($style == 'rows'){ // rows
$output .= '<div class="layout-rows">';
while( $loop->have_posts() ){
// ...
}
$output .= "</div>";
} elseif($style == 'columns') { // columns
$output .= '<div class="layout-columns">';
while( $loop->have_posts() ){
// ...
}
$output .= "</div>";
} else { // everything else
$output .= '<div class="layout-default">';
while( $loop->have_posts() ){
// ...
}
$output .= "</div>";
}
} else {
$output = 'No Staff Meet This Criteria Yet.';
}
$output .= "</div>";

How to modify a theme-bundled widget in child theme?

I want to remove the nofollow code from the latest posts displayed in the sidebar. I found that the code which adds rel=nofollow tag to latest post is located here
theme folder/example theme/lib/activity/plugin.php.
Here is the exact code:
private function get_latest_posts( $post_count ) {
// Get the latest posts
$latest_posts = get_posts(
array(
'numberposts' => $post_count,
'order' => 'desc',
'orderby' => 'date'
)
);
// Create the markup for the listing
$html = '<div class="tab-pane" id="recent">';
$html .= '<ul class="latest-posts">';
if( count( $latest_posts ) > 0 ) {
foreach( $latest_posts as $post ) {
$html .= '<li class="clearfix">';
// Add the small featured image
if( has_post_thumbnail( $post->ID ) ) {
$html .= '<a class="latest-post-tn fademe" href="' . get_permalink( $post->ID ) . '" rel="nofollow">';
$html .= get_the_post_thumbnail( $post->ID, array( 50, 50 ) );
$html .= '</a>';
} // end if
$html .='<div class="latest-meta">';
// Add the title
$html .= '<a href="' . get_permalink( $post->ID ) . '" rel="nofollow">';
$html .= get_the_title( $post->ID );
$html .= '</a>';
// Add date posted
// If there's no title, then we need to turn the date into the link
if( strlen( get_the_title( $post->ID ) ) == 0 ) {
$html .= '<a href="' . get_permalink( $post->ID ) . '" rel="nofollow">';
} // end if
$html .= '<span class="latest-date">';
$html .= get_the_time( get_option( 'date_format' ), $post->ID );
$html .= '</span>';
// Close the anchor
if(strlen( get_the_title( $post->ID ) ) == 0 ) {
$html .= '</a>';
} // end if
$html .='</div>';
$html .= '</li>';
} // end foreach
} else {
$html .= '<li>';
$html .= '<p class="no-posts">' . __( "You have no recent posts.", 'standard' ) . '</p>';
$html .= '</li>';
} // end if/else
$html .= '</ul>';
$html .= '</div>';
return $html;
} // end get_latest_posts
Now please tell me how to remove the nofollow tag from this code using the child theme?
Since you have control of the child theme, you can wrap the call to display the widget zone for that widget with something that grabs the output, performs a regex search/replace on it, and outputs the result. I wrote a blog post about that recently:
Filtering the output of WordPress widgets
The basics are that you have a function that replaces dynamic_sidebar() with your own function, like this:
function theme_dynamic_sidebar($index = 1) {
// capture output from the widgets
ob_start();
$result = dynamic_sidebar($index);
$out = ob_get_clean();
// do regex search/replace on $out
echo $out;
return $result;
}
Seems that you are out of luck.
That's a private function and no filter hooks are offered by the theme author.
You may try to override the include('path/to/plugin.php'); and include your own modified version.

wp_list_categories - with custom taxonomy

I'm back struggling with some more coding :(
I have written a custom PHP page to loop through a custom post type. Now what I want is to list the custom posts categories, and have each category link to the same page but pass the category to the query_posts() function.
My code is currently
<div id="left" style="float:left;width:200px">
<b>Categories</b><br><br>
<?php wp_list_categories( $args ); ?>
</div> <!-- end of left container -->
<div id="middle" style="float:left;width:700px">
<?php
$args = array(
'post_type'=> 'e-books',
'showposts' => '-1',
'eCats' => $cat,
'order' => 'ASC'
);
query_posts( $args );
where
$cat = $_GET['cat']
What I want is each link from the wp_list_categories() to be something like.
http://example.com/products.php?cat=fiction
Is this possible? I can't get my head around the Walker class :( I've got this far
class MyWalker extends Walker_Category {
function start_el(&$output, $category, $depth, $args) {
extract($args);
$cat_name = esc_attr( $category->name );
$cat_name = apply_filters( 'list_cats', $cat_name, $category );
$link = '<a href="products.php?cat="';
$link .= $cat_name;
$link .= '>';
$link .= $cat_name . '</a>';
if ( 'list' == $args['style'] ) {
$output .= "\t<li";
$class = 'cat-item cat-item-' . $category->term_id;
if ( !empty($current_category) ) {
$_current_category = get_term( $current_category, $category->taxonomy );
if ( $category->term_id == $current_category )
$class .= ' current-cat';
elseif ( $category->term_id == $_current_category->parent )
$class .= ' current-cat-parent';
}
$output .= ' class="' . $class . '"';
$output .= ">$link\n";
} else {
$output .= "\t$link<br />\n";
}
}
}
but the links aren't completing - they are showing as
?cat=
For listing category with the custom taxonomy :
You can you following code:
$catnames = get_terms( 'custom taxonomy name');
Then use foreach loop to disply the category.

Resources