How to wrap HTML/PHP concatenation in Wordpress Function - wordpress

I'm making my first basic Wordpress Theme, and trying to create a function to retrieve and display the first 6 posts, which I could use in various areas depending on the page you're on. It is very similar to this tutorial, that I found... after facing the fact it doesn't work in my theme.
Here is a simplified piece of the code (I tried several versions) :
function show_last_items() {
while ( have_posts() && $count < 6) :
the_post();
$count++;
print '<article id="post-'.the_ID().'" class="post-link-display">
<a href="'.the_permalink().'" title="'.the_title_attribute().'">
<div class="date">
<p>'.the_time( 'd/m' ).'</p>
</div>
<div class="title">
<h6>'.the_title().'</h6>
</div>
</a>
</article>';
endwhile;
}
For some reason, while the code inside the function works just fine if I use it directly on a template part (for example sidebar.php), all the HTML seems to be wiped off when used as a function... Or more exactly, it still exists, but in-between the data. So I get something like this displayed :
55http://localhost/myTheme/posts/55/my-post-title/My post title19/03My
post title
56http://localhost/myTheme/posts/56/my-other-post-title/My other post
title19/03My other post title
Yet the <article> tag and every markup in it is still there, empty, after each line that displays the retrieved information. I'd really like to understand why the concatenation doesn't work properly in this case. Is it something about the theme support ? It doesn't seem to me the WP_Widget class would change anything to this issue, but maybe I'm wrong ?

