Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I am building a wordpress theme and for some reason my list of pages are showing outside the li when I view the source
<ul class="tabs">
<?php
while ( $queryObject->have_posts()) : $queryObject->the_post();
echo sprintf('<li>%s</li>', the_title());
endwhile;
?>
</ul>
And when I review the source in the inspector in chrome
<ul class="tabs">
Deans’ Corner<li></li>
Education Plan<li></li>
Counselors’ Corner<li></li>
</ul>
I got to be missing something simple
Take a look at the docs for the_title():
https://codex.wordpress.org/Function_Reference/the_title
Displays or returns the title of the current post.
[...]
$echo
(Boolean) (optional) Display the title (TRUE) or return it for use in PHP (FALSE).
Default: TRUE
the_title() takes an optional parameter $echo which decides whether it should return or echo the title of the page. Since you did not fill it and it defaults to the echo option, your code does not work.
If the_title() echoes, you won't be able to use it directly with sprintf().
Therefore, you can do just use HTML...
<li><?= the_title(); ?></li>
The below example is unnecessary because of the_title()'s optional parameters as pointed out in Timosta's answer.
Or you can use output buffering if you need to use the_title() as a function argument:
ob_start()
the_title();
$title = ob_get_clean();
echo sprintf('<li>%s</li>', $title);
The problem is that by default the_title() echos the title instead of returning it. You can actually specify what to echo before and after the title so there is no need for using sprintf().
This should work:
<ul class="tabs">
<?php
while ( $queryObject->have_posts()) : $queryObject->the_post();
the_title('<li>', '</li>');
endwhile;
?>
</ul>
Check the WordPress Codex:
http://codex.wordpress.org/Function_Reference/the_title
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have a php code which echos a html element:
<?php echo the_content(); ?>
It's resulting in:
<p>MY CUSTOM TEXT FROM ANOTHER PAGE INSIDE MY SITE</p>
How can I remove the <p> & </p> from the result?
I don't want to manually remove the tags <p> & </p> from the page it's coming from, because it is also used in another page in html.
*Edited
I'm currently trying this: *But it's not working...
$content = the_content();
echo strip_tags($content);
and
$content = the_content();
$content = strip_tags($content);
echo ($content);
You could use strip_tags():
<?php echo strip_tags(the_content()); ?>
Also:
<?= strip_tags(the_content()) ?>
I did it to work with this:
<?php echo substr(strip_tags($post->post_content), 0, 160) . '...'; ?>
Probably I was using the element the_content() from wordpress which includes automatically an HTML element <p> & </p> for the front-end page, with post_content it gets the inner content from the post without the f*cking <p> & </p>
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have a website in which I use just html, but I'd like to add one location where I would edit things like the nav and the footer. Then it would be instantly updated across the whole site.
So, for example, I'd like to put:
<nav>
<div class="nav-wrapper">
Logo
<ul id="nav-mobile" class="right hide-on-med-and-down">
<li>Sass</li>
<li>Components</li>
<li>JavaScript</li>
</ul>
</div>
</nav>
Into
<?php echo $nav;?>
Any help would be greatly appreciated! =)
Add the html that you want to show into a file. For eg:
add this to menu.html
<nav>
<div class="nav-wrapper">
Logo
<ul id="nav-mobile" class="right hide-on-med-and-down">
<li>Sass</li>
<li>Components</li>
<li>JavaScript</li>
</ul>
</div>
</nav>
Now include this page in other HTML pages wherever you want to call it like below
<?php
include 'menu.html';
?>
I like Lal's answer. I would change include 'menu.html'; with include_once 'menu.php'; The idea is that you will can add some php variables if you need them on some point.....
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Using advanced custom fields plugin on my wordpress site. I have a select option in one of the custom post types which determines if post going to be a link to another page or call out popup.
Here is how it looks.
<!-- here goes a code defining post type. Works fine -->
<?php $hmltype = get_field('post_url_or_popup'); ?>
<div class="tablecell midlineunit middle">
<a class="table midunithref mw1" <?php if ($hmltype == hml_url) { echo 'href="<?php the_field('hplb_url'); ?>" ' } endif; ?> >
</a>
</div>
Must be a syntax error, but I'm just starting out with php, so, kind of difficult to find the mistake.
What is hml_url? Is it a variable called $hml_url..? Back to your issues, you're using endif; here completely wrong. You can only ever call it if you instantiate it like this:
if(condition) :
do stuff;
endif;
Now to fix your print.
<a class="table midunithref mw1" <?php echo ($hmltype == hml_url) ? 'href="'. the_field('hplb_url'); .'"' : ''; ?> >
You'll need to figure out/tell us what hml_url is for us to solve your issue. You would've also seen the error if you turned your error reporting on. You can do this by adding this to the top of your script:
ini_set('display_errors', 1);
error_reporting(-1); // or you could do E_ALL
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm trying to edit a social sharing plugin, to change the link/text show in the popups for twitter, etc..
Basically when it clicks to share on Twitter, it displays the page title and current URL on a thank you page, I want to customize it to display certain text and a different URL. Tried changing the &url=<?php echo $url; ?> part to &url=<?php http://www.urltext.com; ?> for example but doesn't work, how can I edit this so the syntax is proper?
case 'twitter':
?><a rel="external nofollow" class="ss-twitter" href="http://twitter.com/intent/tweet/?text=<?php echo $title; ?>&url=<?php echo $url; ?><?php if(!empty($twitter_username)) { echo '&via=' . $twitter_username; } ?>" target="_blank"><span class="ss-icon-twitter"></span><?php echo $twitter_text; ?></a> <?php
break;
The syntax is incorrect. You'll want to use:
&url=<?php echo 'http://www.urltext.com'; ?>
The url is a string; so you need to echo it as a string.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I got a simple PHP code like this one:
<div id="master">
<div id="info">
<?php include 'division.php';
echo $winrate;
?>
</div>
<div id="lastmatches">
<?php include 'lastmatches.php';?>
</div>
</div>
</body>
As you see i want to echo $winrate, but $winrate is a variable that comes from lastmatches.php. So it will never work. Some has got an idea to echo $winrate in the div info? Im stuck and i hope you guys can help me out!
Thanks in advance!
You need to include lastmatches.php before to define $winrate.
But if this file outputs some content then you will want to use the caching system to output the right content at the right place.
<div id="master">
<div id="info">
<?php include 'division.php';
// begin cache
ob_start();
include 'lastmatches.php';
// end cache
$lastmatchescontent = ob_get_clean();
echo $winrate;
?>
</div>
<div id="lastmatches">
<?php echo $lastmatchescontent; ?>
</div>
</div>
</body>
i suggest you to follow MVC approach, if you do not want to use framework u can do something like this: https://github.com/LPodolski/basic_php_templating
this will allow code to be more readable, by separating concerns of generating output from getting data from db, parsing it etc