Archived topic
Widgets Registration
10 replies · Started by Raul on January 24, 2021
Good morning,
I want to modify the Dispatch Demo. I have a widget created by me (timeline posts in the right sidebar) and for my last theme I put it in a child-theme and registrated it in the functions.php. But I don't know how to do it in GeneratedPress?
And as I see the picture of a post has the same width like the posttext and the sidebar together. How can I modify it to get the picture in the same width as the text, so that they are both parallel to the sidebar.
Here is my old design. This is what I want: 
Hi there,
I want to modify the Dispatch Demo. I have a widget created by me (timeline posts in the right sidebar) and for my last theme I put it in a child-theme and registrated it in the functions.php. But I don’t know how to do it in GeneratedPress?
You should be able to reuse the PHP code you made from the previous theme you've used and place it on a child theme's functions.php. It should work the same unless you've used theme specific hooks.
And as I see the picture of a post has the same width like the posttext and the sidebar together. How can I modify it to get the picture in the same width as the text, so that they are both parallel to the sidebar.
Can you link us to the site in question so we could check for the best approach to achieve this? You can use the private information text field to provide the details.
This here is my old site: https://web.archive.org/web/20201020002825/https://domain.de/category/3ddrucker/
And this here the current site as example for the design: https://domain.de/news/resident-evil-village-termin-und-neuer-trailer/
I have a second question: Do you know how to add code in the sidebar but for every category a different snippet? I use hooks but it works only in the posts. If I go to my category "xyzcat" it's not working. But If I call one of the articles "xyzcat/article" it works. Or do I have to ad Archives too?
It seems that some things are not really working. For example I used the customizer for my page and set padding for container. And it works for every page and post instead of the archives, why? If I go to archives like "categoryX" and set them, it works, put after exiting Customizer it resets to default values.
If your code is built to work with any theme, then all you should have to do is plug it into the child theme and include any necessary CSS.
If you want to include different PHP in the sidebar depending on conditions, our Hook Element is likely your best bet: https://docs.generatepress.com/article/hooks-element-overview/
Hi Tom,
it was theme specific because I get errors. DO you think I can also programm a widget with element hooks? Or is it only possible with function.php?
Hi there,
if you have a non-theme specific version of that function, then you can simply make the function call back in the Hook Element. You can of course write the function directly inside the hook as well but personally a large function is best used as a callback.
Do you have an example?
As you can see on my image at the top of the post I have at the right side a timeline widget. But it was theme specific ... :(
Do you have an example of the code that isn't working?
Hi,
this is my function.php
<?php
//Erzwingt neue URL für Einstellungen in phpmySQL: update_option( 'siteurl', 'http://domain.de' );
//update_option( 'home', 'http://domain.de' );
//Eigene Widgets referenzieren
get_template_part('widgets/widgets');
// Meine Widgets
function my_widgets_init() {
register_sidebar( array(
'name' => 'Widgetleiste rechts',
'id' => 'widgets_right',
'before_widget' => '<section class="widgets_right">',
'after_widget' => '</section>',
'before_title' => '<header><h2>',
'after_title' => '</h2></header>',
));
//Widget registrieren
register_widget('WP_PCP_Widget_Recent_Posts'); //Klassenname des Widgets benutzen
//Widget registrieren
register_widget('WP_PCP_Widget_Recent_Posts2'); //Klassenname des Widgets benutzen
}
add_action( 'widgets_init', 'my_widgets_init' );
add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles');
function theme_enqueue_styles()
{
//wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );
//wp_enqueue_style( 'child-style', get_stylesheet_directory_uri() . '/style.css', array('parent-style') );// diese Zeile funktioniert bei manchen Themes nicht
$parent_style = 'parent-style'; // This is 'powermag-style' for the powermag theme.
// Alternative für dynamische Versionsverwaltung wegen Browsercache
// Get modification time.
$modificated = date( 'YmdHi', filemtime( get_stylesheet_directory() . '/style.css' ) );
// unt wp_get_theme()get Version ersetzen durch $modificated
wp_enqueue_style( $parent_style, get_template_directory_uri() . '/style.css' );
wp_enqueue_style( 'child-style',
get_stylesheet_directory_uri() . '/style.css',
array( $parent_style ),
wp_get_theme()->get('Version'));
//$modificated );
}
?>
This here my widget timeline:
<?php
/**
* Widget API: WP_Widget_Recent_Posts class
*
* @package WordPress
* @subpackage Widgets
* @since 4.4.0
*/
/**
* Core class used to implement a Recent Posts widget.
*
* @since 2.8.0
*
* @see WP_Widget
*/
class WP_PCP_Widget_Recent_Posts2 extends WP_Widget {
/**
* Sets up a new Recent Posts widget instance.
*
* @since 2.8.0
*/
public function __construct() {
$widget_ops = array(
'classname' => 'widget_recent_entries',
'description' => __( 'Timeline' ),
'customize_selective_refresh' => true,
);
// Klassenname für jedes Widget anders!!!
parent::__construct( 'pcp-recent-posts2', __( 'Timeline' ), $widget_ops );
$this->alt_option_name = 'widget_recent_entries';
}
/**
* Outputs the content for the current Recent Posts widget instance.
*
* @since 2.8.0
*
* @param array $args Display arguments including 'before_title', 'after_title',
* 'before_widget', and 'after_widget'.
* @param array $instance Settings for the current Recent Posts widget instance.
*/
public function widget( $args, $instance ) {
if ( ! isset( $args['widget_id'] ) ) {
$args['widget_id'] = $this->id;
}
$title = ( ! empty( $instance['title'] ) ) ? $instance['title'] : __( 'Timeline' );
$categories = $instance['categories'];
$post_type = 'post'; //posts oder any statt all, da sonst alle Kategorien angezeigt werden, auch themes
$post_types = get_post_types();
unset($post_types['sidebar'], $post_types['page'], $post_types['reviews'], $post_types['gallery'], $post_types['portfolio'], $post_types['attachment'], $post_types['revision'], $post_types['nav_menu_item']);
// NEU
if($post_type == 'any') {
$post_type_array = $post_types;
} else {
$post_type_array = $post_type;
}
/** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
$title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
$number = ( ! empty( $instance['number'] ) ) ? absint( $instance['number'] ) : 5;
if ( ! $number ) {
$number = 5;
}
$show_date = isset( $instance['show_date'] ) ? $instance['show_date'] : false;
/**
* Filters the arguments for the Recent Posts widget.
*
* @since 3.4.0
* @since 4.9.0 Added the <code>$instance</code> parameter.
*
* @see WP_Query::get_posts()
*
* @param array $args An array of arguments used to retrieve the recent posts.
* @param array $instance Array of settings for the current widget.
*/
$r = new WP_Query( apply_filters( 'widget_posts_args', array(
'posts_per_page' => $number,
'no_found_rows' => true,
'post_status' => 'publish',
'ignore_sticky_posts' => true,
'cat' => $categories,
'post_type' => $post_type_array,
), $instance ) );
if ( ! $r->have_posts() ) {
return;
}
?>
<?php echo $args['before_widget']; ?>
<?php
if ( $title ) {
echo $args['before_title'] . $title . $args['after_title'];
}
?>
<aside class="widget module-timeline">
<!-- start:articles -->
<?php
$day_check = '';
while ($r->have_posts()) : $r->the_post();
$day = get_the_date('j');
?>
<div class="timeline">
<!-- start:article -->
<article class="def">
<?php if ( $show_date ) : ?>
<span class="published"><?php echo get_the_date( 'M d', $recent_post->ID ); ?></span>
<span class="published-time"><?php echo get_the_date( 'H:m', $recent_post->ID ); ?></span>
<?php endif; ?>
<!-- Um korrekte Kategorie auszugeben -->
<?php $cat = get_the_category($r->ID); ?>
<div class="cnt">
<!-- Unterschiedliche Farben für Bullets: Das else-Statement ist Original. Alles andere hinzugefügt, um nicht News als hauptkategorie anzuzeigen sondern die zweite -->
<i class="<?php if ( of_get_option('pm_parentcat') == 'end' ) {
echo end($cat)->cat_name;
}else
{echo $cat[0]->cat_name ;} ?>">
<!-- <i class="bullet parent-bullet-2 bullet-2"> -->
</i><span class="category parent-cat-2 cat-2">
<?php
// Original mit Id der ersten Kategorie
//echo '<a href="'. get_category_link( $cat[0]->term_id ) . '" title=" ' . $cat[0]->name . '">
//'. $cat[0]->cat_name .' </a>';
// Erweitert für beide Kategorien
if ( of_get_option('pm_parentcat') == 'end' ) {
//echo '<a href="'. get_category_link( end($category)->term_id ) .'">'. end($category)->cat_name .'</a>';
// Gibt alle Kategorien naus,
$separator = ' / ';
$output = '';
foreach((get_the_category()) as $cat) {
$output .= '<a href="'. get_category_link( ($cat)->term_id ) .'">'. ($cat)->cat_name .'</a>'. $separator;
}echo trim( $output, $separator );
} else {
$parentscategory ="";
foreach((get_the_category()) as $cat) {
if ($cat->category_parent == 0) {
$parentscategory .= ' <a href="' . get_category_link($cat->cat_ID) . '" title="' . $cat->name . '">' . $cat->name . '</a>, ';
}
}
echo substr($parentscategory,0,-2);
}
?>
</span>
<h3><a href="<?php the_permalink() ?>"><?php the_title(); ?></a> (<?php comments_number( '0', '1', '%' ); ?>)</h3>
</div>
</article>
<!-- end:article --> <?php
$day_check = $day;
endwhile; ?>
</div>
<!-- end:article-container -->
</aside>
<?php if ( have_posts() ) : ?>
<?php endif; ?>
<!-- </div> -->
<?php
echo $args['after_widget'];
}
/**
* Handles updating the settings for the current Recent Posts widget instance.
*
* @since 2.8.0
*
* @param array $new_instance New settings for this instance as input by the user via
* WP_Widget::form().
* @param array $old_instance Old settings for this instance.
* @return array Updated settings to save.
*/
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = sanitize_text_field( $new_instance['title'] );
$instance['number'] = (int) $new_instance['number'];
$instance['show_date'] = isset( $new_instance['show_date'] ) ? (bool) $new_instance['show_date'] : false;
$instance['categories'] = (int) $new_instance['categories'];
$instance['post_type'] = 'any';
return $instance;
}
/**
* Outputs the settings form for the Recent Posts widget.
*
* @since 2.8.0
*
* @param array $instance Current settings.
*/
public function form( $instance ) {
$title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
$number = isset( $instance['number'] ) ? absint( $instance['number'] ) : 5;
$show_date = isset( $instance['show_date'] ) ? (bool) $instance['show_date'] : false;
$categories = isset( $instance['categories'] ) ? absint( $instance['categories'] ) : 'any';
?>
<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 $title; ?>" /></p>
<p><label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php _e( 'Number of posts to show:' ); ?></label>
<input class="tiny-text" id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" type="number" step="1" min="1" value="<?php echo $number; ?>" size="3" /></p>
<p><input class="checkbox" type="checkbox"<?php checked( $show_date ); ?> id="<?php echo $this->get_field_id( 'show_date' ); ?>" name="<?php echo $this->get_field_name( 'show_date' ); ?>" />
<label for="<?php echo $this->get_field_id( 'show_date' ); ?>"><?php _e( 'Display post date?' ); ?></label></p>
<p>
<label for="<?php echo $this->get_field_id('categories'); ?>"><?php _e( 'Filter nach Kategorie:' )?></label>
<!-- Übernommen von Powermag Theme, vorher normale Auswahl ohne Option wie in Newsliste Widget -->
<select id="<?php echo $this->get_field_id('categories'); ?>" name="<?php echo $this->get_field_name('categories'); ?>" class="widefat categories" style="width:100%;">
<option value='any'>All categories</option>
<?php $categories = get_categories('hide_empty=0&depth=1&type=post'); ?>
<?php foreach($categories as $category) { ?>
<option value='<?php echo $category->term_id; ?>' <?php if ($category->term_id == $instance['categories']) echo 'selected="selected"'; ?>><?php echo $category->cat_name; ?></option>
<?php } ?>
</select>
</p>
<?php
}
}
?>
<?php
//register_widget('WP_PCP_Widget_Recent_Posts');
add_action( 'widgets_init', create_function( '', 'register_widget( "WP_PCP_Widget_Recent_Posts2" );' ) );
?>
If I insert it I get the error: Call to undefined function of_get_option()
of_get_option() most likely is a custom theme function from your previous theme.
It seems to be from this:
https://wptheming.com/options-framework-plugin/
Not exactly sure what the function does. Consider writing one on your own for this particular purpose. :D