Try this
function show_last_items() {
while ( have_posts() && $count < 6) :
the_post();
$count++;
sprintf('<article id="post-%s" class="post-link-display">
<a href="%s" title="%s">
<div class="date">
<p>%s</p>
</div>
<div class="title">
<h6>%s</h6>
</div>
</a>
</article>', the_ID(), the_permalink(), the_title_attribute(), the_time( 'd/m' ), the_title());
}

Have you tried building a string and then returning it?
$return_string = '';
// while loop
$return_string .= '<article .....';
// end while
return $return_string;

Related

how to edit $woo_options['woo_featured_height'

I have a old wordpress site that is defining some CSS using this code:
$fixed_height = ' style="height: ' . $woo_options['woo_featured_height'] . 'px;"';
<div class="slide <?php echo $css_class; ?>"<?php echo $fixed_height; ?>>
I cannot figure out where the $woo_options['woo_featured_height'] is defined. I have searched the entire codebase and there is no such value.
That's a woocommerce options array and most likely the option you are looking for comes from a database table and won't exists in your codebase. See this link for more information.

How to add a class to a Drupal 7 region?

I am trying to add a .clearfix class to my footer region in a Drupal 7. Is there a way to do this?
I am currently using the following to print my footer region:
<?php print render($page['footer']); ?>
Which outputs:
<div class="region region-footer">
<div id="block-1>....</div>
<div id="block-2>....</div>
</div>
Here's the code snippet:
function MY_THEME_NAME_preprocess_region(&$variables, $hook) {
if($variables['region'] == "MY_REGION_NAME"){
$variables['classes_array'][] = 'MY_CLASS_NAME';
}
}
Or if you'd rather insert the class into all of the regions:
function MY_THEME_NAME_preprocess_region(&$variables, $hook) {
$variables['classes_array'][] = 'MY_CLASS_NAME';
}
Copy region.tpl.php (found in modules/system directory) to your theme directory. Then copy everything inside it and create a new file. Paste into that file and make any changes you like to the template. Once finished, save it as region--footer.tpl.php and clear the cache on your site to see the changes.
The region.tpl.php contains (along with a lot of comments explaining possible variables):
<?php if ($content): ?>
<div class="<?php print $classes; ?>">
<?php print $content; ?>
</div>
<?php endif; ?>
So all you would need to do is add a class on that DIV.
It is even better if you use a hook, you can use template_preprocess_region.
Try adding the include to the footer.php.tpl file. You may have to create it.

Home Page Only Footer

I have a blog, http://sweatingthebigstuff.com and I would like to add an extra line in the footer which will display only from the homepage. I found this code, but it is not working for me. Do I have the wrong syntax or is there something else I can try to get this to work?
<?php if ( is_home() ) { ?>
text
<?php } ?>
Here is where footer.php is called
<?php include (TEMPLATEPATH . '/sidebar1.php'); ?>
<div class="cleared"></div>
<?php get_footer(); ?>
And here is the footer code:
text 2
Contact | Disclaimer | Privacy StatementCopyright © 2009-2010 Sweating The Big Stuff. All Rights Reserved.
and then some sitemeter crap.
is_home() sets a global var that doesn't seem to reset itself or re-evaluate, wp kinda strange.
Try putting wp_reset_query() at the end start of your if statement code
Actually, it'll be better to call it before as we can ensure the queries are reset
<?php wp_reset_query();
if ( is_home() ) { ?>
text
<?php } ?>
Now that the php is working, ideally you'd want the code above.
text
I just did a view source and I can plainly see the php code, which shouldn't be visible since it is meant to be parsed server side. The following shouldn't be there in the view source.... wrong file being edited?
<p>
<?php if ( is_home() ) { ?>
text
<?php } ?>
<wp_reset_query()>
<br />
<br />
The footer.php file should be located in wp-content/themes/nameofyourtheme folder
is_home() is a method that should return true or false. You need to implement this method somewhere. If blogspot doesn't implement this method for you, you need to do it yourself. For your website I think this function would do what you want:
<?php
function is_home(){
$r = $_SERVER['REQUEST_URI'];
return $r == '/' || $r == '' || $r == '/index.php';
}
if(is_home()) {
?>
text
<?php } ?>
And where you want the footer, put:
<?php include 'footer.php'; ?>
instead of the line:
<?php get_footer(); ?>
I believe your problem is that get_footer() is reading the footer as text, so it isn't executing the PHP beforehand. If you do it this way you can add as much PHP in the header as you want.
There is nothing wrong with your syntax, and when I view your page source, I see " text "
What is the file extension of your footer page? if it is "footer.php" then I shouldn't see the opening and closing terms for php (). php won't run unless the file extension is ".php"
as to a previous answer:
<?php wp_reset_query();?> should go BEFORE <?php if(is_home()){?>text<?php } ?> in this scenario. is_home() depends on a loop being present on the page. Maybe somewhere you used a custom query, or one of your plugins used a custom query that upset the default query vars. Like I said, use the reset statement before your if statement.
You need to modify the code to include a check for is_front_page() like so:
<?php
$ishomepage = ( is_home() || is_front_page() );
switch( $ishomepage )
{
case true :
echo 'Your homepage-only snippet of text goes here';
break;
case false :
default :
// Do nothing... or do something else...
break;
}
?>
Reference: WordPress Codex: is_front_page()
Default WP installations don't have a homepage defined, it uses your index.php and checks for other templates like a home.php template as a starting point (home.php only if your theme has it.) Here's a diagram from their online docs that shows how their hierarchy works: WordPress Template Hierarchy.
By default, WordPress shows your most recent posts on the front page
of your site. But many WordPress users want to have a static front
page or splash page as the front page instead. This "static front
page" look is common for those who wish to not have a "blog" look to
their site, giving it a more CMS (content management system) feel.
If you want to know how to set a static homepage, read this article and follow the instructions: Creating a Static Frontpage. When you set a static front page, is_home() will work as expected.

Adding a static text on top of view when it is not filtered

I want to add a static text (some sort of explanation/welcome text) on the very top of a view (over the filter) in Drupal 6.x with Views "2". I want this text appear only when the view is not filtered (i.e. on initial load of the page).
My problem is that the only place I figure out to make it work partially is in the views-exposed-form--MYVIEW.tpl.php. The problem is that when I place the code in this template, I don't know if the view is filtered or not, so the text appear on every single page! I don't have access to this info in that template so the only place this is available ($rows or $empty variables for example) is in views-view--MYVIEW.tpl.php.
But there I got an another problem. The order in witch it seams the variables are output are not the same as the order in witch they appeared in the file. For example, the $exposed variable content is render always on top, then $admin_links, $header and so forth.
<?php if ($header): ?>
<div class="view-header">
<?php print $header; ?>
</div>
<?php endif; ?>
<?php if (!$rows): ?>
<h3>This static text appear AFTER $exposed !!!</h3>
<?php endif; ?>
<?php if ($exposed): ?>
<div class="view-filters">
<?php print $exposed; ?>
</div>
<?php endif; ?>
<?php if ($attachment_before): ?>
<div class="attachment attachment-before">
<?php print $attachment_before; ?>
</div>
<?php endif; ?>
So even if I place my static content before this code, the filter form always appear on top!
I found the reason why is doing this: the exposed filter form is rendered as a part of the content-top <div></div>, but not the result (and $header, $footer, etc).
So is this by design? Do I miss something? How can I get my static text on the very top of the content-top!?
Well, after some tweaking and lecture on preprocess function in the theming system, I found a solution to my problem. I share it with you and if you find a more elegante approach; let me know!
What I did is this...
1) In the template.php file of the theme, I add two fonctions:
function YOURTHEME_preprocess_views_view__MYVIEW(&$variables) {
if ($variables['rows'] || $variables['empty']) {
$GLOBALS['dont_show_static_text'] = TRUE;
}
}
function YOURTHEME_preprocess_views_exposed_form__MYVIEW(&$variables) {
if ($GLOBALS['dont_show_static_text']) {
$variables['custom_flag1'] = TRUE;
}
}
So, if my views is showing some results ($row) or a blank result ($empty), then I set a flag to be use in the template file....
2) In my views-exposed-form--MYVIEW.tpl.php
...
<?php if (!$custom_flag1): ?>
<h2>Some static text here</h2>
<?php endif; ?>
...
And voilà! My static text is showing up only on the initial load of the view and it shows on TOP of the filter (not under!).
Hope this help someone else!

