WordPress: Adding a widget area to the primary page template? - php

I'm trying to add a widget area to the main body of page.php, so that I can place unique widgets on each page of my site. I've followed the instructions here: http://codex.wordpress.org/Widgetizing_Themes
I added this to functions.php in my theme:
function arphabet_widgets_init() {
register_sidebar( array(
'name' => 'Main widget',
'id' => 'main_widget_area',
'before_widget' => '<div>',
'after_widget' => '</div>',
'before_title' => '<h2 class="rounded">',
'after_title' => '</h2>',
) );
}
add_action( 'widgets_init', 'arphabet_widgets_init' );
and this is page.php:
get_header(); ?>
<div id="primary" class="content-area">
<div id="content" class="site-content" role="main">
<?php if ( is_active_sidebar( 'main_widget_area' ) ) : ?>
<div id="main-widget" class="widget-area main-widget">
<?php dynamic_sidebar( 'main_widget_area' ); ?>
</div><!-- #primary-sidebar -->
<?php endif; ?>
<?php /* The loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
<?php if ( has_post_thumbnail() && ! post_password_required() ) : ?>
<div class="entry-thumbnail">
<?php the_post_thumbnail(); ?>
</div>
<?php endif; ?>
<h1 class="entry-title"><?php the_title(); ?></h1>
</header><!-- .entry-header -->
<div class="entry-content">
<?php the_content(); ?>
<?php wp_link_pages( array( 'before' => '<div class="page-links"><span class="page-links-title">' . __( 'Pages:', 'twentythirteen' ) . '</span>', 'after' => '</div>', 'link_before' => '<span>', 'link_after' => '</span>' ) ); ?>
</div><!-- .entry-content -->
<footer class="entry-meta">
<?php edit_post_link( __( 'Edit', 'twentythirteen' ), '<span class="edit-link">', '</span>' ); ?>
</footer><!-- .entry-meta -->
</article><!-- #post -->
<?php comments_template(); ?>
<?php endwhile; ?>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
(I added the top section, with main_widget_area)
However when I edit the page I see no way to drop in a widget. I'm not sure if I'm missing a step, or if I'm just not editing the page in the right way.

Take a look at the "Widgets" menu underneath "Appearance" in the WordPress backend. There will be a list of widgets on the left side of the page and a list of available sidebars on the right. Just drag-and-drop the widget you want into the sidebar you registered and you should be good to go.

Related

Sidebar is appearing on custom post pages without being called

I'm creating a Wordpress theme for a graphic portfolio website, and I'm utilizing the sidebar.php for a hero image on the index page that is not meant to appear on other pages. While this works for all static inside pages, I have two custom post categories, and the sidebar section has started appearing above those posts.
I should note that this hasn't always been a problem, and started cropping up for no apparent reason while working on something else.
On index.php, the sidebar is called as follows:
<?php get_header(); ?>
<?php get_sidebar(); ?>
<?php
if ( have_posts() ) : while ( have_posts() ) : the_post();
get_template_part( 'content', get_post_format() );
endwhile; ?>
<nav>
<ul class="pager">
<li><?php next_posts_link( 'Previous' ); ?></li>
<li><?php previous_posts_link( 'Next' ); ?></li>
</ul>
</nav>
<?php endif;?>
<?php get_footer(); ?>
While page.php is similar but lacks the php line calling the sidebar, as follows:
<?php get_header(); ?>
<div class="row">
<div class="col-sm-12">
<?php
if ( have_posts() ) : while ( have_posts() ) : the_post();
get_template_part( 'content', get_post_format() );
endwhile; endif;?>
</div> <!-- /.col -->
</div> <!-- /.row -->
<?php get_footer(); ?>
page-fine-art.php also lacks that php line, as follows:
<div class="row">
<div class="col-sm-12">
<?php
$args = array(
'post_type' => 'fine-art',
'orderby' => 'menu_order',
'order' => 'ASC'
);
$custom_query = new WP_Query( $args );
while ($custom_query->have_posts()) : $custom_query->the_post(); ?>
<div class="blog-post">
<h2 class="blog-post-title"><? php the_title(); ?></h2>
<p class="blog-post-meta"><?php the_date(); ?> by <?php the_author(); ?></p>
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail();
} ?>
<?php the_excerpt(); ?>
<?php endwhile; ?>
</div> <!-- /.col -->
</div> <!-- /.row -->
<?php get_footer(); ?>
The function creating the the custom posts for "fine art" goes:
function create_my_custom_posts() {
register_post_type( 'fine-art',
array(
'labels' => array(
'name' => __( 'Fine Art' ),
'singular_name' => __( 'Fine Art' ),
),
'public' => true,
'has_archive' => true,
'supports' => array(
'title',
'editor',
'thumbnail',
'custom-fields'
)
));
I don't see where sidebar.php is being called in the final example, or where else it could be coming from.

how to customize post in wordpress?

I try to customize my own post using the shortcode way in wordpress. I want to display the post according to its category.
<?php
function sample_post($atts) {
extract(shortcode_atts(array(
'category_name' => 'uncategorized'
), $atts));
$args = array('category_name' => $category_name);
query_posts($args);
if (have_posts()) : while (have_posts()) : the_post(); ?>
<label><?php the_title(); ?></label>
<?php
endwhile;
endif;
}
add_shortcode('sample_post', 'sample_post');
?>
the code is fine and I have a file in my template name content-page.php
here is the code of my content-page
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
<h1 class="entry-title"><?php the_title(); ?></h1>
</header><!-- .entry-header -->
<div class="entry-content">
<?php the_content(); ?>
<?php
wp_link_pages( array(
'before' => '<div class="page-links">' . __( 'Pages:', 'unite' ),
'after' => '</div>',
) );
edit_post_link( __( 'Edit', 'foodgrower' ), '<span class="edit-link">', '</span>' );
?>
</div><!-- .entry-content -->
</article><!-- #post-## -->
I don't know if I miss something about the setup or query on it. Here's I really want to displays. I only want to display <label><?php the_title(); ?></label> (the title of the post), but now it displays the content-page.php. I don't get it also why it appears in my page. I didn't call the content-page.php to display in my page.
Change your code to return the data, like the following,
function sample_post($atts) {
$return = '';
extract(shortcode_atts(array(
'category_name' => 'uncategorized'
), $atts));
$args = array('category_name' => $category_name);
$the_query = new WP_Query( $args );
if ($the_query->have_posts()) : while ($the_query->have_posts()) : $the_query->the_post();
$return .= "<label>".the_title()."</label>";
endwhile;
endif;
return $return;
}
add_shortcode('sample_post', 'sample_post');

PhP Footer error

Hello i was trying change the footer of my wordpress site to somthing else. I thought i was just changing the words but i some how messed up the code. I don't know enough about php to fix was i did
Error is on line 40
<?php if ( 'on' == et_get_option( 'divi_back_to_top', 'false' ) ) : ?>
<span class="et_pb_scroll_top et-pb-icon"></span>
<?php endif;
if ( ! is_page_template( 'page-template-blank.php' ) ) : ?>
<footer id="main-footer">
<?php get_sidebar( 'footer' ); ?>
<?php
if ( has_nav_menu( 'footer-menu' ) ) : ?>
<div id="et-footer-nav">
<div class="container">
<?php
wp_nav_menu( array(
'theme_location' => 'footer-menu',
'depth' => '1',
'menu_class' => 'bottom-nav',
'container' => '',
'fallback_cb' => '',
) );
?>
</div>
</div> <!-- #et-footer-nav -->
<?php endif; ?>
<div id="footer-bottom">
<div class="container clearfix">
<?php
if ( false !== et_get_option( 'show_footer_social_icons', true ) ) {
get_template_part( 'includes/social_icons', 'footer' );
}
?>
<p id="footer-info"><?php printf( __( (changed this) %1$s | Powered by %2$s', 'Divi' ), 'Elegent themes (and this) ', 'WordPress' ); ?></p>
</div> <!-- .container -->
</div>
</footer> <!-- #main-footer -->
</div> <!-- #et-main-area -->
<?php endif; // ! is_page_template( 'page-template-blank.php' ) ?>
</div> <!-- #page-container -->
<?php wp_footer(); ?>
</body>
</html>
I really have no idea what is wrong. I understand html. Thanks for any help.
You have missing single quote in the code, please use following code, I have fixed the code by restoring to original Divi code line
<?php if ( 'on' == et_get_option( 'divi_back_to_top', 'false' ) ) : ?>
<span class="et_pb_scroll_top et-pb-icon"></span>
<?php endif;
if ( ! is_page_template( 'page-template-blank.php' ) ) : ?>
<footer id="main-footer">
<?php get_sidebar( 'footer' ); ?>
<?php
if ( has_nav_menu( 'footer-menu' ) ) : ?>
<div id="et-footer-nav">
<div class="container">
<?php
wp_nav_menu( array(
'theme_location' => 'footer-menu',
'depth' => '1',
'menu_class' => 'bottom-nav',
'container' => '',
'fallback_cb' => '',
) );
?>
</div>
</div> <!-- #et-footer-nav -->
<?php endif; ?>
<div id="footer-bottom">
<div class="container clearfix">
<?php
if ( false !== et_get_option( 'show_footer_social_icons', true ) ) {
get_template_part( 'includes/social_icons', 'footer' );
}
?>
<p id="footer-info"><?php printf( __( 'Designed by %1$s | Powered by %2$s', 'Divi' ), 'Elegant Themes', 'WordPress' ); ?></p>
</div> <!-- .container -->
</div>
</footer> <!-- #main-footer -->
</div> <!-- #et-main-area -->
<?php endif; // ! is_page_template( 'page-template-blank.php' ) ?>
</div> <!-- #page-container -->
Specifically you have omitted the single quote from this line
<p id="footer-info"><?php printf( __( 'Designed by
See single quote just after __( , that you had omitted from your original code.

Appy Wordpress Widget to Widget area

I'm working the first time with WordPress, now I'm trying to add a custom Widget to an HTML box.
The HTML: The blank News box
I started to read a bit about widgets and the tutorials told me to create a new Widget area inside my functions.php document, which i did with the following code:
<?php
/**
* Register our sidebars and widgetized areas.
*
*/
function arphabet_widgets_init() {
register_sidebar( array(
'name' => 'Home right sidebar',
'id' => 'home_right_1',
'before_widget' => '<div>',
'after_widget' => '</div>',
'before_title' => '<h2 class="rounded">',
'after_title' => '</h2>',
) );
}
add_action( 'widgets_init', 'arphabet_widgets_init' );
?>
Now i went into my index.php document, which stores the HTML so I tried to add the following code to the HTML blank news box:
<?php if ( is_active_sidebar( 'sidebar-1' ) ) : ?>
<div id="primary-sidebar" class="primary-sidebar widget-area" role="complementary">
<?php dynamic_sidebar( 'sidebar-1' ); ?>
</div><!-- #primary-sidebar -->
<?php endif; ?>
After i finished this also, i went into my WordPress admin menu and applied the plugin to the widget.
But it wouldn't show up...
Does anyone have a idea why?
update your index.php code as below:
<?php if ( is_active_sidebar( 'home_right_1' ) ) : ?>
<div id="primary-sidebar" class="primary-sidebar widget-area" role="complementary">
<?php dynamic_sidebar( 'home_right_1' ); ?>
</div><!-- #primary-sidebar -->
<?php endif; ?>

