wordpress - dynamic sidebar will not hold any widgets - php

Here is the code in functions.php:
register_sidebar(array(
'name'=> 'Home Page Advanced Search',
'id' => 'custom'
));
register_sidebar(array(
'name'=> 'Profile Picture Upload',
'id' => 'pic upload'
));
Then, I use the two sidebars in my template like this:
?php if ( is_active_sidebar( 'custom' )) : ?>
<div id="widget-area">
<?php dynamic_sidebar( 'custom' ); ?>
</div>
<?php endif; ?>
<?php if ( is_active_sidebar( 'pic upload' )) : ?>
<div id="upload-photo-area">
<?php dynamic_sidebar( 'pic upload' ); ?>
</div>
<?php endif; ?>
The sidebar with id='custom' works fine. I am able to put widgets in it in my dashboard and display them. The sidebar with id='pic upload' is not working correctly. All widgets that I put inside of it do not stay there (in the dashboard API) and, therefore, do not display on my page.
All help is appreciated. Thanks in advance.

You can't have a space in the name of a sidebar. As per the codex
If you name your own ID values in the register_sidebar() WordPress
function, you might increase readability of the code. The ID should be
all lowercase alphanumeric characters and not contain white space. You
can also use the - and _ characters. IDs must be unique and cannot
match a sidebar name. Using your own IDs can also make the sidebar
name translatable.
I added the emphases.
Change pic upload to pic-upload or pic_upload should fix that.

Related

How to add posts with navigation in home.php?

I have a theme I'm building and I'm trying to work on the homepage from home.php. I already have my listing of latest articles generating, but I want the page navigation to show up at the bottom for the next and/or previous set of articles. Ideally, some sort of lazy load too, but at the moment, I just want to understand how to do the page navigation part.
I'm having trouble finding articles on how to do this without "add THIS plugin" or "use THIS theme".
Here's what I already have to generate my articles section:
<section id="articles" class="container">
<?php
$grid_posts = get_posts( array(
'posts_per_page' => 12
) );
if ( $grid_posts ) {
foreach ( $grid_posts as $post ) :
setup_postdata( $post ); ?>
<?php
if ( has_post_thumbnail() ) : get_template_part('template-parts/posts-grid', 'image');
else : get_template_part('template-parts/posts-grid', 'default');
endif;
?>
<?php
endforeach;
wp_reset_postdata();
}
?>
</section>
I just want to find a resource that shows me how to either "load more articles" on that page or show the "next/previous page" navigation.
Thanks
You're looking for wp_link_pages
wp_link_pages( array(
'before' => '<div class="page-links">' . esc_html__( 'Pages:', 'test' ),
'after' => '</div>',
) );
For those kind of questions and to get a basic understanding, it's also always worth at least looking into a "template theme" like _s
In your case, the file to look at would be this one

Wordpress Template. Display contents of a Text Widget

I have created a wordpress template.
I have added some HTML into a Text widget and saved it.
Is there a way to add some code into my template so I can get that widget to be displayed?
Is this possible instead of having to enter the html code directly into my template?
Your Text widget will be inside of a sidebar. So if you have a sidebar called "left-sidebar", you can use code like this in your template.
<?php if ( is_active_sidebar( 'left-sidebar' ) ) : ?>
<ul id="sidebar">
<?php dynamic_sidebar( 'left-sidebar' ); ?>
</ul>
<?php endif; ?>
Now all the widgets you put in left-sidebar will be included in that template automatically.
There's more information about dynamic_sidebar here: https://codex.wordpress.org/Function_Reference/dynamic_sidebar
Note that you can also create new sidebars using register_sidebar in your functions.php, so if you want to use different sidebars in different templates you can. https://codex.wordpress.org/Function_Reference/register_sidebar
Yes it is infact it's very simple, you have to create a sidebar for where you wan that widget to go. In your main php file or wherever you want that widget to appear you can add the following code:
<?php if ( !function_exists('dynamic_sidebar')
|| !dynamic_sidebar('sidebar1') ) : ?>
<?php endif; ?>
Then you would also have to declare that sidebar within the functions.php file like so:
if ( function_exists('register_sidebar') )
register_sidebar(array('name'=>'sidebar1',
'before_widget' => '',
'after_widget' => '',
'before_title' => '',
'after_title' => '',
));
and you can have as many sidebars that you want, just give them a different name like sidebar2 etc..

Create a Custom WordPress Page with Post titles

