I've changed my approach and edited the get_related_author fucntion, but its only bringing in 1 post can someone see where I'm going wrong?
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' => 5 ) );
foreach ( $authors_posts as $authors_post ) {
$output = '<div class="listio"><ul><li> <div class="author-post">';
$output .= '<div style="background: url(<?php echo $src[0]; ?> ) !important;
width: 80px; height: 50px; float: left; margin-right: 13px;
background-size: 80px 50px!important; background-color: pink;"></div>';
$output .= '<a href="' . get_permalink( $authors_post->ID ) . '">' .
apply_filters( 'the_title', $authors_post->post_title, $authors_post->ID ) .
'</a>';
$output .= '</div></li></ul></div>';
}
return $output;
}
There you go:
function get_author_related_posts($author_id, $excluded_post){
global $wpdb;
$author_posts = $wpdb->get_results(
"
SELECT post_author, ID
FROM $wpdb->posts
WHERE post_author = '$author_id' and ID != '$excluded_post'
ORDER BY ID DESC
"
);
return $author_posts;
}
Related
I've been trying to add repeater fields in my wordpress widget form. I found two plugin examples but they don't work anymore. When I click on "Add row", nothing happens.
https://www.commoninja.com/discover/wordpress/plugin/social-repeater-widget
https://www.zealousweb.com/wordpress-plugins/repeater-entries-widget/
I noticed both use jquery in their widgets which I guess doesn't work anymore in the newer Wordpress versions?
/**
* Back-end widget form.
*
* #see WP_Widget::form()
*
* #param array $instance Previously saved values from database.
*/
public function form($instance)
{
$max_entries = get_option( 'max_entries' );
$max_entries = (empty($max_entries)) ? '5' : $max_entries;
$widget_add_id = $this->id . "-add";
$title = !empty($instance['title']) ? $instance['title'] : __('Social Repeater Widget', 'srw-widget');
$srw_html = '<p>';
$srw_html .= '<label for="'.$this->get_field_id('title').'"> '. __( 'Widget Title', 'srw-widget' ) .' :</label>';
$srw_html .= '<input id="'.$this->get_field_id('title').'" name="'.$this->get_field_name('title').'" type="text" value="'.$title.'" />';
$srw_html .= '<div class="'.$widget_add_id.'-input-containers"><div id="entries">';
for( $i =0; $i<$max_entries; $i++){
if(isset($instance['block-' . $i]) || isset($instance['social_platform-' . $i]))
{
$srw_tab_title = !empty($instance['social_platform-' . $i]) ? $instance['social_platform-' . $i] : __( 'Add Social Profile Details', 'srw-widget' );
$display = (!isset($instance['block-' . $i]) || ($instance['block-' . $i] == "")) ? 'style="display:none;"' : '';
$srw_html .= '<div id="entry'.($i+1).'" '.$display.' class="entrys"><span class="entry-title" onclick = "slider(this);"> '.$srw_tab_title.' </span>';
$srw_html .= '<div class="entry-desc cf">';
$srw_html .= '<input id="'.$this->get_field_id('block-' . $i ).'" name="'.$this->get_field_name('block-' . $i ).'" type="hidden" value="'.$instance['block-' . $i].'">';
$social_platform = esc_attr( $instance['social_platform-' . $i] );
$social_platform_link = esc_attr( $instance['social_platform_link-' . $i] );
$srw_html .= '<p class="last desc">';
$srw_html .= '<label for="'.$this->get_field_id('social_platform-' . $i).'"> '. __( 'Social Platform', 'srw-widget' ) .' :</label>';
$srw_html .= '<input class="widefat" id="'.$this->get_field_id('social_platform-' . $i).'" name="'.$this->get_field_name('social_platform-' . $i).'" type="text" value="'.$social_platform.'" placeholder="'.__( 'Enter Social Platform name', 'srw-widget' ).'" />';
$srw_html .= '</p><p>';
$srw_html .= '<label for="'.$this->get_field_id('social_platform_link-' . $i).'"> '. __('Social platform Link', 'srw-widget' ) .' :</label>';
$srw_html .= '<input class="widefat" id="'.$this->get_field_id('social_platform_link-' . $i).'" name="'.$this->get_field_name('social_platform_link-' . $i).'" type="url" value="'.$social_platform_link.'" placeholder="'.__( 'Enter Social Platform Link', 'srw-widget' ).'"/>';
$srw_html .= '</p>';
/* end wrapper with delete entry option */
$srw_html .= '<p><span class="delete-row">'. __( 'Delete Row', 'srw-widget' ) .'</span></p>';
$srw_html .= '</div></div>';
}
}
$srw_html .= '</div></div>';
$srw_html .= '<div id="message">'. __( 'Sorry, you reached to the limit of','srw-widget') .' "'.$max_entries.'" '. __( 'maximum entries', 'srw-widget' ) .'.</div>' ;
$srw_html .= '<div class="'.$widget_add_id.'" style="display:none;">' . __('Add New Platform', 'srw-widget') . '</div>';
?>
<script>
jQuery(document).ready(function(e) {
jQuery.each(jQuery(".<?php echo $widget_add_id; ?>-input-containers #entries").children(), function(){
if(jQuery(this).find('input').val() != ''){
jQuery(this).show();
}
});
jQuery(".<?php echo $widget_add_id; ?>" ).bind('click', function(e) {
var rows = 0;
jQuery.each(jQuery(".<?php echo $widget_add_id; ?>-input-containers #entries").children(), function(){
if(jQuery(this).find('input').val() == ''){
jQuery(this).find(".entry-title").addClass("active");
jQuery(this).find(".entry-desc").slideDown();
jQuery(this).find('input').first().val('0');
jQuery(this).show();
return false;
}
else{
rows++;
jQuery(this).show();
jQuery(this).find(".entry-title").removeClass("active");
jQuery(this).find(".entry-desc").slideUp();
}
});
if(rows == '<?php echo $max_entries;?>')
{
jQuery("#rew_container #message").show();
}
});
jQuery(".delete-row" ).bind('click', function(e) {
var count = 1;
var current = jQuery(this).closest('.entrys').attr('id');
jQuery.each(jQuery("#entries #"+current+" .entry-desc").children(), function(){
jQuery(this).val('');
});
jQuery.each(jQuery("#entries #"+current+" .entry-desc p").children(), function(){
jQuery(this).val('');
});
jQuery('#entries #'+current+" .entry-title").removeClass('active');
jQuery('#entries #'+current+" .entry-desc").hide();
jQuery('#entries #'+current).remove();
jQuery.each(jQuery(".<?php echo $widget_add_id; ?>-input-containers #entries").children(), function(){
if(jQuery(this).find('input').val() != ''){
jQuery(this).find('input').first().val(count);
}
count++;
});
});
});
</script>
<style>
.cf:before, .cf:after { content: ""; display: table; }
.cf:after { clear: both; }
.cf { zoom: 1; }
.clear { clear: both; }
.clearfix:after { content: "."; display: block; height: 0; clear: both; visibility: hidden; }
.clearfix { display: inline-block; }
* html .clearfix { height: 1%; }
.clearfix { display: block;}
#rew_container input,select,textarea{ float: right;width: 60%;}
#rew_container label{width:40%;}
<?php echo '.'.$widget_add_id; ?>{
background: #ccc none repeat scroll 0 0;font-weight: bold;margin: 20px 0px 9px;padding: 6px;text-align: center;display:block !important; cursor:pointer;
}
.desc{height:55px;}
#entries{ padding:10px 0 0;}
#entries .entrys{ padding:0; border:1px solid #e5e5e5; margin:10px 0 0; clear:both;}
#entries .entrys:first-child{ margin:0;}
#entries .delete-row{margin-top:20px;float:right;text-decoration: underline;color:red;}
#entries .entry-title{ display:block; font-size:14px; line-height:18px; font-weight:600; background:#f1f1f1; padding:7px 5px; position:relative;}
#entries .entry-title:after{ content: '\f140'; font: 400 20px/1 dashicons; position:absolute; right:10px; top:6px; color:#a0a5aa;}
#entries .entry-title.active:after{ content: '\f142';}
#entries .entry-desc{ display:none; padding:0 10px 10px; border-top:1px solid #e5e5e5;}
#rew_container #entries p.last label{ white-space: pre-line; float:left; width:39%;}
#message{padding:6px;display:none;color:red;font-weight:bold;}
</style>
<div id="rew_container">
<?php echo $srw_html;?>
</div>
<?php
}
I'm pretty new to php and wordpress so any help would be appreciated.
Please try the below code to create a repeater field in the widget.
dd_action( 'widgets_init', 'services_widget' );
function services_widget() {
register_widget( 'services_widget' );
}
class services_widget extends WP_Widget {
public function __construct() {
$widget_ops = array(
'classname' => 'services_widget',
'description' => 'Add a service description.'
);
$control_ops = array( 'width' => 400, 'height' => 350 );
parent::__construct( 'services_widget', 'Services', $widget_ops, $control_ops );
}
public function widget( $args, $instance ) {
$title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
echo $args['before_widget'];
if ( ! empty( $title ) ) {
echo $args['before_title'] . $title . $args['after_title'];
}
if( have_rows('service', 'widget_' . $args['widget_id']) ):
echo '<ul>';
while ( have_rows('service', 'widget_' . $args['widget_id']) ) : the_row();
echo '<li class="service one-half">';
$title = get_sub_field( 'title', 'widget_' . $args['widget_id'] );
$body = get_sub_field( 'body', 'widget_' . $args['widget_id']);
$button = get_sub_field( 'button', 'widget_' . $args['widget_id'] );
$button_link = get_sub_field( 'button_link', 'widget_' . $args['widget_id'] );
if( $title ) {
echo '<h4>' . $title . '</h4>';
}
if( $body ) {
echo '<p>' . $body . '</p>';
}
if( $body ) {
echo '<a class="more" href="' . $button_link . '">' .$button . '</a>';
}
echo '</li><div class="clearfix"></div></ul>';
endwhile;
endif;
echo $args['after_widget'];
}
public function form( $instance ) {
$instance = wp_parse_args( (array) $instance, array( 'title' => '', 'text' => '' ) );
$filter = isset( $instance['filter'] ) ? $instance['filter'] : 0;
$title = sanitize_text_field( $instance['title'] );
?>
<p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" /></p>
<p><input id="<?php echo $this->get_field_id('filter'); ?>" name="<?php echo $this->get_field_name('filter'); ?>" type="checkbox"<?php checked( $filter ); ?> /> <label for="<?php echo $this->get_field_id('filter'); ?>"><?php _e('Automatically add paragraphs'); ?></label></p>
<?php
}
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = sanitize_text_field( $new_instance['title'] );
if ( current_user_can( 'unfiltered_html' ) ) {
$instance['text'] = $new_instance['text'];
} else {
$instance['text'] = wp_kses_post( $new_instance['text'] );
}
$instance['filter'] = ! empty( $new_instance['filter'] );
return $instance;
}
}
I'm using the below code to make wordpress search only by product titles. However, the woocommerce "Filter by Attributes" widget still shows filters as if the search was done on body text too.
So for example if I search "face" the results that show up all have "face" in the title but the Filter By Attribute widget will still show Chocolate as an attribute because there is a chocolate bar that has a description "Face it, we all love chocolate"
Then if you click the "Chocolate" filter, it will show no results, because the search is only showing results with "face" in the title.
I don't even know where to begin looking to change the behaviour of the filter by attributes widget.
// Search titles only
function __search_by_title_only( $search, $wp_query )
{
global $wpdb;
if(empty($search)) {
return $search; // skip processing - no search term in query
}
$q = $wp_query->query_vars;
$n = !empty($q['exact']) ? '' : '%';
$search =
$searchand = '';
foreach ((array)$q['search_terms'] as $term) {
$term = esc_sql($wpdb->esc_like($term));
$search .= "{$searchand}($wpdb->posts.post_title LIKE '{$n}{$term}{$n}')";
$searchand = ' AND ';
}
if (!empty($search)) {
$search = " AND ({$search}) ";
if (!is_user_logged_in())
$search .= " AND ($wpdb->posts.post_password = '') ";
}
return $search;
}
add_filter('posts_search', '__search_by_title_only', 500, 2);
I've cobbled together my own filter that plays nice with the above search (only the titles) and doesn't show any Brands that have items that don't have the keyword in the title. My only issue now is figuring out how to get the count of items.
<?php
$searchterm = htmlentities($_GET["s"]);
$filter_brand = htmlentities($_GET["filter_brand"]);
$current = $_SERVER['REQUEST_URI'];
$site_url = home_url();
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'order' => 'ASC',
'orderby' => 'title',
's' => $searchterm
);
// The Query
$the_query = new WP_Query( $args );
if( $the_query->have_posts() ) :
echo'<div id="woocommerce_layered_nav-3" class="widget woocommerce widget_layered_nav woocommerce-widget-layered-nav">
<ul class="woocommerce-widget-layered-nav-list">';
$unique_singler = array();
while ( $the_query->have_posts() ) : $the_query->the_post();
$brand_terms = get_the_terms( $post, 'pa_brand' );
$singler = array_values($brand_terms)[0];
$name = $singler->name;
$name_string = strtolower($name);
$name_slug = str_replace(" ","-",$name_string);
// only create option if city hasn't been added yet
if( ! in_array( $name, $unique_singler ) ) :
// add city to array so it doesn't repeat
$unique_singler[] = $name;
;?>
<li class="woocommerce-widget-layered-nav-list__item wc-layered-nav-term <?php if (!($filter_brand == $name_slug) && !empty($filter_brand)) { echo 'hidden'; }?>">
<?php echo '<a class="remover ';
if ($filter_brand == $name_slug) { echo 'shown'; }
echo '" href="'.$site_url.'/?s='.$searchterm.'&tags=1&ixwps=1&post_type=product"><i class="fas fa-times"></i></a>';
?><?php
echo '<a class="dup" href="'.$current.'&filter_brand='.$name_slug.'">'.$name.'</a> <span class="count">(' .count($XXXXXXX). ')</span>';
?></li>
<?php
endif;?>
<?php endwhile;?>
</ul>
</div>
<?php else :?>
<p>Nothing to see</p>
<?php endif; ?>
<style>
li.woocommerce-widget-layered-nav-list__item.wc-layered-nav-term a.remover {
display:none;
}
li.woocommerce-widget-layered-nav-list__item.wc-layered-nav-term a.remover.shown {
display:inline-block;
}
li.woocommerce-widget-layered-nav-list__item.wc-layered-nav-term.hidden
{
display:none;
}
.x-sidebar .widget ul li {
border-top: 1px solid rgba(0,0,0,0.085) !important;
}
ul.woocommerce-widget-layered-nav-list
{
border-top: 1px solid rgba(0,0,0,0.085) !important;
border-bottom: 1px solid rgba(0,0,0,0.085) !important;
}
.woocommerce-widget-layered-nav-list, li.woocommerce-widget-layered-nav-list__item.wc-layered-nav-term {
margin-left:0 !important;
}
.x-sidebar .widget ul li a {
padding-top: 8px;
padding-bottom: 8px;
}
div#woocommerce_layered_nav-3 {
margin: 0;
}
a.remover.shown i {
text-decoration: underline !important;
margin-right:0.5em;
}
</style>
I created a list of categories in a page. The code is as follows:
<?php
$args = array(
'orderby' => 'name',
'parent' => 0
);
$categories = get_categories( $args );
foreach ( $categories as $category ) {
echo '<div style="margin:0 0 10px 10px; border: 1px solid #888; float: left; padding:1px; text-align:center;"><img src="https://arsipkoran.ga/img/logo/koran-thumb/thumb-' . $category->cat_name . '.png" alt="' . $category->cat_name . '" style="width:180px;height:30px;" /><br/>' . $category->name . '<br/> (' . $category->category_count . ' arsip)</div>';
}
?>
The results are as successful as the following links: https://pustakakoran.com/direktori-e-koran/
Then I try to apply to the tag, the code as follows:
<?php
$args = array(
'orderby' => 'name',
'parent' => 0
);
$tags = get_the_tags( $args );
foreach ( $tags as $tag ) {
echo '<div style="margin:0 0 10px 10px; border: 1px solid #888; float: left; padding:1px; text-align:center;"><img src="https://arsipkoran.ga/img/logo/koran-thumb/thumb-' . $tag->name . '.png" alt="' . $tag->name . '" style="width:180px;height:30px;" /><br/>' . $tag->name . '<br/> (' . $tag->tag_count . ' kitab)</div>';
}
?>
But the result can not. Can anyone help me, which part of the code should be fixed.
Use get_tags() and not get_the_tags().
which part of the code should be fixed
(1) Replace this:
$tags = get_the_tags( $args );
..with this:
$tags = get_tags( $args );
(2) Replace this:
$tag->tag_count
..with this:
$tag->count
I found lots of methods to change the "sub-menu", but the problem is my friend coded a new Walker_Nav_Menu and here is the code.
All I want is:
to add a class in 3rd line where CLASS="ADD New HERE"
to change the class in 4th line, from <ul class="sub-menu"> to <ul class="dropdown">
again add a class in the 5th line, just like the 3rd line.
<ul id="menu-herid" class="nav navbar-nav" data-breakpoint="800">
<li id="menu-item-2821" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children">
<a CLASS="ADD New HERE" href="http://theme.dev/home/">Home</a>
<ul class="sub-menu">
<li id="menu-item-2838" class="menu-item menu-item-type-post_type menu-item-object-page">
Blog with Slideshow
</li>
</ul>
</li>
</ul>
// Navigation with description
if (! class_exists('hs_description_walker')) {
class hs_description_walker extends Walker_Nav_Menu {
function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
global $wp_query;
$indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
$class_names = $value = 'class="dropdown"';
$classes = empty( $item->classes ) ? array() : (array) $item->classes;
$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) );
$class_names = ' class="'. esc_attr( $class_names ) . '"';
$output .= $indent . '<li id="menu-item-'. $item->ID . '"' . $value . $class_names .'>';
$attributes = ! empty( $item->attr_title ) ? ' title="' . esc_attr( $item->attr_title ) .'"' : '';
$attributes .= ! empty( $item->target ) ? ' target="' . esc_attr( $item->target ) .'"' : '';
$attributes .= ! empty( $item->xfn ) ? ' rel="' . esc_attr( $item->xfn ) .'"' : '';
$attributes .= ! empty( $item->url ) ? ' href="' . esc_attr( $item->url ) .'"' : '';
$description = ! empty( $item->description ) ? '<span class="desc">'.esc_attr( $item->description ).'</span>' : '';
if($depth != 0) {
$description = $append = $prepend = "";
}
$item_output = $args->before;
$item_output .= '<a'. $attributes .'>';
$item_output .= $args->link_before;
if (isset($prepend))
$item_output .= $prepend;
$item_output .= apply_filters( 'the_title', $item->title, $item->ID );
if (isset($append))
$item_output .= $append;
$item_output .= $description.$args->link_after;
$item_output .= '</a>';
$item_output .= $args->after;
$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
}
}
}
Now the thing is I wanted the sub-menu to be changed to ""
$defaults = array(
'theme_location' => '',
'menu' => 'menu-name',
'container' => 'div',
'container_class' => '',
'container_id' => '',
'menu_class' => 'menu',
'menu_id' => '',
'echo' => true,
'fallback_cb' => 'wp_page_menu',
'before' => '',
'after' => '',
'link_before' => '',
'link_after' => '',
'items_wrap' => '<ul id="%1$s" class="%2$s">%3$s</ul>',
'depth' => 0,
'walker' => new My_Walker_Nav_Menu() //call walker
);
//Add this in your theme functions.php file
class My_Walker_Nav_Menu extends Walker_Nav_Menu {
function start_lvl(&$output, $depth) {
$indent = str_repeat("\t", $depth);
$output .= "\n$indent<ul class=\"dropdown\">\n";
}
}
you try to this code...
HTML DEMO
Reference link
Wordress Code
<nav class="navbar-collapse collapse photoshoot-menu">
<?php wp_nav_menu(array('theme_location' => 'primary','container' => ' ')); ?>
</nav>
I am starting at WP theming, and it's been more than one day that I am looking to do the similar menu with the wp_nav_menu function :
<div class="ui simple dropdown item">
CATEGORY NAME WITH SUB-CATEGORIES
<i class="dropdown icon"></i>
<div class="menu">
<div class="item" >Sub Category 1</div>
<div class="item" >Sub Category 2</div>
</div>
</div>
Also, if there's no sub categories, the HTML output would be like this :
<div class="ui simple dropdown item">
CATEGORY NAME WITHOUT SUB-CATEGORY
</div>
What do I have to put in my functions.php ?
Any hint or any help would be very much appreciated !
Thanks.
Add next code to functions.php:
class SH_Nav_Menu_Walker extends Walker {
var $tree_type = array( 'post_type', 'taxonomy', 'custom' );
var $db_fields = array( 'parent' => 'menu_item_parent', 'id' => 'db_id' );
function start_lvl(&$output, $depth) {
$indent = str_repeat("\t", $depth);
$output .= "\n$indent";
$output .= "<i class=\"dropdown icon\"></i>\n";
$output .= "<div class=\"menu\">\n";
}
function end_lvl(&$output, $depth) {
$indent = str_repeat("\t", $depth);
$output .= "$indent</div>\n";
}
function start_el(&$output, $item, $depth, $args) {
$value = '';
$classes = empty( $item->classes ) ? array() : (array) $item->classes;
$classes = in_array( 'current-menu-item', $classes ) ? array( 'current-menu-item' ) : array();
$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );
$class_names = strlen( trim( $class_names ) ) > 0 ? ' class="' . esc_attr( $class_names ) . '"' : '';
$id = apply_filters( 'nav_menu_item_id', '', $item, $args );
$id = strlen( $id ) ? ' id="' . esc_attr( $id ) . '"' : '';
$attributes = ! empty( $item->attr_title ) ? ' title="' . esc_attr( $item->attr_title ) .'"' : '';
$attributes .= ! empty( $item->target ) ? ' target="' . esc_attr( $item->target ) .'"' : '';
$attributes .= ! empty( $item->xfn ) ? ' rel="' . esc_attr( $item->xfn ) .'"' : '';
$attributes .= ! empty( $item->url ) ? ' href="' . esc_attr( $item->url ) .'"' : '';
$item_output = $args->before;
$item_output .= '<a'. $attributes . $id . $value . $class_names . '>';
$item_output .= '<div class="item">';
$item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
$item_output .= '</div>';
$item_output .= "</a>\n";
$item_output .= $args->after;
$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
}
}
and call wp_nav_menu in the "right" place with next parameters:
wp_nav_menu(array('items_wrap' => '<div id="%1$s" class="%2$s ui simple dropdown item">%3$s</div>', 'theme_location' => 'sidebar_right_menu', 'walker' => new SH_Nav_Menu_Walker))