How would I code this for wordpress? - php

Hello I am fairly new to wordpress.
I was wondering if you guys have any idea on how to display post from wordpress and tweets
all in the same 'loop'.
I know It is possible using a if statement that displays a tweet ever nth-wordpress post. But I would like for both the tweets and the posts to be displayed by date in the same page. any ideas?
so far this is what my page looks like
<?php
get_header();
if(have_posts()) {
while(have_posts()) {
the_post();
$post_link = get_permalink(get_the_ID());
//($beforeString, $afterString, T/F)
//T/F retun in html-T or in php-F
the_title('<h1>', '</h1>');
the_content('<p>', '<p>');
//echo <a href="get_permalink()"
}
}
get_footer();
?>

I'd try to get both sources into one array, then sort it and print it out:
<?php
get_header();
//define array
$entrys=array();
//get wp-posts
if(have_posts()) {
while(have_posts()) {
the_post();
$timestamp=//timestamp of post
$entries[$timestamp]=array(
'link'=>get_permalink(get_the_ID()),
'title'=>get_the_title(get_the_ID()),
'content'=>get_the_content()
);
}
}
//insert the twitter-data into the same array here
//sort array
ksort($entries);
//print array
foreach($entries as $e){
echo '<div class="entry"><h1>'.$e['title'].'</h1>';
echo '<p>'.$e['content'].'</p>';
echo 'Link</div>';
}
get_footer();
?>

Related

Is there a way to group Wordpress search results by page template?

