WordPress widget outputs a unwanted number - php

I made a WordPress widget that is only supposed to display an image but is displaying a number also. I don't know why this happens.
Widget output code:
public function widget( $args, $instance ) {
?>
<img src="<?= $instance['image'] ?>" alt="" class="img-responsive">
<?php
}
Widget area it's in:
<div class="spotlight">
<?= dynamic_sidebar('header') ?>
</div>
Unwanted output (the image is correct but it also prints a "1"):
Any help on how to fix this?

Change your code to:
<div class="spotlight">
<?php dynamic_sidebar('header'); ?>
</div>
The <?= short open tag means <?php echo and dynamic_sidebar function returns true (which is converted to number in your case) if widget sidebars exists.

Related

why are two paragraphs being added on wordpress custom excerpt function used?

I'm trying to develop wordpress site, where I'm using a custom excerpt function at front-end to display my post content. code is the following:
<h5 class="post-title"> <?php the_title() ?> </h5>
<?php the_post_thumbnail('home') ?>
<p class="content excerpt"> <?php news_excerpt(25) ?> </p><hr />
But, the problem is that two content showing paragraphs are adding at browser output. The output is the following:
<p class="content excerpt"> <a href="http://localhost/wordpress/index.php/2021/01/11/%e0%a6%9f%e0%a6%bf%e0%a6%95%e0%a6%be-%e0%a6%aa%e0%a7%87%e0%a6%a4%e0%a7%87-%e0%a7%a8%e0%a7%ac-%e0%a6%9c%e0%a6%be%e0%a6%a8%e0%a7%81%e0%a7%9f%e0%a6%be%e0%a6%b0%e0%a6%bf-%e0%a6%a5%e0%a7%87%e0%a6%95/"> <!-- wp:paragraph -->
<p>চলতি মাসের ২১ থেকে ২৫ তারিখের মধ্যে দেশে করোনাভাইরাসের টিকা আসবে। আর এই টিকা দেওয়া শুরু হবে ফেব্রুয়ারির প্রথম সপ্তাহে। এ জন্য নিবন্ধন </a></p>
How can I solve this problem?
Thank you.
Go with this function for paragraphs operations on content :
wpautop( string $pee, bool $br = true )
https://developer.wordpress.org/reference/functions/wpautop/
At last, I myself develop a solution to this problem. this is as follows:
function my_content()
{
echo trim(strip_tags(get_the_content()));
}
function get_my_content()
{
return trim(strip_tags(get_the_content()));
}
And it worked fine for me.
Thanks all.

Show images from TEXT section on PAGE wordpress

I am trying to show all images on a Page in wordpress. My wordpress page is set up like you see in the image below.
What I want to do is show each of these images on a custom page I have made, called Portrait Album. See code below. I guess it is the opposite of this that i want to implement.
I am not sure how I can show each image in the Text section. Does anyone out there know how to do this? should be obvious but I cannot find an answer anywhere.
I know get_content() will show the content, but how can I specifically show the images, so I can show these in a loop enabling me to make a grid using css and create a photo gallery.
currently this is the code I have:
<?php /* Template Name: Portrait Album */ ?>
<?php get_header(); ?>
<div class="portrait-page" style="background-color: black;">
<?php if (have_posts()) : while (have_posts()) : the_post();?>
<?php the_content(); ?>
echo "<img src='".wp_get_attachment_url( $the_post->ID )."' />";
<?php endwhile; endif; ?>
</div>
</div>
</div>
What I want to do is something like this, where col-1-3 is one-third of the width of the page
<div class="row">
<div class="col-1-3">
echo "<img src='"RELEVANT PHP CODE HERE"' />"
</div>
</div>

PHP if/else get_field() with WordPress

