WordPress Shortcode - Change Layout Based On Attribute - wordpress

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>";

Related

How do I display the taxonomy term alongside the post type post title?

I would like to display the taxonomy term of the post type post besides the post type post title, separated by the "in" text string.
There are two issues:
only "array" is displayed instead of the term
I don't know how to code the "in" text string correctly, between the term and the title (they should be in the same row)
Here is the (wrong) code in question:
$output .= '<div>' . get_the_title() . '</div>'; in $output .= '<div>' . wp_get_post_terms( $post_id, $taxonomy = 'itemscategories') . '</div>';
Embedded in this shortcode:
function myprefix_custom_grid_shortcode( $atts ) {
// Parse your shortcode settings with it's defaults
$atts = shortcode_atts( array(
'posts_per_page' => '-1',
'term' => ''
), $atts, 'myprefix_custom_grid' );
$user_id = userpro_get_view_user( get_query_var('up_username') );
// Extract shortcode atributes
extract( $atts );
// Define output var
$output = '';
// Define query
$query_args = array(
'author'=> $user_id,
'post_type' => 'items', // Change this to the type of post you want to show
'posts_per_page' => $posts_per_page,
);
// Query by term if defined
if ( $term ) {
$query_args['tax_query'] = array(
array(
'taxonomy' => 'category',
'field' => 'ID',
'terms' => $term,
),
);
}
// Query posts
$custom_query = new WP_Query( $query_args );
// Add content if we found posts via our query
if ( $custom_query->have_posts() ) {
// Open div wrapper around loop
$output .= '<div>';
// Loop through posts
while ( $custom_query->have_posts() ) {
// Sets up post data so you can use functions like get_the_title(), get_permalink(), etc
$custom_query->the_post();
// This is the output for your entry so what you want to do for each post.
$output .= '<div>' . get_the_title() . '</div>'; in $output .= '<div>' . wp_get_post_terms( $post_id, $taxonomy = 'itemscategories') . '</div>';
}
// Close div wrapper around loop
$output .= '</div>';
// Restore data
wp_reset_postdata();
}
// Return your shortcode output
return $output;
}
add_shortcode( 'myprefix_custom_grid', 'myprefix_custom_grid_shortcode' );
The wp_get_post_terms function by default returns an array so if you want to display the first term name you can do something like this:
// This is the output for your entry so what you want to do for each post.
$terms = wp_get_post_terms( $post_id, 'itemscategories' );
if ( $terms && ! is_wp_error( $terms ) ) {
$output .= '<div>' . esc_html( get_the_title() ) . ' ' . esc_html__( 'Posted in:', 'text_domain' ) . ' ' . esc_html( $terms[0]->name ) . '</div>';
}
Or you can instead use the alternative function - https://developer.wordpress.org/reference/functions/get_the_term_list/
// This is the output for your entry so what you want to do for each post.
$output .= '<div>' . esc_html( get_the_title() ) . get_the_term_list( $post_id, 'itemscategories', 'Posted in: ', ', ' ) . '</div>';
This function will display a list of all the terms belonging to the post with links to the term archives. If you want to remove the archive links you can wrap get_the_terms_list inside the strip_tags function.

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.

Custom Menu in Sidebar 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

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