Sidebar does not show widgets but individual sidebar content

My dynamic sidebar does not show standard widgets like recent posts or archives but the individual content I put into the sidebar.php
This is the sidebar-part from my functions.php:
add_action( 'widgets_init', 'my_register_sidebars' );
function my_register_sidebars() {
register_sidebar( array(
'name' => __( 'Primäre Sidebar' ),
'id' => 'sidebar-main',
'description' => __( 'Rechte Hauptsidebar für Werbeflächen und Meta' ),
'before_title' => '<h4>',
'after_title' => '</h4>',
) );
and this is the code from sidebar.php:
<aside id="sidebar-main">
<div id="widgetarea">
<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar() ) : endif; ?>
<h4>TESTESTEST</h4>
<p>HALLOHALLO</p>
</div>
</aside>
As always, I call the sidebar by <?php get_sidebar(); ?> in my theme.
How come the other stuff is being displayed but not the widgets? I have no clue.
Edit: I already tried deactivating plugins - no change.
Your call to your sidebar is wrong. This
<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar() ) : endif; ?>
should be
<?php if ( is_active_sidebar( 'sidebar-main' ) ) : ?>
<?php dynamic_sidebar( 'sidebar-main' ); ?>
<?php endif; ?>
try this in sidebar.php:
<aside id="sidebar-main">
<div id="widgetarea">
<?php dynamic_sidebar('Primäre Sidebar') ?>
</div>
</aside>

Categories