On my search results page, I have the standard loop that outputs pages and posts from the search query.
However, I want to create a group or section that gets all of the search results that return is_page_template('template-procedure-minimal') as TRUE first (preferably with a heading), and then output the rest of the list as usual.
How can this be done?
<?php get_search_form(); ?>
<hr class="dotted">
<div id="search-results" class="marginbottom2">
<?php if(have_posts()) {
// Results found
global $wp_query;
echo '<p>'.$wp_query->found_posts.' result'.(($wp_query->found_posts > 1) ? 's' : null).' found.</p>';
while(have_posts()) {
the_post();
echo '<li>'.get_the_title().'</li>';
}
get_template_part('nav', 'below');
} else {
// No results
echo '<p>Sorry, your search returned nothing. Did you spell it correctly?</p>';
} ?>
</div>
<p class="lead halfmargin">Can't find what you're looking for?</p>
<p>Contact Us</p>
You can use get_pate_template_slug() function to determine which template is using.
Here is an example (I haven't tested this code):
`
$page_template_posts = array();
// Results found
global $wp_query;
echo '<p>'.$wp_query->found_posts.' result'.(($wp_query->found_posts > 1) ? 's' : null).' found.</p>';
while(have_posts()) {
the_post();
$post_template = get_page_template_slug(get_the_ID());
array_push($page_template_posts[$post_template], get_post(get_the_ID()));
}
foreach ($page_template_posts as $key => $page_template_post){
echo '<div>'. $key .'</div>';
foreach ($page_template_post as $post_obj) {
echo '<li>'.get_the_title($post_obj->ID).'</li>';
}
}
get_template_part('nav', 'below');
} else {
// No results
echo '<p>Sorry, your search returned nothing. Did you spell it correctly?</p>';
} ?>
`

Do I need to place `endif` beneath each `if` Conditional Statement or can they be grouped together at the bottom of entire Blog Loop?

I have just inserted the following code into a page template:
<?php
//First Loop
$testBlog = new WP_Query(
'type=post&posts_per_page=1'
);
if ( $testBlog->have_posts() ):
while( $testBlog->have_posts() ): $testBlog->the_post();
get_template_part('format',get_post_format());
endwhile;
wp_reset_postdata();
//Second Loop
$newTestBlog = new WP_Query(
'type=post&posts_per_page=3&offset=0'
);
if ( $newTestBlog ->have_posts() ):
while( $newTestBlog ->have_posts() ): $newTestBlog ->the_post();
get_template_part('format',get_post_format());
endwhile;
wp_reset_postdata();
endif;
endif;
?>
Is it okay to end the above code with 2 endifs, as there are 2 if Conditional Statements or do the endifs need to be placed directly beneath its associated Conditional Statement before the next Conditional Statement is run?
Also, I have placed testBlog and newTestBlog into the code as I am under the impression they need to be unqiue. Is this the case or can they be duplicated?
First of all, if you use if (expression): you need also end endif; always. It is the equivalent to an { that you have to close as well with }.
But the endif; syntax should primary used in context with HTML code not in PHP only code:
<?php if ($header == true): ?>
<h1>Header</h1>
<?php else: ?>
<p>paragraph</p>
<?php endif; ?>
If you have PHP only code use this:
if ($header == true) {
// it's true
}
else {
// it's false
}
The same it true for while (expression): and endwhile; etc.
See PHP PSR-2: Coding Style Guide
Here how you code would look like:
<?php
$testBlog = new WP_Query('type=post&posts_per_page=1');
if ($testBlog->have_posts()) {
while ($testBlog->have_posts()) {
$testBlog->the_post();
get_template_part('format', get_post_format());
}
wp_reset_postdata();
$newTestBlog = new WP_Query('type=post&posts_per_page=3&offset=0');
if ($newTestBlog->have_posts()) {
while ($newTestBlog->have_posts()) {
$newTestBlog->the_post();
get_template_part('format', get_post_format());
}
wp_reset_postdata();
}
}
If you say now: this is not what I want, because you want to do 2 independent blocks, than you understand why this it's a better way, because you see your mistakes immediately. I assume you want to do this:
<?php
$testBlog = new WP_Query('type=post&posts_per_page=1');
if ($testBlog->have_posts()) {
while ($testBlog->have_posts()) {
$testBlog->the_post();
get_template_part('format', get_post_format());
}
wp_reset_postdata();
}
$newTestBlog = new WP_Query('type=post&posts_per_page=3&offset=0');
if ($newTestBlog->have_posts()) {
while ($newTestBlog->have_posts()) {
$newTestBlog->the_post();
get_template_part('format', get_post_format());
}
wp_reset_postdata();
}
Yes it is fine to use two endifs. Each endif will correspond to the first if conditional above it that it encounters, that has not been closed by another endif.

Passing values to wordpress shortcode

<?php while (have_rows('home_playlist', 'option')): the_row();
$track = get_sub_field('home_track'); ?>
echo $track;
Here echo generate some selective IDs 2,4,7,10,12 (mp3 songs title) from wp panel. And I need to show the title on the player. How do i pass the value of $track into the 'ids'....
<?php
$tracks=array();
while (have_rows('home_playlist', 'option')): the_row();
$tracks[] = get_sub_field('home_track'); ?>
$list= implode(',',$tracks);
echo $list;
mp3j_put( '[popout ids="'.$list.'" autoplay="y"]' ); ?>

Using same variable inside and outside loop

On my blog page I have a loop that uses custom template for displaying posts, content-blog.php.
content-blog.php is also used for rendering the messages Infinite Scroll fetches. It displays messages from category Twitter in one way and all other messages in other.
I want to add extra class to first post that is not in category Twitter to achieve this
twitter
twitter
any-other.extraclass
any-other
any-other
twitter
twitter
any-other.extraclass
any-other
Or add extra class to last Twitter to achieve this
twitter
twitter.extraclass
any-other
any-other
any-other
twitter
twitter.extraclass
any-other
any-other
I have figured that a structure in home.php
<?php $flag = 0; ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php if (has_category( 'Twitter' )): ?>
stuff done..
<?php $flag = 1; ?>
<?php else: ?>
stuff done and extra glass given if $flag == 1
<?php $flag = 0; ?>
<?php endif; ?>
<?php endwhile; // end of the loop. ?>
Works but it does not work when using a separate template part.
<?php $flag = 0; ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', 'blog' ); ?>
--content-blog.php--
<?php if (has_category( 'Twitter' )): ?>
stuff done..
<?php $flag = 1; ?>
<?php else: ?>
stuff done and extra glass given if $flag == 1
<?php $flag = 0; ?>
<?php endif; ?>
-- /content-blog.php --
<?php endwhile; // end of the loop. ?>
Today i learned the difference between local variable scope and global variable scope. After setting the variable in functions.php and declaring $flag global everything worked.
--content-blog.php--
<?php global $flag; ?>
<?php if (has_category( 'Twitter' )): ?>
stuff done..
<?php $flag = 1; ?>
<?php else: ?>
stuff done and extra glass given if $flag == 1
<?php $flag = 0; ?>
<?php endif; ?>
-- /content-blog.php --

Editing a WordPress Theme to resemble another site based off same theme

Building a Word Press based site using the Suburbia Word Press theme.
Trying to get the main page to resemble this site (http://appazoogle.com/) which uses the same theme. I need for the arraignment of the postings to resemble the ones on the main page of appazoogle. What's the secret?
My site is:
http://galleryeastnetwork.com/wordpress/
If you are talking about the first 3 posts being way wider than the rest of the site, then it seems like you just need to change out the HTML classes of the first three posts to be post one instead of post two
Provide more information on what you are trying to do and I would be glad to help
This would take place in the section you have commented, FIRST LOOP
It's hard to say what specific difference you want to remove. If it is those two big posts on the top, than you have to modify home.php of Suburbia theme from this
<?php get_header(); ?>
<?php include (TEMPLATEPATH . "/sidebar.php"); ?>
<?php
if ( $paged == 0 ) {
$offset1 = 0;
$offset2 = 2;
} else {
$off = $paged - 1;
$offset1 = $off * 7;
$offset2 = $off * 7 + 2;
}
?>
<!-- LOOP1 -->
<?php if (have_posts()) : ?>
<?php query_posts('posts_per_page=2&offset='.$offset1); ?>
<?php while (have_posts()) : the_post(); ?>
<div class="post two">
To this:
<?php get_header(); ?>
<?php include (TEMPLATEPATH . "/sidebar.php"); ?>
<?php
if ( $paged == 0 ) {
$offset1 = 0;
$offset2 = 4;
} else {
$off = $paged - 1;
$offset1 = $off * 7;
$offset2 = $off * 7 + 4;
}
?>
<!-- LOOP1 -->
<?php if (have_posts()) : ?>
<?php query_posts('posts_per_page=4&offset='.$offset1); ?>
<?php while (have_posts()) : the_post(); ?>
<div class="post one">
It is not only about class. It is also about correct offset.

Categories