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 --
Related
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.
I have an array with products as objects in it. Each object contains a key = category_title
I have a foreach loop that generates products in Bootstrap rows with configurable columns, in my case $col=3.
What I need is to have a new row if a new category_title comes up.
I created an array to push all the category_titles in it and create a variable if it is a new category. But now I am stuck.
The foreach looks like this:
<?php foreach($productspercat as $product): ?>
<!-- Make sure product is enabled and visible #front end -->
<?php // if($product->enabled && $product->visibility):?>
<?php
$catnew = false;
$categorytitle = $product->category_title;
if(!in_array($categorytitle, $totalcategories, true)):?>
<?php array_push($totalcategories,$categorytitle );
$catnew=true;
?>
<?php endif;?>
<div class="j2store-products-row <?php echo 'row-'.$row; ?> row">
<?php endif;?>
<div class="col-sm-<?php echo round((12 / $col));?>">
<h2><?php echo $categorytitle; ;
var_dump($catnew);
?></h2>
test
</div>
<?php $counter++;
$firstrun = true; ?>
<?php if (($rowcount == $col) or ($counter == $total)) : ?>
</div>
<?php endif; ?>
<?php // endif; ?>
<?php endforeach;?>
So basically if a new category comes up, it should close the row and start a fresh row with a new column count.
appreciate any help!
If you don't want to page refresh then you can do that by using jQuery or Angular JS.
By jQuery you can add data by jquery prepend() function if you wanna add data in starting or append() function to add ending section.
<?php
// Arranged products by category title
$arrangedProducts = array()
foreach($productspercat as $product) {
if (!array_key_exists($product->category_title, $arrangedProducts)) {
$arrangedProducts[$product->category_title] = array();
}
$arrangedProducts[$product->category_title][] = $product;
}
// each category is a key and its value is an array with all its products
foreach ($arrangedProducts as $categoryTitle => $products) {
// you create a new row
// ....
foreach ($products as $product) {
// you display product information
}
// you end the row
}
?>
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();
?>
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.
I am relatively new to The Zend framework having only been working with it probably two months at the most. I am now trying to get a list of results from a query I run, send it through the zend paginator class and display 4 results per page, however it does not seem to be recognising when it has got 4 results, I believe there is an error in my code but I can not for the life of me see it, I was hoping someone here will be able to pick up on it and help me and out and probably tell me it is something stupid that I have missed. Thanks here is the code I have written.
This the query in my model
public function getAllNews()
{
$db = Zend_Registry::get('db');
$sql = "SELECT * FROM $this->_name WHERE flag = 1 ORDER BY created_at";
$query = $db->query($sql);
while ($row = $query->fetchAll()) {
$result[] = $row;
}
return $result;
}
This is code in my controller
function preDispatch()
{
$this->_helper->layout->setLayout('latestnews');
Zend_Loader::loadClass('newsArticles');
$allNews = new newsArticles();
$this->view->allNews = $allNews->getAllnews();
//die (var_dump($this->view->allNews));
$data = $this->view->allNews;
// Instantiate the Zend Paginator and give it the Zend_Db_Select instance Argument ($selection)
$paginator = Zend_Paginator::factory($data);
// Set parameters for paginator
$paginator->setCurrentPageNumber($this->_getParam("page")); // Note: For this to work of course, your URL must be something like this: http://localhost:8888/index/index/page/1 <- meaning we are currently on page one, and pass that value into the "setCurrentPageNumber"
$paginator->setItemCountPerPage(1);
$paginator->setPageRange(4);
// Make paginator available in your views
$this->view->paginator = $paginator;
//die(var_dump($data));
}
Below is the view that builds the paginator,
<?php if ($this->pageCount): ?>
<div class="paginationControl">
<!-- Previous page link -->
<?php if (isset($this->previous)): ?>
< Previous |
<?php else: ?>
<span class="disabled">< Previous</span> |
<?php endif; ?>
<!-- Numbered page links -->
<?php foreach ($this->pagesInRange as $page): ?>
<?php if ($page != $this->current): ?>
<?= $page; ?> |
<?php else: ?>
<?= $page; ?> |
<?php endif; ?>
<?php endforeach; ?>
<!-- Next page link -->
<?php if (isset($this->next)): ?>
Next >
<?php else: ?>
<span class="disabled">Next ></span>
<?php endif; ?>
</div>
<?php endif; ?>
And finally the code the makes up my view
<div id="rightColumn">
<h3>Latest News</h3>
<?php if (count($this->paginator)): ?>
<ul>
<?php foreach ($this->paginator as $item): ?>
<?php
foreach ($item as $k => $v)
{
echo "<li>" . $v['title'] . "</li>";
}
?>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<?= $this->paginationControl($this->paginator, 'Sliding', '/latestnews/partial_pagination_control.phtml'); ?>
</div>
I will be greatful for any help you can give me.
Thanks
Sico
$paginator should be set to 4 as you want to show only 4 records at a time :
$paginator->setItemCountPerPage(4);
So I think the root of your problem is in your model class with the getAllNews function. The Zend_Db fetchAll function retrieves an associative array of all the records matching your query. You are returning an array of one element that is an array of all the rows retrieved by your select statement.
Try this:
public function getAllNews()
{
$db = Zend_Registry::get('db');
$sql = "SELECT * FROM $this->_name WHERE flag = 1 ORDER BY created_at";
return $db->fetchAll($sql);
}
You sould not pass values to paginator if you want it to limit your query.
There is a code will work for you
$usersTable = $this->getTable();
$registerSelect = $usersTable->select()->where('status = ?', 'OK')->order('lastLogin DESC');
$paginator = new Zend_Paginator(new Zend_Paginator_Adapter_DbTableSelect($registerSelect));
$paginator->setCurrentPageNumber($page);
$paginator->setItemCountPerPage($limit);