wordpress, detecting number of widets inside a div - php

i am using wordpress and have come at a point that i have to hide the div if there is no widget displayed in it.
can some one tell me the code for detection of number of widgets displayed in a specific area.

try this
<?php if ( is_active_sidebar( 'your-widget-area' ) ) : ?>
<!--This div will not display if there is no active widgets
so place your div inside if condition-->
<div>
<?php dynamic_sidebar( 'your-widget-area' ); ?>
</div>
<?php endif; ?>

Related

Understrap customize Blog Page - Unclear which page and where to modify

I am new to Understrap and I would like to add some margin-top to the blog page that displays all the posts. Unlike other blank wordpress theme starters I have used, I don't yet know the structure in order to add margin top to just the posts page. Between content.php single.php and content-single, I am a bit lost. If any one can orient me on this, I would be very thankful.
I found that I had to modify the index.php page.
I also wanted to add a header to my Resent posts page so I modified the index php as such:
<div class="wrapper" id="index-wrapper">
<div class="darkness"></div>
<div class="<?php echo esc_attr( $container ); ?>" id="content" tabindex="-1">
<div class="row">
<!-- Do the left sidebar check and opens the primary div -->
<?php get_template_part( 'global-templates/left-sidebar-check' ); ?>
<main class="site-main" id="main">
<h1 class="mb-3">Recent Posts</h1>
<?php if ( have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
The div with the class darkness was to add some extra margin and color to the top of the page to go behind the transparent header. In the end, this is what I needed.

How do I tell WooCommerce NOT to display HTML if it is a category page (in PHP)?

I am trying to add some HTML code into the WordPress main shop page, but not have it appear on the category pages. I am using the code below, but the HTML is still showing up on the category pages. I also tried using a category array with the category names, but it wasn't working. Is there a specific format for the category names (like no capitals or &'s)? What am I missing?
Thank you ahead of time!!
<?php if (!is_category( ) ) ?>
<div class="shop_giftcard_box">
<div class="shop_giftcard_image"><img src="http://example.com/wp-content/uploads/2017/03/gift_card_3.png"></div>
<div class="shop_giftcard_subheading"><h3>Gift Card</h3></div>
</div>
<?php ?>
Your PHP condition is missing some parts, try this:
<?php if ( !is_product_category() ) : ?>
<div class="shop_giftcard_box">
<div class="shop_giftcard_image"><img src="http://example.com/wp-content/uploads/2017/03/gift_card_3.png"></div>
<div class="shop_giftcard_subheading"><h3>Gift Card</h3></div>
</div>
<?php endif; ?>

Wrap wordpress post and title in custom div