Adding next and previous buttons to static pages in wordpress?

I'm trying to add next and previous buttons to the static pages on my wordpress site.
I've been able to find some content on how to add these buttons to your blog post but haven't been able to find anything like this regarding static pages.
I'd like to add next and previous buttons to appear on the child pages within all the parent pages on my site, so you'd be able to use a link to navigate to the next/previous page located within the same parent.
Does anyone know how I could go about doing this or of any plugin that might help me out?
--
Thanks to markratledge, I've almost got it, but I just having one problem.
It seems the next and previous links are working almost how I'd like but they are coming in in alphabetical order when I want to to match the order I've got my pages ordered in.
this is what I've tried but it doesn't seem to work
$pagelist = get_pages('child_of='.$post->post_parent.'sort_column=menu_order');
Seems I just figured it out was missing &... should look like this.
$pagelist = get_pages('child_of='.$post->post_parent.'&sort_column=menu_order');
This should work, from the Wordpress Codex (Next and Previous Links « WordPress Codex).
Exclude pages with parameters in get_pages: http://codex.wordpress.org/Function_Reference/get_pages
(Or this plugin http://wordpress.org/extend/plugins/next-page/):
<?php
$pagelist = get_pages('sort_column=menu_order&sort_order=asc');
$pages = array();
foreach ($pagelist as $page) {
$pages[] += $page->ID;
}
$current = array_search($post->ID, $pages);
$prevID = $pages[$current-1];
$nextID = $pages[$current+1];
?>
<div class="navigation">
<?php if (!empty($prevID)) { ?>
<div class="alignleft">
<a href="<?php echo get_permalink($prevID); ?>"
title="<?php echo get_the_title($prevID); ?>">Previous</a>
</div>
<?php }
if (!empty($nextID)) { ?>
<div class="alignright">
<a href="<?php echo get_permalink($nextID); ?>"
title="<?php echo get_the_title($nextID); ?>">Next</a>
</div>
<?php } ?>
</div>

Resources