How to add a dynamic footer in WordPress?

2

I'm making a website in WordPress and wanted to know if there is a plugin that makes a footer to run, and I can change the footer content in backoffice .

I do not have code because I do not know how to do it. I would like to have type in backoffice an option that is a footer and then I can change the content and then in the code I just have to call this plugin .     

asked by anonymous 16.11.2015 / 15:18

1 answer

0

If you want to make an backend option within the Settings area, you must use the Settings API . To do within Theme settings you have to use the Theme Customization API . Each of them has functions to be called in the frontend and pull the values.

Another easy way is to create a Custom Post Type where you can register more than one footer option and pull on the frontend with the get_posts() function.

In this example plugin, I'm creating a post type that only supports titles, see supports => array(SUAS-OPÇÕES) :

<?php
/*
 * Plugin Name: Rodapés personalizados
 * Plugin URI: http://pt.stackoverflow.com/a/98553/201
 * Version: 1.0
 * Author: brasofilo
 */

add_action( 'init', 'footers_custom_init' );

# Ajustar $labels e $args conforme suas necessidades
function footers_custom_init() {
  $labels = array(
    'name' => 'Footers',
    'singular_name' => 'Footer',
    'add_new' => 'Add New',
    'add_new_item' => 'Add New Footer',
    'edit_item' => 'Edit Footer',
    'new_item' => 'New Footer',
    'all_items' => 'All Footers',
    'view_item' => 'View Footer',
    'search_items' => 'Search Footers',
    'not_found' =>  'No teasers found',
    'not_found_in_trash' => 'No teasers found in Trash', 
    'parent_item_colon' => 'Parent Page',
    'menu_name' => 'Footers'
  );

  $args = array(
    'labels' => $labels,
    'description' => '',
    'public' => false,
    'publicly_queryable' => true,
    'show_ui' => true, 
    'show_in_menu' => true, 
    'query_var' => true,
    'rewrite' => array(),
    'capability_type' => 'page',
    'has_archive' => false, 
    'hierarchical' => true,
    'menu_position' => 60,
    'supports' => array( 'title')
  ); 

  register_post_type( 'footers', $args );
}

if ( ! function_exists( 'imprime_rodape' ) ) {
    # Ajustar $args conforme suas necessidades
    function imprime_rodape() {
        $args = array( 'numberposts' => 1, 'orderby' => 'rand', 'post_type' => 'footers' );
        $rand_footer = get_posts( $args );        
        echo $rand_footer[0]->post_title;
    }
}

Next, in the footer.php of your theme file, use the following plugin function to print a random footer:

<?php if ( function_exists( 'imprime_rodape' ) ) imprime_rodape(); ?>
    
16.11.2015 / 19:46