I'm trying to prepare a blog for microformat, so I need a div to start above the title and close just above the social share buttons (and below meta data, date, author, # of views, etc) - see site for reference: http://www.sara-maria.dk/sundt/laekre-saltede-mandler-med-soedt-tilbehoer/
It is a Wordpress site using CherryFramework with a childtheme and I've tried the following:
adding the opening of the div to title.php and the closing div to footer.php
However, for some reason the div is not using the expected closing div. Instead it is being closed way up higher on the page.
I've created a new functions.php in the childtheme and used the following code:
function my_content($content) {
global $post;
return ''.$content.'';
}
add_filter('the_content', 'my_content');
Problem is that this only wraps it around the post and my PHP skills are not very good, so I haven't been able to customize it to include the title and the meta data.
Anyone who can help me how I best can create the custom div?
Thanks,
Kasper
Update - copying in loop-single.php on request from dojs:
<?php /* Loop Name: Single */ ?>
<div id="loopTEST">
<?php if (have_posts()) : while (have_posts()) : the_post();
// The following determines what the post format is and shows the correct file accordingly
$format = get_post_format();
get_template_part( 'includes/post-formats/'.$format );
if($format == '')
get_template_part( 'includes/post-formats/standard' );
get_template_part( 'includes/post-formats/share-buttons' );
wp_link_pages('before=<div class="pagination">&after=</div>');
?>
</div>
<!---removed author block--->
<?php
get_template_part( 'includes/post-formats/related-posts' );
comments_template('', true);
endwhile; endif;
?>
Update
If you look at the DOM of your site you can clearly see that the title section is found in it's own file.
Take a look at this HTML
<div class="row">
<div class="span12" data-motopress-type="static" data-motopress-static-file="static/static-title.php">
<section class="title-section">
<h1 class="title-header">
Lækre saltede mandler med sødt tilbehør </h1>
<!-- BEGIN BREADCRUMBS-->
...
<!-- END BREADCRUMBS -->
</section><!-- .title-section -->
</div>
</div>
You would think that you have to add a div to "static/static-title.php", but that would most likely destroy the layout.
To be honest, the structure of this theme seems horrible to me (which means that the theme is shit), however if you are hell bent on using it you need to find the file (which would most likely be "single.php" in your themes root directory) that includes "static/static-title.php" and add a div on the line above that.
Okay, well to really see how this builds up your single post pages you might need to go through the included template parts, but try this to start out with.
<div id="loopTEST">
<?php if (have_posts()) : while (have_posts()) : the_post();
$format = get_post_format();
?>
<div> <!-- This should be above the title -->
<?php
get_template_part( 'includes/post-formats/'.$format );
if($format == '')
get_template_part( 'includes/post-formats/standard' );
?>
</div> <!-- This should be below the post but above the social media buttons -->
<?php
get_template_part( 'includes/post-formats/share-buttons' );
wp_link_pages('before=<div class="pagination">&after=</div>');
?>
</div>

displaying wordpress pages not showing the correct format

I am using this PHP Code on my website to get wordpress page content from the database:
$page = get_page_by_id($records["value"], OBJECT, 'page');
query_posts('p='.$page->ID.'');
echo '<div id="page-title">'.$page->post_title.'</div>';
echo nl2br($page->post_content);
echo '<p> </p><hr /><p> </p>';
in wordpress, i have put the page with HTML and aligned images etc but when it displays on my website it just shows the images and text all under each other with the incorrect format (not as i put it in the text editor)
what have i done wrong here?
Im also wondering why you are doing like this? there is no need to call post title and content that way. simply make a page.php. The TwentyFourteen does have page template but in a folder named "page-templates". You can simply make a page.php by sample code below.
<?php get_header(); ?> //the header template
<?php while ( have_posts() ) : the_post(); ?> //start the loop
<?php the_title(); ?> //Page Title
<?php the_content(); ?> //The content
<?php endwhile; ?> //end loop
<?php get_sidebar(); ?> //sidebar template
<?php get_footer(); ?> // footer template
you can add div / span wherever you want.

Wordpress, wrap posts in div

I'm quite new to wordpress, so bear with me. I have a site up and running allowing me to add content dynamically, the only problem is styling each post. I'm looking to wrap posts in a div, so images would be wrapped in one div and text posts would be wrapped in another, if that makes sense?Here's some code I've been using:
<?php if ( in_category('photos') ) { ?>
<div id="testing">
<?php } else { ?>
<div id="test-2">
<?php } ?>
The only problem with this is that it wraps all posts in a given category and doesn't put a closing '< / div >' after the content, if that makes sense?
Well, the only reason you're not getting any closing </div>s is because you're not putting it anywhere in your php.
This should work:
<?php while( have_posts() ) : the_post()
if( in_category( 'photos' ) { ?>
<div id="testing">
// All your post content and whatnot
<?php the_content(); ?>
</div>
<?php } else { ?>
<div id="test-2">
// All your post content
<?php the_content(); ?>
</div>
<?php }
endwhile; ?>
If you don't write complete markup in your theme files, you can't possibly expect Wordpress, or PHP, to automatically close your HTML tags.
"if that makes sense" No it doesn't really make sense as Wordpress by default adds a CSS-class for every category and tag to the surrounding div. If that is not the case in your theme, use the function post_class() to add the CSS-classes to your div.
See the post_class()-documentation for usage examples.

Categories