WordPress.org

Ready to get started?Download WordPress

Forums

How To: Display all posts in specific category in dashboard widget? (14 posts)

  1. ezabinski
    Member
    Posted 1 year ago #

    I'm trying to display all posts titles in a specific category "Chicago" to be displayed in a dashboard widget. I'm just looking for the titles to be displayed in the dashboard, then the user would click the title and it opens to a new page and displays the entire post.

    I did some research and on http://codex.wordpress.org/Dashboard_Widgets_API

    I found this template code:

    // Create the function to output the contents of our Dashboard Widget
    
    function example_dashboard_widget_function() {
    	// Display whatever it is you want to show
    	echo "Hello World, I'm a great Dashboard Widget";
    } 
    
    // Create the function use in the action hook
    
    function example_add_dashboard_widgets() {
    	wp_add_dashboard_widget('example_dashboard_widget', 'Example Dashboard Widget', 'example_dashboard_widget_function');
    } 
    
    // Hook into the 'wp_dashboard_setup' action to register our other functions
    
    add_action('wp_dashboard_setup', 'example_add_dashboard_widgets' ); // Hint: For Multisite Network Admin Dashboard use wp_network_dashboard_setup instead of wp_dashboard_setup.

    I'm not sure what to do from here. Does anyone have the code already written or can help me out?

    I'm new to WordPress and HTML/PHP. Thank you for helping.

    Eric

  2. keesiemeijer
    moderator
    Posted 1 year ago #

    Try it with this in your functions.php:

    function example_dashboard_widget_function() {
    
    	$posts = get_posts('category_name=chicago&posts_per_page=-1');
    
    	if($posts){
    		echo '<p>';
    		foreach ($posts as $post) {
    			echo '<a href="' . get_edit_post_link( $post->ID) . '">' . $post->post_title .'</a><br/>';
    		}
    		echo '</p>';
    	}
    }
    
    // Create the function use in the action hook
    
    function example_add_dashboard_widgets() {
    	wp_add_dashboard_widget('example_dashboard_widget', 'Category Chicago', 'example_dashboard_widget_function');
    } 
    
    // Hook into the 'wp_dashboard_setup' action to register our other functions
    
    add_action('wp_dashboard_setup', 'example_add_dashboard_widgets' ); // Hint: For Multisite Network Admin Dashboard use wp_network_dashboard_setup instead of wp_dashboard_setup.
  3. ezabinski
    Member
    Posted 1 year ago #

    Error message:
    Parse error: syntax error, unexpected '"', expecting ',' or ';' in /home/dermbids/public_html/wp-content/themes/Lucid/functions.php on line 400

    It prevented me from logging back in and website loading. Went into the FTP to modify the function.php.

    Any other suggestions?

  4. ezabinski
    Member
    Posted 1 year ago #

    Disregard that last message. I copied the code wrong.

    The error message is
    Fatal error: Cannot redeclare example_dashboard_widget_function() (previously declared in /home/dermbids/public_html/wp-content/themes/Lucid/functions.php:381) in /home/dermbids/public_html/wp-content/themes/Lucid/functions.php on line 406

  5. keesiemeijer
    moderator
    Posted 1 year ago #

    Strange, I've tested the code I've posted here and have no errors.

    Can you post the code of your theme's functions.php (with my code inside). See the forum rules on how to post code.

  6. ezabinski
    Member
    Posted 1 year ago #

    I hope I did this right. But my function.php code that is causing problems can be viewed

    http://pastebin.com/xMisA58p

  7. keesiemeijer
    moderator
    Posted 1 year ago #

    You didn't replace your old code with mine but added it. This means there are two functions with the same name. This is not allowed with php. try it with this functions.php: http://pastebin.com/ruChg3QE

  8. ezabinski
    Member
    Posted 1 year ago #

    It Works!

    Sorry for the amateur move.

    Is there a way to have this widget appear only to a user group, such as Contributors?

  9. keesiemeijer
    moderator
    Posted 1 year ago #

    No problem. I can't even count how many times I've done this also.

    For WordPress roles (not custom created Roles) try and change this [untested]:

    wp_add_dashboard_widget('example_dashboard_widget', 'Category Chicago', 'example_dashboard_widget_function');

    to this:

    if ( current_user_can( 'contributor' ) ) {
    wp_add_dashboard_widget('example_dashboard_widget', 'Category Chicago', 'example_dashboard_widget_function');
    }
  10. ezabinski
    Member
    Posted 1 year ago #

    I found a round about way to assign it to a user role. It will work for now; however, I did notice when the widget is present on Admin, it take you to edit the post.

    When it's a user with less permission clicks on the link, it redirects to the index.php, resulting in no change.

    in the code it states

    a href="' . get_edit_post_link

    I would like it to just show the post, not edit, for all roles. How do I make this happen?

    Thanks,

  11. ezabinski
    Member
    Posted 1 year ago #

    If it shows up as edit for admin thats ok.

    But for other roles, I would like it just to view the post

  12. keesiemeijer
    moderator
    Posted 1 year ago #

    Try changing this:

    get_edit_post_link( $post->ID)

    to this:

    get_permalink( $post->ID )
  13. ezabinski
    Member
    Posted 1 year ago #

    Works!

    Now if i wanted to create an additional custom widget lets so for "Cars" it's not as simple as changing the code from Chicago to Cars is it?

  14. keesiemeijer
    moderator
    Posted 1 year ago #

    For multiple category dashboard widgets you can try something like this:

    function example_dashboard_widget_function( $widget, $args) {
    
    	$cat = '';
    	// check the title of the widget to see what category is needed
    	if($args['title'] == 'Category Chicago') {
    		$cat_slug = 'chicago';
    	}
    	if($args['title'] == 'Category Cars') {
    		$cat_slug = 'cars';
    	}
    
    	if('' != $cat) {
    	$posts = get_posts('category_name=' . $cat_slug . '&posts_per_page=-1');
    
    	if($posts){
    		echo '<p>';
    		foreach ($posts as $post) {
    			echo '<a href="' . get_permalink( $post->ID) . '">' . $post->post_title .'</a><br/>';
    		}
    		echo '</p>';
    	}
    	}
    }
    
    // Create the function use in the action hook
    
    function example_add_dashboard_widgets() {
    	if ( current_user_can( 'contributor' ) ) {
    		wp_add_dashboard_widget('chicago_dashboard_widget', 'Category Chicago', 'example_dashboard_widget_function');
    		wp_add_dashboard_widget('car_dashboard_widget', 'Category Cars', 'example_dashboard_widget_function');
    	}
    } 
    
    // Hook into the 'wp_dashboard_setup' action to register our other functions
    
    add_action('wp_dashboard_setup', 'example_add_dashboard_widgets' ); // Hint: For Multisite Network Admin Dashboard use wp_network_dashboard_setup instead of wp_dashboard_setup.

Topic Closed

This topic has been closed to new replies.

About this Topic