I'm trying to give a client some options in the WP backend to customize a carousel that is displayed on their website's homepage. I am using Advanced Custom Fields to handle getting the input from the client. I want to give them two options:
Option #1) Allows the client to insert a string of text to be displayed ('carousel_title_text')
Option #2) Allows the client to upload a logo to be displayed ('carousel_logo')
I want the code to check for title text, and if there is none, display the logo instead. I haven't yet decided what will happen if both fields are empty. Anyway, here is what I've come up with:
<?
if( get_field('carousel_title_text') ) { ?>
<h2 class="promo"><?php the_field('carousel_title_text');?></h2>
<? } elseif( get_field('carousel_logo') ) {
the_field('carousel_logo');
}
?>
The 'carousel_title_text' displays as long as there is an input, but when it's empty and 'carousel_logo' is not, it does not properly output the logo. Can anyone tell me what I'm doing wrong or if there's a better approach?
Thanks in advance,
If I get it right, your problem is not with the If/Else statement but with the logo field. I assume you properly configured this field (carousel_logo) as Image in ACF.
Image fields offer three different Return Value possibilities:
Note: In some versions of ACF you'll found Image Object instead of Image Array, but it's conceptually the same.
None of them will show your image properly if you just use the_field('carousel_logo'); in your template. So, how do you get your logo?
If you want the easiest solution, choose Image URL as Return Value for your logo field and print it like that:
<?
$title = get_field('carousel_title_text');
$logo = get_field('carousel_logo');
if (!empty($title)) : ?>
<h2 class="promo"><?= $title ?></h2>
<?php elseif (!empty($logo)) : ?>
<img class="logo" src="<?= $logo ?>">
<?php else: ?>
<!-- No title, no logo -->
<?php endif ?>
I've changed a little your code, but basically the key line here is:
<img class="logo" src="<?= $logo ?>">
This, or the equivalent:
<img class="logo" src="<?php echo $logo ?>">
should show your image when the title is missing.
Bonus: If you're handling different thumbnail sizes I'd recommend you to use Image Array as Return Value. In that case, $logo would be an Array containing all information (such as sizes, all thumbnail URLs, captions, etc.)
Use:
<img class="logo" src="<?= $logo['sizes']['medium'] ?>">
where 'medium' is your thumbnail size label to output it.
Also, please check official docs on ACF's Image field type for more examples and further explanations.
You have put else condition on last, that will default text will show if client will not Enter text ot logo.
you will used bellow code.
<?php
if( get_field('carousel_title_text') ) { ?>
<h2 class="promo"><?php the_field('carousel_title_text');?></h2>
<?php } elseif( get_field('carousel_logo') ) {
the_field('carousel_logo');
}else {
echo " <h2 class="promo"> Default slider title </h2>"
}?>
it's default title client indicate to he has not entered slider text
Hope it's work for you.
Try:
<?php
$output = (get_field('carousel_title_text')) ? get_field('carousel_title_text'):get_field('carousel_logo');
echo $output;
?>
This sounds like it's to do with the settings for the logo, which I'm assuming is an image field? In the Wordpress admin there will be a setting under that field called 'Return value' which can be set to either 'Array' or 'URL'.
If this is set to 'Array', you'd need to store that array to a variable and then access its properties in your markup like so:
<?php
if ( get_field('carousel_title_text') ) { ?>
<h2 class="promo"><?php the_field('carousel_title_text'); ?></h2>
<?php } else{
$carousel_logo = get_field('carousel_logo');
if ( $carousel_logo ) { ?>
<img src="<?php echo $carousel_logo['url']; ?>" />
<?php } else { ?>
<h2 class="promo">Default slider title </h2>
<?php }
}
?>
Alternatively, you could set your field to only return the image URL and then access it like so:
<?php
if ( get_field('carousel_title_text' ) { ?>
<h2 class="promo"><?php the_field('carousel_title_text'); ?></h2>
<?php } elseif ( $carousel_logo ) { ?>
<img src="<?php the_field('carousel_logo'); ?>" />
<?php } else { ?>
<h2 class="promo">Default slider title </h2>
<?php }
?>
ACF's documentation on the Image field will provide more info on this.

Wordpress use the_content() with out gallery?

Is it possible to use the_content() excluding the galleries? I want to make this because I need the gallery displayed with flexslider. I already integrate flexslider. If I want to show the content of the post, for example: some text, separated images and of course a gallery. It will display the gallery with flexslider and then the content with the text, images and the gallery again. I don't want the gallery duplicated.
<div class="entry-content">
<?php $gallery = get_post_gallery( get_the_ID(), false );?>
<div class="flexslider flexslider-gallery">
<ul class="slides slides-gallery">
<?php foreach( $gallery['src'] AS $src ){ ?>
<li>
<img alt="Gallery image" src="<?php echo $src; ?>" />
</li>
<?php } ?>
</ul> <!-- end .slides -->
</div> <!-- end .flexslider -->
<?php the_content(); ?>
</div><!-- .entry-content -->
Ok here is the solution:
Add to functions.php
function remove_shortcode_from($content) {
$content = strip_shortcodes( $content );
return $content;
}
and then call it when you need, in my case in content-gallery.php:
add_filter('the_content', 'remove_shortcode_from');
the_content();
remove_filter('the_content', 'remove_shortcode_from')
Two ways:
1) The WordPress Gallery is contained in a div with class .gallery. If flexsider does not also use that class, you could simply set your CSS to:
.gallery { display: none; }
2) You could get the content with get_the_content, parse it using a DOM parser and delete the Gallery.
Option #1 is less efficient, but avoids the complexity of parsing the DOM.
Neither method is guaranteed to work if the site is use a non-standard gallery plugin that is creating non-WP HTML.

Wordpress dynamic_sidebar() displaying unnecessary value

I have a wordpress dynamic sidebar.
<h2 style="text-align:center; ">
<?php //echo dynamic_sidebar('callus'); ?>
<?php echo dynamic_sidebar('callus'); ?>
</h2>
and the function is displaying everything ok but displaying a "1" at the bottom of the sidebar text. I searched a lot but couldnot debug. What might be the problem?
If you've got a problem with a Wordpress function, always check it in codex first (Hint: In Google enter codex followed by a space and paste the wordpress function name in there, this normally brings up the right page):
Usage
<?php dynamic_sidebar( $index ); ?>
Return Value (boolean)
True, if widget sidebar was found and called. False if not found or not called.
As you can see, do not echo. The function itself already takes care of the output. If the function now returns, let's say true, echo will additionally output 1 (boolean true to string conversion, echo is string context).
<h2 style="text-align:center; ">
<?php echo dynamic_sidebar('callus'); ?>
^^^^
</h2>
This echo is not needed. Instead:
<h2 style="text-align:center; ">
<?php dynamic_sidebar('callus'); ?>
</h2>

Categories