I am trying to create a custom WordPress page that will contain only the links to all my post titles, divided on 4 column. I am also using Bootstrap with WordPress.
I created the php file, created a new page with her page atribute, but the post titles don't display.
This is the code i used:
<?php
/**
* The template used for displaying page content in questions.php
*
* #package fellasladies
*/
?>
<?php
<article id="post-<?php the_ID(); ?>" <?php post_class('col-md-4 col-sm-4 pbox'); ?>>
<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:', 'fellasladies' ),
'after' => '</div>',
) );
?>
</div><!-- .entry-content -->
<?php edit_post_link( __( 'Edit', 'fellasladies' ), '<footer class="entry-meta"><span class="edit-link">', '</span></footer>' ); ?>
</article><!-- #post-## -->
I really appreciate your help! Thanks
You'll need to start by creating a Query which fills an array with the posts you want to iterate. Read about the get_posts() function in WordPress.
Here's an example. Note that we can't use functions which are meant to be used "in the loop" such as the_title() or the_content(). We must specify the post_id for each iteration. We should not modify the main query for situations such as this.
// the arguments for the get_posts() function
$args = array(
'post_type' => 'post', // get posts int he "post" post_type
'posts_per_page' => -1 // this means the array will be filled with all posts
);
$my_posts = get_posts($args);
// now we'll iterate the posts
foreach ( $my_posts as $p ) {
// a title
echo get_the_title($p->ID);
// the link
echo get_permalink($p->ID);
// a custom field value
echo get_post_meta($p->ID,'custom_field_key',true);
}
Theming what happens inside each iteration is up to you.
Good luck! :)
I encourage you to go read about Page Templates on Wordpress Codex, that could help you a lot!
Pages are one of WordPress's built-in Post Types. You'll probably want most of your website Pages to look about the same. Sometimes, though, you may need a specific Page, or a group of Pages, to display or behave differently. This is easily accomplished with Page Templates.
It seems that you have a <?php useless. You also don't define your template's name, which is required.

Wordpress, the portfolio page is using the heading 'Blog Archives' and I want it to say 'Portfolio'

On my Wordpress site, when I click on my Portfolio Category, it shows the title 'Blog Archives' which is not what I would like.
I would like a title of 'Portfolio' . So I've found the template file for archive.php , and it basically says if the variable is_Day, is_Month or is_Year is set, then show the relevant archive title, else show 'Blog Archive' , see the code below
<h2>
<?php if ( is_day() ) : /* if the daily archive is loaded */ ?>
<?php printf( __( 'Daily Archives: <span>%s</span>' ), get_the_date() ); ?>
<?php elseif ( is_month() ) : /* if the montly archive is loaded */ ?>
<?php printf( __( 'Monthly Archives: <span>%s</span>' ), get_the_date('F Y') ); ?>
<?php elseif ( is_year() ) : /* if the yearly archive is loaded */ ?>
<?php printf( __( 'Yearly Archives: <span>%s</span>' ), get_the_date('Y') ); ?>
<?php else : /* if anything else is loaded, ex. if the tags or categories template is missing this page will load */ ?>
<?php _e('Blog Archive', 'theme5820'); ?>
<?php endif; ?>
</h2>
It appears to me that the Portfolio page is using the archive template to display posts, because above it says 'if anything else is loaded, ex. if the tags or categories template is missing this page will load'
So my question would really be, is it ok to just change 'Blog Archive' to Portfolio, or should I create a page for the portfolio category, as the portfolio pages themselves show the title ok.
Cheers, Rich :)
First, see if there's a category.php template. If so, copy it to category-portfolio.php; if not, copy archive.php to category-portfolio.php. Then edit the new file and remove whatever header code you don't want (if there's a category.php the code may be somewhat different from archive.php since it would be more specific to category displays). WP will automatically use the new template when you select the Portfolio category.
(I'm assuming this is in your own theme. If not, create a child theme, put the new file there, and select it. That way you won't get burned if your current theme is upgraded).

WordPress Theme from scratch, widgets don't work

I know this topic has been discussed quite a bit but I've tried everything I can find online and nothing is working. I need a single widget area in my custom template. I wrote if from scratch and didn't use a barebones type starter therefore I didn't have a functions.php to start from.
In wp-admin it shows my widget as it should but when there is a widget in that area and the page is reloaded it resets. in other words the widget doesn't persist in the area it's supposed to. am I missing something? is my wordpress install bad?
here is my functions.php in it's entirety
<?php
register_sidebar
(
array(
'name' => 'Header Widget',
'id' => 'headerBanner',
'before_widget' => '<div id="banner">',
'after_widget' => '</div>',
'before_title' => '',
'after_title' => '',
)
)
?>
and my index.php
<?php if ( !dynamic_sidebar (1)) : ?>
<h1>it didn't work</h1>
<?php endif; ?>
try:
<?php if(!dynamic_sidebar ('headerBanner')) : ?>
<h1>it doesn't work</h1>
<?php endif; ?>
You need to pass the id of the sidebar you want to display when calling dynamic_sidebar
Also note that dynamic_sidebar will return false if there are no widgets added to the sidebar so add a widget to it and see if it works
Have you tried this other approach?
<?php dynamic_sidebar( 'headerBanner' ); ?>

Categories