Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Header.php files inside page.php WordPress

Hey guys,

I needed an efficient way to have the open graph protocol tags assigned for article, profile for usage with new facebook api. Everything works perfect www.bangstyle.com but on the page template for the profile which I copied and paste the entire content for header, the featured articles on top only loads the entity I am on.

What should look like: www.bangstyle.com hover the "featured stories" on top
what it looks now: http://www.bangstyle.com/user-profile/?user_id=3166 hover the "featured stories" on top

The code for the query is


<?php query_posts($query_string . '&cat=3&showposts=5'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<li id="listingforfeatured" onclick="location.href='<?php the_permalink() ?>';" >
<div id="enadiotria" style="float:left; margin-right:5px; width:35px; height:35px; display:block; overflow:hidden">
<?php if ( has_post_thumbnail()) {
$large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), array(35,35));
echo '<a href="' . $large_image_url[0] . '" title="' . the_title_attribute('echo=0') . '" >';
the_post_thumbnail('thumbnail');
echo '</a>';
} ?>
</div>
<div style="text-align:left; line-height:14px; font-size:13px; font-family: 'Oswald',sans-serif; float:left; width:158px;"><a style="color:white" href="<?php the_permalink() ?>" >
<?php
$maxTitleLength = 50;
$t = get_the_title();
if( strlen($t) > $maxTitleLength ) {
// find the last space before truncation
$ls = strrchr(substr($t, 0, $maxTitleLength), ' ');
if( $ls ) $t = substr($t, 0, $maxTitleLength - strlen($ls)) . '...';
else $t = substr($t, 0, $maxTitleLength) . '...';
}
echo $t;
?>
</a></div>
</li>
<?php endwhile; wp_reset_query(); ?>
<?php endif; ?>


Not sure why its not working when i have the code on the page.php itself

Any help appreciated

Answers (4)

2011-10-22

Luis Cordova answers:

have you tried to insert some echos or debug right below the query or

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> ?

please check this first

2011-10-22

Kristin Falkner answers:

Have you tried resetting the query at the end instead of in the endwhile so:

<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); ?>

2011-10-22

Luis Abarca answers:

The header.php its included via get_header()

So the variables in header.php are not global, try to add your function in functions.php


// functions.php



function get_featured_stories()
{
global $query_string;
query_posts($query_string . '&cat=3&showposts=5'); ?>

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

<li id="listingforfeatured" onclick="location.href='<?php the_permalink() ?>';" >

<div id="enadiotria" style="float:left; margin-right:5px; width:35px; height:35px; display:block; overflow:hidden">

<?php if ( has_post_thumbnail()) {

$large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), array(35,35));

echo '<a href="' . $large_image_url[0] . '" title="' . the_title_attribute('echo=0') . '" >';

the_post_thumbnail('thumbnail');

echo '</a>';

} ?>

</div>

<div style="text-align:left; line-height:14px; font-size:13px; font-family: 'Oswald',sans-serif; float:left; width:158px;"><a style="color:white" href="<?php the_permalink() ?>" >

<?php

$maxTitleLength = 50;

$t = get_the_title();

if( strlen($t) > $maxTitleLength ) {

// find the last space before truncation

$ls = strrchr(substr($t, 0, $maxTitleLength), ' ');

if( $ls ) $t = substr($t, 0, $maxTitleLength - strlen($ls)) . '...';

else $t = substr($t, 0, $maxTitleLength) . '...';

}

echo $t;
?>
</a></div>
</li>
<?php endwhile; wp_reset_query(); ?>
<?php endif;
}



or you can simple get the query

// functions.php


function get_featured_stories()
{
global $query_string;

return new WP_Query($query_string . '&cat=3&showposts=5');
}


Then in you page.php


$myposts = get_featured_stories();

<?php if ( $myposts->have_posts() ) : while ( $myposts->have_posts() ) : $myposts->the_post(); ?>

<li id="listingforfeatured" onclick="location.href='<?php the_permalink() ?>';" >

<div id="enadiotria" style="float:left; margin-right:5px; width:35px; height:35px; display:block; overflow:hidden">

<?php if ( has_post_thumbnail()) {

$large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), array(35,35));

echo '<a href="' . $large_image_url[0] . '" title="' . the_title_attribute('echo=0') . '" >';

the_post_thumbnail('thumbnail');

echo '</a>';

} ?>

</div>

<div style="text-align:left; line-height:14px; font-size:13px; font-family: 'Oswald',sans-serif; float:left; width:158px;"><a style="color:white" href="<?php the_permalink() ?>" >

<?php

$maxTitleLength = 50;

$t = get_the_title();

if( strlen($t) > $maxTitleLength ) {

// find the last space before truncation

$ls = strrchr(substr($t, 0, $maxTitleLength), ' ');

if( $ls ) $t = substr($t, 0, $maxTitleLength - strlen($ls)) . '...';

else $t = substr($t, 0, $maxTitleLength) . '...';

}

echo $t;
?>
</a></div>
</li>
<?php endwhile; ?>
<?php endif;


Luis Abarca comments:

you can also cut all that code and create a new file "featured-stories.php", then just replace the code in your templates with:


get_template_part('featured-stories');


Nikolaos Vassos comments:

all your suggestions didnt work


Luis Abarca comments:

Can you paste te result of var_dump($query_string) to check whats going on on page.php and the custom query


Nikolaos Vassos comments:

tried the dump, nothing came up. should i put it after or before the query start?

2011-10-22

Linda answers:

Hi, try moving the wp_reset_query(); to after the endif;.

**edit** Kristin beat me to it. :)

-Linda


Nikolaos Vassos comments:

doesn't work unfortunately


Linda comments:

Hi again, so are you using query_posts for both the header and then again to pull up the users profile?


Linda comments:

If you are, try putting

<?php rewind_posts(); ?>

just before your second query on the page.


Nikolaos Vassos comments:

No users' profiles are using custom meta and custom user type.


Nikolaos Vassos comments:

i tried that regardless and no luck


Linda comments:

Hi, can you paste your entire page.php if you don't mind?

-Linda


Nikolaos Vassos comments:

Hey guys,

wordpress allows you like category files to have headers

all you need is header-two.php and call it as get_header('two')


Linda comments:

Awesome! Glad you got it working.

Linda